ebb_worker_bridge.erl 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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(distributed, Func, Args, Receiver) ->
  19. Node = ebb_work_manager:available_node(),
  20. ebb_work_manager:start_work(Node),
  21. start_link(Node, Func, Args, Receiver);
  22. start_link(Node, Func, Args, Receiver) ->
  23. supervisor_bridge:start_link(?MODULE, {Node, Func, Args, Receiver}).
  24. %%====================================================================
  25. %% supervisor_bridge callbacks
  26. %%====================================================================
  27. %%--------------------------------------------------------------------
  28. %% Funcion: init(Args) -> {ok, Pid, State} |
  29. %% ignore |
  30. %% {error, Reason}
  31. %% Description:Creates a supervisor_bridge process, linked to the calling
  32. %% process, which calls Module:init/1 to start the subsystem. To ensure a
  33. %% synchronized start-up procedure, this function does not return until
  34. %% Module:init/1 has returned.
  35. %%--------------------------------------------------------------------
  36. init({Func, Args, Receiver}) ->
  37. case ebb_worker:start_link(Func, Args, Receiver) of
  38. {ok, Pid} ->
  39. {ok, Pid, #state{mode=local, worker=Pid}};
  40. Error ->
  41. Error
  42. end;
  43. init({Node, Func, Args, Receiver}) ->
  44. case ebb_worker:start_link(Node, Func, Args, Receiver) of
  45. {ok, Pid} ->
  46. {ok, Pid, #state{mode={distributed, Node}, worker=Pid}};
  47. Error ->
  48. Error
  49. end.
  50. %%--------------------------------------------------------------------
  51. %% Func: terminate(Reason, State) -> void()
  52. %% Description:This function is called by the supervisor_bridge when it is
  53. %% about to terminate. It should be the opposite of Module:init/1 and stop
  54. %% the subsystem and do any necessary cleaning up.The return value is ignored.
  55. %%--------------------------------------------------------------------
  56. terminate(_Reason, #state{mode=local}) ->
  57. ok;
  58. terminate(_Reason, #state{mode={distributed, Node}}) ->
  59. ebb_work_manager:work_finished(Node),
  60. ok.
  61. %%====================================================================
  62. %% Internal functions
  63. %%====================================================================