-module(ebb_worker_bridge). -behaviour(supervisor_bridge). %% API -export([start_link/3, start_link/4]). %% supervisor_bridge callbacks -export([init/1, terminate/2]). -define(SERVER, ?MODULE). -record(state, {mode, worker}). %%==================================================================== %% API %%==================================================================== %%-------------------------------------------------------------------- %% Function: start_link() -> {ok,Pid} | ignore | {error,Error} %% Description: Starts the supervisor bridge %%-------------------------------------------------------------------- start_link(Func, Args, Receiver) -> supervisor_bridge:start_link(?MODULE, {Func, Args, Receiver}). start_link(distributed, Func, Args, Receiver) -> Node = ebb_work_manager:available_node(), ebb_work_manager:start_work(Node), start_link(Node, Func, Args, Receiver); start_link(Node, Func, Args, Receiver) -> supervisor_bridge:start_link(?MODULE, {Node, Func, Args, Receiver}). %%==================================================================== %% supervisor_bridge callbacks %%==================================================================== %%-------------------------------------------------------------------- %% Funcion: init(Args) -> {ok, Pid, State} | %% ignore | %% {error, Reason} %% Description:Creates a supervisor_bridge process, linked to the calling %% process, which calls Module:init/1 to start the subsystem. To ensure a %% synchronized start-up procedure, this function does not return until %% Module:init/1 has returned. %%-------------------------------------------------------------------- init({Func, Args, Receiver}) -> case ebb_worker:start_link(Func, Args, Receiver) of {ok, Pid} -> {ok, Pid, #state{mode=local, worker=Pid}}; Error -> Error end; init({Node, Func, Args, Receiver}) -> case ebb_worker:start_link(Node, Func, Args, Receiver) of {ok, Pid} -> {ok, Pid, #state{mode={distributed, Node}, worker=Pid}}; Error -> Error end. %%-------------------------------------------------------------------- %% Func: terminate(Reason, State) -> void() %% Description:This function is called by the supervisor_bridge when it is %% about to terminate. It should be the opposite of Module:init/1 and stop %% the subsystem and do any necessary cleaning up.The return value is ignored. %%-------------------------------------------------------------------- terminate(_Reason, #state{mode=local}) -> ok; terminate(_Reason, #state{mode={distributed, Node}}) -> ebb_work_manager:work_finished(Node), ok. %%==================================================================== %% Internal functions %%====================================================================