Add autocluster lock

This commit is contained in:
Feng Lee 2017-07-21 15:44:38 +08:00
parent c1bce429d9
commit 081717be92
1 changed files with 25 additions and 7 deletions

View File

@ -149,23 +149,41 @@ register_acl_mod() ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
start_autocluster() -> start_autocluster() ->
case application:get_env(?APP, autocluster_lock) of
undefined ->
application:set_env(?APP, autocluster_lock, true),
ekka:callback(prepare, fun emqttd:shutdown/1), ekka:callback(prepare, fun emqttd:shutdown/1),
ekka:callback(reboot, fun emqttd:reboot/0), ekka:callback(reboot, fun emqttd:reboot/0),
Fun = fun() -> ekka_autocluster:start(fun after_autocluster/0) end, run_outside_application({ekka_autocluster, start, [fun after_autocluster/0]});
run_outside_application(5000, Fun). {ok, _Lock} ->
ignore
end.
after_autocluster() -> after_autocluster() ->
emqttd_plugins:init(), emqttd_plugins:init(),
emqttd_plugins:load(), emqttd_plugins:load(),
start_listeners(). start_listeners().
run_outside_application(Delay, Callback) -> run_outside_application({M, F, Args}) ->
spawn(fun() -> spawn(fun() ->
group_leader(whereis(init), self()), group_leader(whereis(init), self()),
timer:sleep(Delay), wait_app_ready(5),
Callback() try erlang:apply(M, F, Args)
catch
_:Error -> lager:error("Autocluster exception: ~p", [Error])
end,
application:unset_env(?APP, autocluster_lock)
end). end).
wait_app_ready(0) ->
timeout;
wait_app_ready(Retries) ->
case lists:keymember(?APP, 1, application:which_applications()) of
true -> ok;
false -> timer:sleep(1000),
wait_app_ready(Retries - 1)
end.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Start Listeners %% Start Listeners
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------