diff options
author | Mickaël Rémond <mremond@process-one.net> | 2015-03-08 20:44:43 +0300 |
---|---|---|
committer | Mickaël Rémond <mremond@process-one.net> | 2015-03-08 20:44:43 +0300 |
commit | a339df2d6a6f88b6e8cf04e7c7afb1ff21bb376f (patch) | |
tree | 840959733956df2836be55b44afe35613be21cc1 /src/ejabberd_hooks.erl | |
parent | fd91ee516929d1dcfa3bfbd1584a308dc8573a75 (diff) |
More ejabberd_hooks refactor
Diffstat (limited to 'src/ejabberd_hooks.erl')
-rw-r--r-- | src/ejabberd_hooks.erl | 30 |
1 files changed, 16 insertions, 14 deletions
diff --git a/src/ejabberd_hooks.erl b/src/ejabberd_hooks.erl index 4530f2e26..9015bbe6a 100644 --- a/src/ejabberd_hooks.erl +++ b/src/ejabberd_hooks.erl @@ -304,9 +304,14 @@ code_change(_OldVsn, State, _Extra) -> %%% Internal functions %%%---------------------------------------------------------------------- +-spec run1([local_hook()|distributed_hook()], atom(), list()) -> ok. + run1([], _Hook, _Args) -> ok; +%% Run distributed hook on target node. +%% It is not attempted again in case of failure. Next hook will be executed run1([{_Seq, Node, Module, Function} | Ls], Hook, Args) -> + %% MR: Should we have a safe rpc, like we have a safe apply or is bad_rpc enough ? case rpc:call(Node, Module, Function, Args, ?TIMEOUT_DISTRIBUTED_HOOK) of timeout -> ?ERROR_MSG("Timeout on RPC to ~p~nrunning hook: ~p", @@ -326,15 +331,10 @@ run1([{_Seq, Node, Module, Function} | Ls], Hook, Args) -> run1(Ls, Hook, Args) end; run1([{_Seq, Module, Function} | Ls], Hook, Args) -> - Res = if is_function(Function) -> - catch apply(Function, Args); - true -> - catch apply(Module, Function, Args) - end, + Res = safe_apply(Module, Function, Args), case Res of {'EXIT', Reason} -> - ?ERROR_MSG("~p~nrunning hook: ~p", - [Reason, {Hook, Args}]), + ?ERROR_MSG("~p~nrunning hook: ~p", [Reason, {Hook, Args}]), run1(Ls, Hook, Args); stop -> ok; @@ -367,15 +367,10 @@ run_fold1([{_Seq, Node, Module, Function} | Ls], Hook, Val, Args) -> run_fold1(Ls, Hook, NewVal, Args) end; run_fold1([{_Seq, Module, Function} | Ls], Hook, Val, Args) -> - Res = if is_function(Function) -> - catch apply(Function, [Val | Args]); - true -> - catch apply(Module, Function, [Val | Args]) - end, + Res = safe_apply(Module, Function, [Val | Args]), case Res of {'EXIT', Reason} -> - ?ERROR_MSG("~p~nrunning hook: ~p", - [Reason, {Hook, Args}]), + ?ERROR_MSG("~p~nrunning hook: ~p", [Reason, {Hook, Args}]), run_fold1(Ls, Hook, Val, Args); stop -> stopped; @@ -384,3 +379,10 @@ run_fold1([{_Seq, Module, Function} | Ls], Hook, Val, Args) -> NewVal -> run_fold1(Ls, Hook, NewVal, Args) end. + +safe_apply(Module, Function, Args) -> + if is_function(Function) -> + catch apply(Function, Args); + true -> + catch apply(Module, Function, Args) + end. |