2004-08-08 21:07:55 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_hooks.erl
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2004-08-08 21:07:55 +02:00
|
|
|
%%% Purpose : Manage hooks
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 8 Aug 2004 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2017-01-02 21:41:53 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2017 ProcessOne
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
2009-01-12 15:44:42 +01:00
|
|
|
%%%
|
2014-02-22 11:27:40 +01:00
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
2004-08-08 21:07:55 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_hooks).
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2004-08-08 21:07:55 +02:00
|
|
|
|
|
|
|
-behaviour(gen_server).
|
|
|
|
|
|
|
|
%% External exports
|
|
|
|
-export([start_link/0,
|
2009-03-06 10:34:13 +01:00
|
|
|
add/3,
|
2004-08-08 21:07:55 +02:00
|
|
|
add/4,
|
2015-03-08 19:03:02 +01:00
|
|
|
add/5,
|
2009-04-28 16:57:16 +02:00
|
|
|
add_dist/5,
|
2015-03-08 19:03:02 +01:00
|
|
|
add_dist/6,
|
2009-03-06 10:34:13 +01:00
|
|
|
delete/3,
|
2004-08-08 21:07:55 +02:00
|
|
|
delete/4,
|
2005-06-20 05:18:13 +02:00
|
|
|
delete/5,
|
2015-03-08 19:03:02 +01:00
|
|
|
delete_dist/5,
|
2009-04-28 16:57:16 +02:00
|
|
|
delete_dist/6,
|
2015-03-08 19:03:02 +01:00
|
|
|
run/2,
|
2005-06-20 05:18:13 +02:00
|
|
|
run/3,
|
2015-03-08 19:03:02 +01:00
|
|
|
run_fold/3,
|
|
|
|
run_fold/4,
|
|
|
|
get_handlers/2]).
|
2004-08-08 21:07:55 +02:00
|
|
|
|
2015-03-08 17:06:43 +01:00
|
|
|
-export([delete_all_hooks/0]).
|
|
|
|
|
2004-08-08 21:07:55 +02:00
|
|
|
%% gen_server callbacks
|
|
|
|
-export([init/1,
|
|
|
|
handle_call/3,
|
|
|
|
handle_cast/2,
|
|
|
|
code_change/3,
|
|
|
|
handle_info/2,
|
|
|
|
terminate/2]).
|
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2004-08-08 21:07:55 +02:00
|
|
|
|
|
|
|
-record(state, {}).
|
2015-03-08 18:23:22 +01:00
|
|
|
-type local_hook() :: { Seq :: integer(), Module :: atom(), Function :: atom()}.
|
|
|
|
-type distributed_hook() :: { Seq :: integer(), Node :: atom(), Module :: atom(), Function :: atom()}.
|
2004-08-08 21:07:55 +02:00
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% API
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
start_link() ->
|
|
|
|
gen_server:start_link({local, ejabberd_hooks}, ejabberd_hooks, [], []).
|
|
|
|
|
2015-03-08 17:06:43 +01:00
|
|
|
-spec add(atom(), fun(), number()) -> ok.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2009-03-06 10:34:13 +01:00
|
|
|
%% @doc See add/4.
|
|
|
|
add(Hook, Function, Seq) when is_function(Function) ->
|
|
|
|
add(Hook, global, undefined, Function, Seq).
|
|
|
|
|
2015-03-08 17:06:43 +01:00
|
|
|
-spec add(atom(), HostOrModule :: binary() | atom(), fun() | atom() , number()) -> ok.
|
2009-03-06 10:34:13 +01:00
|
|
|
add(Hook, Host, Function, Seq) when is_function(Function) ->
|
|
|
|
add(Hook, Host, undefined, Function, Seq);
|
|
|
|
|
2009-01-21 18:36:28 +01:00
|
|
|
%% @doc Add a module and function to this hook.
|
|
|
|
%% The integer sequence is used to sort the calls: low number is called before high number.
|
2004-08-08 21:07:55 +02:00
|
|
|
add(Hook, Module, Function, Seq) ->
|
2005-06-20 05:18:13 +02:00
|
|
|
add(Hook, global, Module, Function, Seq).
|
|
|
|
|
2015-03-08 17:06:43 +01:00
|
|
|
-spec add(atom(), binary() | global, atom(), atom() | fun(), number()) -> ok.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
add(Hook, Host, Module, Function, Seq) ->
|
|
|
|
gen_server:call(ejabberd_hooks, {add, Hook, Host, Module, Function, Seq}).
|
2004-08-08 21:07:55 +02:00
|
|
|
|
2015-03-08 17:06:43 +01:00
|
|
|
-spec add_dist(atom(), atom(), atom(), atom() | fun(), number()) -> ok.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2009-04-28 16:57:16 +02:00
|
|
|
add_dist(Hook, Node, Module, Function, Seq) ->
|
|
|
|
gen_server:call(ejabberd_hooks, {add, Hook, global, Node, Module, Function, Seq}).
|
|
|
|
|
2015-03-08 17:06:43 +01:00
|
|
|
-spec add_dist(atom(), binary() | global, atom(), atom(), atom() | fun(), number()) -> ok.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2009-04-28 16:57:16 +02:00
|
|
|
add_dist(Hook, Host, Node, Module, Function, Seq) ->
|
|
|
|
gen_server:call(ejabberd_hooks, {add, Hook, Host, Node, Module, Function, Seq}).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec delete(atom(), fun(), number()) -> ok.
|
|
|
|
|
2009-03-06 10:34:13 +01:00
|
|
|
%% @doc See del/4.
|
|
|
|
delete(Hook, Function, Seq) when is_function(Function) ->
|
|
|
|
delete(Hook, global, undefined, Function, Seq).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec delete(atom(), binary() | atom(), atom() | fun(), number()) -> ok.
|
|
|
|
|
2009-03-06 11:03:29 +01:00
|
|
|
delete(Hook, Host, Function, Seq) when is_function(Function) ->
|
2009-03-06 10:34:13 +01:00
|
|
|
delete(Hook, Host, undefined, Function, Seq);
|
|
|
|
|
2009-01-21 18:36:28 +01:00
|
|
|
%% @doc Delete a module and function from this hook.
|
|
|
|
%% It is important to indicate exactly the same information than when the call was added.
|
2004-08-08 21:07:55 +02:00
|
|
|
delete(Hook, Module, Function, Seq) ->
|
2005-06-20 05:18:13 +02:00
|
|
|
delete(Hook, global, Module, Function, Seq).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec delete(atom(), binary() | global, atom(), atom() | fun(), number()) -> ok.
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
delete(Hook, Host, Module, Function, Seq) ->
|
|
|
|
gen_server:call(ejabberd_hooks, {delete, Hook, Host, Module, Function, Seq}).
|
2004-08-08 21:07:55 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec delete_dist(atom(), atom(), atom(), atom() | fun(), number()) -> ok.
|
|
|
|
|
2009-04-28 16:57:16 +02:00
|
|
|
delete_dist(Hook, Node, Module, Function, Seq) ->
|
|
|
|
delete_dist(Hook, global, Node, Module, Function, Seq).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec delete_dist(atom(), binary() | global, atom(), atom(), atom() | fun(), number()) -> ok.
|
|
|
|
|
2009-04-28 16:57:16 +02:00
|
|
|
delete_dist(Hook, Host, Node, Module, Function, Seq) ->
|
|
|
|
gen_server:call(ejabberd_hooks, {delete, Hook, Host, Node, Module, Function, Seq}).
|
|
|
|
|
2015-03-08 17:06:43 +01:00
|
|
|
-spec delete_all_hooks() -> true.
|
|
|
|
|
|
|
|
%% @doc Primarily for testing / instrumentation
|
|
|
|
delete_all_hooks() ->
|
|
|
|
gen_server:call(ejabberd_hooks, {delete_all}).
|
|
|
|
|
2015-03-08 18:23:22 +01:00
|
|
|
-spec get_handlers(atom(), binary() | global) -> [local_hook() | distributed_hook()].
|
|
|
|
%% @doc Returns currently set handler for hook name
|
|
|
|
get_handlers(Hookname, Host) ->
|
|
|
|
gen_server:call(ejabberd_hooks, {get_handlers, Hookname, Host}).
|
2015-03-08 17:06:43 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec run(atom(), list()) -> ok.
|
|
|
|
|
2009-01-21 18:36:28 +01:00
|
|
|
%% @doc Run the calls of this hook in order, don't care about function results.
|
|
|
|
%% If a call returns stop, no more calls are performed.
|
2004-08-08 21:07:55 +02:00
|
|
|
run(Hook, Args) ->
|
2005-06-20 05:18:13 +02:00
|
|
|
run(Hook, global, Args).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec run(atom(), binary() | global, list()) -> ok.
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
run(Hook, Host, Args) ->
|
|
|
|
case ets:lookup(hooks, {Hook, Host}) of
|
2004-08-08 21:07:55 +02:00
|
|
|
[{_, Ls}] ->
|
|
|
|
run1(Ls, Hook, Args);
|
|
|
|
[] ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec run_fold(atom(), any(), list()) -> any().
|
|
|
|
|
2009-01-21 18:36:28 +01:00
|
|
|
%% @doc Run the calls of this hook in order.
|
|
|
|
%% The arguments passed to the function are: [Val | Args].
|
|
|
|
%% The result of a call is used as Val for the next call.
|
|
|
|
%% If a call returns 'stop', no more calls are performed and 'stopped' is returned.
|
2014-08-26 01:04:15 +02:00
|
|
|
%% If a call returns {stop, NewVal}, no more calls are performed and NewVal is returned.
|
2004-08-08 21:07:55 +02:00
|
|
|
run_fold(Hook, Val, Args) ->
|
2005-06-20 05:18:13 +02:00
|
|
|
run_fold(Hook, global, Val, Args).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec run_fold(atom(), binary() | global, any(), list()) -> any().
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
run_fold(Hook, Host, Val, Args) ->
|
|
|
|
case ets:lookup(hooks, {Hook, Host}) of
|
2004-08-08 21:07:55 +02:00
|
|
|
[{_, Ls}] ->
|
|
|
|
run_fold1(Ls, Hook, Val, Args);
|
|
|
|
[] ->
|
2004-08-13 00:34:19 +02:00
|
|
|
Val
|
2004-08-08 21:07:55 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Callback functions from gen_server
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: init/1
|
|
|
|
%% Returns: {ok, State} |
|
|
|
|
%% {ok, State, Timeout} |
|
|
|
|
%% ignore |
|
|
|
|
%% {stop, Reason}
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
init([]) ->
|
2017-04-05 14:10:18 +02:00
|
|
|
ets:new(hooks, [named_table, {read_concurrency, true}]),
|
2004-08-08 21:07:55 +02:00
|
|
|
{ok, #state{}}.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_call/3
|
|
|
|
%% Returns: {reply, Reply, State} |
|
|
|
|
%% {reply, Reply, State, Timeout} |
|
|
|
|
%% {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, Reply, State} | (terminate/2 is called)
|
|
|
|
%% {stop, Reason, State} (terminate/2 is called)
|
|
|
|
%%----------------------------------------------------------------------
|
2007-12-07 00:15:04 +01:00
|
|
|
handle_call({add, Hook, Host, Module, Function, Seq}, _From, State) ->
|
2015-03-08 18:23:22 +01:00
|
|
|
HookFormat = {Seq, Module, Function},
|
|
|
|
Reply = handle_add(Hook, Host, HookFormat),
|
2004-08-08 21:07:55 +02:00
|
|
|
{reply, Reply, State};
|
2009-04-28 16:57:16 +02:00
|
|
|
handle_call({add, Hook, Host, Node, Module, Function, Seq}, _From, State) ->
|
2015-03-08 18:23:22 +01:00
|
|
|
HookFormat = {Seq, Node, Module, Function},
|
|
|
|
Reply = handle_add(Hook, Host, HookFormat),
|
2009-04-28 16:57:16 +02:00
|
|
|
{reply, Reply, State};
|
2015-03-08 18:23:22 +01:00
|
|
|
|
2007-12-07 00:15:04 +01:00
|
|
|
handle_call({delete, Hook, Host, Module, Function, Seq}, _From, State) ->
|
2015-03-08 18:23:22 +01:00
|
|
|
HookFormat = {Seq, Module, Function},
|
|
|
|
Reply = handle_delete(Hook, Host, HookFormat),
|
2004-08-08 21:07:55 +02:00
|
|
|
{reply, Reply, State};
|
2009-04-28 16:57:16 +02:00
|
|
|
handle_call({delete, Hook, Host, Node, Module, Function, Seq}, _From, State) ->
|
2015-03-08 18:23:22 +01:00
|
|
|
HookFormat = {Seq, Node, Module, Function},
|
|
|
|
Reply = handle_delete(Hook, Host, HookFormat),
|
2009-04-28 16:57:16 +02:00
|
|
|
{reply, Reply, State};
|
2015-03-08 18:23:22 +01:00
|
|
|
|
2015-03-08 17:06:43 +01:00
|
|
|
handle_call({get_handlers, Hook, Host}, _From, State) ->
|
|
|
|
Reply = case ets:lookup(hooks, {Hook, Host}) of
|
2015-03-08 18:23:22 +01:00
|
|
|
[{_, Handlers}] -> Handlers;
|
|
|
|
[] -> []
|
2015-03-08 17:06:43 +01:00
|
|
|
end,
|
|
|
|
{reply, Reply, State};
|
2015-03-08 18:23:22 +01:00
|
|
|
|
2015-03-08 17:06:43 +01:00
|
|
|
handle_call({delete_all}, _From, State) ->
|
|
|
|
Reply = ets:delete_all_objects(hooks),
|
|
|
|
{reply, Reply, State};
|
2015-03-08 18:23:22 +01:00
|
|
|
|
2007-12-07 00:15:04 +01:00
|
|
|
handle_call(_Request, _From, State) ->
|
2004-08-08 21:07:55 +02:00
|
|
|
Reply = ok,
|
|
|
|
{reply, Reply, State}.
|
|
|
|
|
2015-03-08 18:23:22 +01:00
|
|
|
-spec handle_add(atom(), atom(), local_hook() | distributed_hook()) -> ok.
|
|
|
|
%% in-memory storage operation: Handle adding hook in ETS table
|
|
|
|
handle_add(Hook, Host, El) ->
|
|
|
|
case ets:lookup(hooks, {Hook, Host}) of
|
|
|
|
[{_, Ls}] ->
|
|
|
|
case lists:member(El, Ls) of
|
|
|
|
true ->
|
|
|
|
ok;
|
|
|
|
false ->
|
|
|
|
NewLs = lists:merge(Ls, [El]),
|
|
|
|
ets:insert(hooks, {{Hook, Host}, NewLs}),
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
[] ->
|
|
|
|
NewLs = [El],
|
|
|
|
ets:insert(hooks, {{Hook, Host}, NewLs}),
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
-spec handle_delete(atom(), atom(), local_hook() | distributed_hook()) -> ok.
|
|
|
|
%% in-memory storage operation: Handle deleting hook from ETS table
|
|
|
|
handle_delete(Hook, Host, El) ->
|
|
|
|
case ets:lookup(hooks, {Hook, Host}) of
|
|
|
|
[{_, Ls}] ->
|
|
|
|
NewLs = lists:delete(El, Ls),
|
|
|
|
ets:insert(hooks, {{Hook, Host}, NewLs}),
|
|
|
|
ok;
|
|
|
|
[] ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2004-08-08 21:07:55 +02:00
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_cast/2
|
|
|
|
%% Returns: {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, State} (terminate/2 is called)
|
|
|
|
%%----------------------------------------------------------------------
|
2007-12-07 00:15:04 +01:00
|
|
|
handle_cast(_Msg, State) ->
|
2004-08-08 21:07:55 +02:00
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_info/2
|
|
|
|
%% Returns: {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, State} (terminate/2 is called)
|
|
|
|
%%----------------------------------------------------------------------
|
2007-12-07 00:15:04 +01:00
|
|
|
handle_info(_Info, State) ->
|
2004-08-08 21:07:55 +02:00
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: terminate/2
|
|
|
|
%% Purpose: Shutdown the server
|
|
|
|
%% Returns: any (ignored by gen_server)
|
|
|
|
%%----------------------------------------------------------------------
|
2007-12-07 00:15:04 +01:00
|
|
|
terminate(_Reason, _State) ->
|
2004-08-08 21:07:55 +02:00
|
|
|
ok.
|
|
|
|
|
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2015-03-08 18:44:43 +01:00
|
|
|
-spec run1([local_hook()|distributed_hook()], atom(), list()) -> ok.
|
|
|
|
|
2007-12-07 00:15:04 +01:00
|
|
|
run1([], _Hook, _Args) ->
|
2004-08-08 21:07:55 +02:00
|
|
|
ok;
|
2015-03-08 18:44:43 +01:00
|
|
|
%% Run distributed hook on target node.
|
|
|
|
%% It is not attempted again in case of failure. Next hook will be executed
|
2009-04-28 16:57:16 +02:00
|
|
|
run1([{_Seq, Node, Module, Function} | Ls], Hook, Args) ->
|
2015-03-08 18:44:43 +01:00
|
|
|
%% MR: Should we have a safe rpc, like we have a safe apply or is bad_rpc enough ?
|
2015-10-07 00:06:58 +02:00
|
|
|
case ejabberd_cluster:call(Node, Module, Function, Args) of
|
2009-04-28 16:57:16 +02:00
|
|
|
timeout ->
|
|
|
|
?ERROR_MSG("Timeout on RPC to ~p~nrunning hook: ~p",
|
|
|
|
[Node, {Hook, Args}]),
|
|
|
|
run1(Ls, Hook, Args);
|
|
|
|
{badrpc, Reason} ->
|
|
|
|
?ERROR_MSG("Bad RPC error to ~p: ~p~nrunning hook: ~p",
|
|
|
|
[Node, Reason, {Hook, Args}]),
|
|
|
|
run1(Ls, Hook, Args);
|
|
|
|
stop ->
|
|
|
|
?INFO_MSG("~nThe process ~p in node ~p ran a hook in node ~p.~n"
|
|
|
|
"Stop.", [self(), node(), Node]), % debug code
|
|
|
|
ok;
|
|
|
|
Res ->
|
|
|
|
?INFO_MSG("~nThe process ~p in node ~p ran a hook in node ~p.~n"
|
|
|
|
"The response is:~n~s", [self(), node(), Node, Res]), % debug code
|
|
|
|
run1(Ls, Hook, Args)
|
|
|
|
end;
|
2004-08-08 21:07:55 +02:00
|
|
|
run1([{_Seq, Module, Function} | Ls], Hook, Args) ->
|
2017-01-09 15:02:17 +01:00
|
|
|
Res = safe_apply(Hook, Module, Function, Args),
|
2009-03-06 10:34:13 +01:00
|
|
|
case Res of
|
2017-01-09 15:02:17 +01:00
|
|
|
'EXIT' ->
|
2004-08-08 21:07:55 +02:00
|
|
|
run1(Ls, Hook, Args);
|
|
|
|
stop ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
run1(Ls, Hook, Args)
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2007-12-07 00:15:04 +01:00
|
|
|
run_fold1([], _Hook, Val, _Args) ->
|
2004-08-08 21:07:55 +02:00
|
|
|
Val;
|
2009-04-28 16:57:16 +02:00
|
|
|
run_fold1([{_Seq, Node, Module, Function} | Ls], Hook, Val, Args) ->
|
2015-10-07 00:06:58 +02:00
|
|
|
case ejabberd_cluster:call(Node, Module, Function, [Val | Args]) of
|
2009-04-28 16:57:16 +02:00
|
|
|
{badrpc, Reason} ->
|
|
|
|
?ERROR_MSG("Bad RPC error to ~p: ~p~nrunning hook: ~p",
|
|
|
|
[Node, Reason, {Hook, Args}]),
|
|
|
|
run_fold1(Ls, Hook, Val, Args);
|
|
|
|
timeout ->
|
|
|
|
?ERROR_MSG("Timeout on RPC to ~p~nrunning hook: ~p",
|
|
|
|
[Node, {Hook, Args}]),
|
|
|
|
run_fold1(Ls, Hook, Val, Args);
|
|
|
|
stop ->
|
|
|
|
stopped;
|
|
|
|
{stop, NewVal} ->
|
|
|
|
?INFO_MSG("~nThe process ~p in node ~p ran a hook in node ~p.~n"
|
|
|
|
"Stop, and the NewVal is:~n~p", [self(), node(), Node, NewVal]), % debug code
|
|
|
|
NewVal;
|
|
|
|
NewVal ->
|
|
|
|
?INFO_MSG("~nThe process ~p in node ~p ran a hook in node ~p.~n"
|
|
|
|
"The NewVal is:~n~p", [self(), node(), Node, NewVal]), % debug code
|
|
|
|
run_fold1(Ls, Hook, NewVal, Args)
|
|
|
|
end;
|
2004-08-08 21:07:55 +02:00
|
|
|
run_fold1([{_Seq, Module, Function} | Ls], Hook, Val, Args) ->
|
2017-01-09 15:02:17 +01:00
|
|
|
Res = safe_apply(Hook, Module, Function, [Val | Args]),
|
2009-03-06 10:34:13 +01:00
|
|
|
case Res of
|
2017-01-09 15:02:17 +01:00
|
|
|
'EXIT' ->
|
2004-08-08 21:07:55 +02:00
|
|
|
run_fold1(Ls, Hook, Val, Args);
|
|
|
|
stop ->
|
|
|
|
stopped;
|
|
|
|
{stop, NewVal} ->
|
|
|
|
NewVal;
|
|
|
|
NewVal ->
|
|
|
|
run_fold1(Ls, Hook, NewVal, Args)
|
|
|
|
end.
|
2015-03-08 18:44:43 +01:00
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
safe_apply(Hook, Module, Function, Args) ->
|
2016-12-28 07:47:11 +01:00
|
|
|
try if is_function(Function) ->
|
|
|
|
apply(Function, Args);
|
2015-03-08 18:44:43 +01:00
|
|
|
true ->
|
2016-12-28 07:47:11 +01:00
|
|
|
apply(Module, Function, Args)
|
|
|
|
end
|
2017-01-23 19:52:25 +01:00
|
|
|
catch E:R when E /= exit; R /= normal ->
|
2017-01-09 15:02:17 +01:00
|
|
|
?ERROR_MSG("Hook ~p crashed when running ~p:~p/~p:~n"
|
|
|
|
"** Reason = ~p~n"
|
|
|
|
"** Arguments = ~p",
|
|
|
|
[Hook, Module, Function, length(Args),
|
|
|
|
{E, R, get_stacktrace()}, Args]),
|
|
|
|
'EXIT'
|
2015-03-08 18:44:43 +01:00
|
|
|
end.
|
2017-01-09 15:02:17 +01:00
|
|
|
|
|
|
|
get_stacktrace() ->
|
|
|
|
[{Mod, Fun, Loc, Args} || {Mod, Fun, Args, Loc} <- erlang:get_stacktrace()].
|