ebb_worker_bridge.erl 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. -module(ebb_worker_bridge).
  2. -behaviour(supervisor_bridge).
  3. %% API
  4. -export([start_link/3, start_link/4]).
  5. %% supervisor_bridge callbacks
  6. -export([init/1, terminate/2]).
  7. -define(SERVER, ?MODULE).
  8. -record(state, {mode, worker}).
  9. %%====================================================================
  10. %% API
  11. %%====================================================================
  12. %%--------------------------------------------------------------------
  13. %% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
  14. %% Description: Starts the supervisor bridge
  15. %%--------------------------------------------------------------------
  16. start_link(Func, Args, Receiver) ->
  17. supervisor_bridge:start_link(?MODULE, {Func, Args, Receiver}).
  18. start_link(Node, Func, Args, Receiver) ->
  19. supervisor_bridge:start_link(?MODULE, {Node, Func, Args, Receiver}).
  20. %%====================================================================
  21. %% supervisor_bridge callbacks
  22. %%====================================================================
  23. %%--------------------------------------------------------------------
  24. %% Funcion: init(Args) -> {ok, Pid, State} |
  25. %% ignore |
  26. %% {error, Reason}
  27. %% Description:Creates a supervisor_bridge process, linked to the calling
  28. %% process, which calls Module:init/1 to start the subsystem. To ensure a
  29. %% synchronized start-up procedure, this function does not return until
  30. %% Module:init/1 has returned.
  31. %%--------------------------------------------------------------------
  32. init({Func, Args, Receiver}) ->
  33. case ebb_worker:start_link(Func, Args, Receiver) of
  34. {ok, Pid} ->
  35. {ok, Pid, #state{mode=local, worker=Pid}};
  36. Error ->
  37. Error
  38. end;
  39. init({Node, Func, Args, Receiver}) ->
  40. case ebb_worker:start_link(Node, Func, Args, Receiver) of
  41. {ok, Pid} ->
  42. {ok, Pid, #state{mode={distributed, Node}, worker=Pid}};
  43. Error ->
  44. Error
  45. end.
  46. %%--------------------------------------------------------------------
  47. %% Func: terminate(Reason, State) -> void()
  48. %% Description:This function is called by the supervisor_bridge when it is
  49. %% about to terminate. It should be the opposite of Module:init/1 and stop
  50. %% the subsystem and do any necessary cleaning up.The return value is ignored.
  51. %%--------------------------------------------------------------------
  52. terminate(normal, _State) ->
  53. ok;
  54. terminate(noconnection, #state{mode={distributed, Node}}) ->
  55. ebb_work_manager:remove_node(Node),
  56. exit(noconnection);
  57. terminate(Reason, _State) ->
  58. exit(Reason).
  59. %%====================================================================
  60. %% Internal functions
  61. %%====================================================================