Skip to content

Instantly share code, notes, and snippets.

@soranoba
Last active June 19, 2016 07:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soranoba/8cca877203a04291b111543b046b46c4 to your computer and use it in GitHub Desktop.
Save soranoba/8cca877203a04291b111543b046b46c4 to your computer and use it in GitHub Desktop.
A -> B -> C
-behaviour(gen_server).
init(_) ->
{ok, #state{member = undefined}}.
handle_call(_, _, #state{member = undefined} = State) -> {noreply, State}.
handle_cast({sync, Pids}, State) -> {noreply, #state{member = Pids}}.
-behaviour(supervisor).
start_link() ->
case supervisor:start_link(?MODULE, []) of
{ok, Pid} ->
Pids = [Pid || {_, Pid, _, _} <- supervisor:which_children(Pid)],
ok = lists:foreach(fun(P) -> gen_server:cast(P, {sync, Pids}) end, Pids),
{ok, Pid};
Err -> Err
end.
-behaviour(gen_server).
init([SupPid]) ->
ok = gen_server:cast(self(), sync),
{ok, #state{sup = SupPid}}.
handle_cast(sync, #state{sup = SupPid} = State) ->
%% この時点で全員の子が取得できるはず.... initの処理が重いとまずそう
Pids = [Pid || {_, Pid, _, _} <- supervisor:which_children(SupPid)],
{noreply, State#state{pids = Pids}}.
-behaviour(supervisor).
start_link() ->
supervisor:start_link(?MODULE, []).
init(_) ->
SupPid = self(),
%% mfa以外は省略
{ok, {{one_for_all, 5, 10}, [#{start => {M, F, [SupPid]}}, #{start => {M, F, [SupPid]}}, ...]}}.
@soranoba
Copy link
Author

aaaはtemporaryじゃないと使えない

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment