25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-24 16:23:40 +01:00

Check result of gen_mod:start/2 callback (#1534)

This commit is contained in:
Evgeniy Khramtsov 2017-02-13 11:11:41 +03:00
parent fd885d0818
commit e1ba499bd6
8 changed files with 44 additions and 58 deletions

View File

@ -33,7 +33,7 @@
-export([start/0, start_link/0]).
-export([route/3, route_iq/4, route_iq/5, process_iq/3,
process_iq_reply/3, register_iq_handler/4, get_features/1,
process_iq_reply/3, get_features/1,
register_iq_handler/5, register_iq_response_handler/4,
register_iq_response_handler/5, unregister_iq_handler/2,
unregister_iq_response_handler/2, bounce_resource_packet/3]).
@ -83,9 +83,6 @@ process_iq(From, To, #iq{type = T, lang = Lang, sub_els = [El]} = Packet)
XMLNS = xmpp:get_ns(El),
Host = To#jid.lserver,
case ets:lookup(?IQTABLE, {Host, XMLNS}) of
[{_, Module, Function}] ->
gen_iq_handler:handle(Host, Module, Function, no_queue,
From, To, Packet);
[{_, Module, Function, Opts}] ->
gen_iq_handler:handle(Host, Module, Function, Opts,
From, To, Packet);
@ -161,24 +158,21 @@ register_iq_response_handler(_Host, ID, Module,
function = Function,
timer = TRef}).
-spec register_iq_handler(binary(), binary(), module(), function()) -> any().
register_iq_handler(Host, XMLNS, Module, Fun) ->
ejabberd_local !
{register_iq_handler, Host, XMLNS, Module, Fun}.
-spec register_iq_handler(binary(), binary(), module(), function(),
gen_iq_handler:opts()) -> any().
gen_iq_handler:opts()) -> ok.
register_iq_handler(Host, XMLNS, Module, Fun, Opts) ->
ejabberd_local !
{register_iq_handler, Host, XMLNS, Module, Fun, Opts}.
{register_iq_handler, Host, XMLNS, Module, Fun, Opts},
ok.
-spec unregister_iq_response_handler(binary(), binary()) -> ok.
unregister_iq_response_handler(_Host, ID) ->
catch get_iq_callback(ID), ok.
-spec unregister_iq_handler(binary(), binary()) -> any().
-spec unregister_iq_handler(binary(), binary()) -> ok.
unregister_iq_handler(Host, XMLNS) ->
ejabberd_local ! {unregister_iq_handler, Host, XMLNS}.
ejabberd_local ! {unregister_iq_handler, Host, XMLNS},
ok.
-spec bounce_resource_packet(jid(), jid(), stanza()) -> stop.
bounce_resource_packet(_From, #jid{lresource = <<"">>}, #presence{}) ->
@ -238,11 +232,6 @@ handle_info({route, From, To, Packet}, State) ->
_ -> ok
end,
{noreply, State};
handle_info({register_iq_handler, Host, XMLNS, Module,
Function},
State) ->
ets:insert(?IQTABLE, {{Host, XMLNS}, Module, Function}),
{noreply, State};
handle_info({register_iq_handler, Host, XMLNS, Module,
Function, Opts},
State) ->

View File

@ -55,7 +55,6 @@
get_vh_session_list/1,
get_vh_session_number/1,
get_vh_by_backend/1,
register_iq_handler/4,
register_iq_handler/5,
unregister_iq_handler/2,
force_update_presence/1,
@ -356,18 +355,17 @@ get_vh_session_number(Server) ->
Mod = get_sm_backend(LServer),
length(online(Mod:get_sessions(LServer))).
register_iq_handler(Host, XMLNS, Module, Fun) ->
ejabberd_sm ! {register_iq_handler, Host, XMLNS, Module, Fun}.
-spec register_iq_handler(binary(), binary(), atom(), atom(), list()) -> any().
-spec register_iq_handler(binary(), binary(), atom(), atom(), list()) -> ok.
register_iq_handler(Host, XMLNS, Module, Fun, Opts) ->
ejabberd_sm ! {register_iq_handler, Host, XMLNS, Module, Fun, Opts}.
ejabberd_sm ! {register_iq_handler, Host, XMLNS, Module, Fun, Opts},
ok.
-spec unregister_iq_handler(binary(), binary()) -> any().
-spec unregister_iq_handler(binary(), binary()) -> ok.
unregister_iq_handler(Host, XMLNS) ->
ejabberd_sm ! {unregister_iq_handler, Host, XMLNS}.
ejabberd_sm ! {unregister_iq_handler, Host, XMLNS},
ok.
%% Why the hell do we have so many similar kicks?
c2s_handle_info(#{lang := Lang} = State, replaced) ->
@ -421,9 +419,6 @@ handle_info({route, From, To, Packet}, State) ->
ok
end,
{noreply, State};
handle_info({register_iq_handler, Host, XMLNS, Module, Function}, State) ->
ets:insert(sm_iqtable, {{Host, XMLNS}, Module, Function}),
{noreply, State};
handle_info({register_iq_handler, Host, XMLNS, Module,
Function, Opts},
State) ->
@ -744,9 +739,6 @@ process_iq(From, To, #iq{type = T, lang = Lang, sub_els = [El]} = Packet)
XMLNS = xmpp:get_ns(El),
Host = To#jid.lserver,
case ets:lookup(sm_iqtable, {Host, XMLNS}) of
[{_, Module, Function}] ->
gen_iq_handler:handle(Host, Module, Function, no_queue,
From, To, Packet);
[{_, Module, Function, Opts}] ->
gen_iq_handler:handle(Host, Module, Function, Opts,
From, To, Packet);

View File

@ -60,7 +60,7 @@ start_link(Host, Module, Function) ->
gen_server:start_link(?MODULE, [Host, Module, Function],
[]).
-spec add_iq_handler(module(), binary(), binary(), module(), atom(), type()) -> any().
-spec add_iq_handler(module(), binary(), binary(), module(), atom(), type()) -> ok.
add_iq_handler(Component, Host, NS, Module, Function,
Type) ->
@ -89,7 +89,7 @@ add_iq_handler(Component, Host, NS, Module, Function,
Function, parallel)
end.
-spec remove_iq_handler(component(), binary(), binary()) -> any().
-spec remove_iq_handler(component(), binary(), binary()) -> ok.
remove_iq_handler(Component, Host, NS) ->
Component:unregister_iq_handler(Host, NS).

View File

@ -51,7 +51,7 @@
-type opts() :: [{atom(), any()}].
-type db_type() :: sql | mnesia | riak.
-callback start(binary(), opts()) -> any().
-callback start(binary(), opts()) -> ok | {ok, pid()}.
-callback stop(binary()) -> any().
-callback mod_opt_type(atom()) -> fun((term()) -> term()) | [atom()].
-callback depends(binary(), opts()) -> [{module(), hard | soft}].
@ -145,19 +145,24 @@ start_module(Host, Module) ->
{error, not_found_in_config}
end.
-spec start_module(binary(), atom(), opts()) -> any().
-spec start_module(binary(), atom(), opts()) -> ok | {ok, pid()}.
start_module(Host, Module, Opts0) ->
?DEBUG("loading ~s at ~s", [Module, Host]),
Opts = validate_opts(Module, Opts0),
ets:insert(ejabberd_modules,
#ejabberd_module{module_host = {Module, Host},
opts = Opts}),
try Module:start(Host, Opts) catch
Class:Reason ->
try case Module:start(Host, Opts) of
ok -> ok;
{ok, Pid} when is_pid(Pid) -> {ok, Pid};
Err -> erlang:error(Err)
end
catch Class:Reason ->
ets:delete(ejabberd_modules, {Module, Host}),
ErrorText =
io_lib:format("Problem starting the module ~p for host "
"~p ~n options: ~p~n ~p: ~p~n~p",
io_lib:format("Problem starting the module ~s for host "
"~s ~n options: ~p~n ~p: ~p~n~p",
[Module, Host, Opts, Class, Reason,
erlang:get_stacktrace()]),
?CRITICAL_MSG(ErrorText, []),

View File

@ -70,8 +70,9 @@ start(Host, Opts) ->
ejabberd_hooks:add(adhoc_local_commands, Host, ?MODULE, announce_commands, 50),
ejabberd_hooks:add(c2s_self_presence, Host,
?MODULE, send_motd, 50),
register(gen_mod:get_module_proc(Host, ?PROCNAME),
proc_lib:spawn(?MODULE, init, [])).
Pid = proc_lib:spawn(?MODULE, init, []),
register(gen_mod:get_module_proc(Host, ?PROCNAME), Pid),
{ok, Pid}.
depends(_Host, _Opts) ->
[{mod_adhoc, hard}].

View File

@ -90,13 +90,13 @@ find_session(SID) ->
start(Host, Opts) ->
start_jiffy(Opts),
Mod = gen_mod:ram_db_mod(global, ?MODULE),
Mod:init(),
TmpSup = gen_mod:get_module_proc(Host, ?PROCNAME),
TmpSupSpec = {TmpSup,
{ejabberd_tmp_sup, start_link, [TmpSup, ejabberd_bosh]},
permanent, infinity, supervisor, [ejabberd_tmp_sup]},
supervisor:start_child(ejabberd_sup, TmpSupSpec),
Mod = gen_mod:ram_db_mod(global, ?MODULE),
Mod:init().
supervisor:start_child(ejabberd_sup, TmpSupSpec).
stop(Host) ->
TmpSup = gen_mod:get_module_proc(Host, ?PROCNAME),

View File

@ -51,17 +51,15 @@
start(Host, Opts) ->
case mod_proxy65_service:add_listener(Host, Opts) of
{error, _} = Err -> erlang:error(Err);
_ ->
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
ChildSpec = {Proc, {?MODULE, start_link, [Host, Opts]},
transient, infinity, supervisor, [?MODULE]},
case supervisor:start_child(ejabberd_sup, ChildSpec) of
{error, _} = Err -> erlang:error(Err);
_ ->
Mod = gen_mod:ram_db_mod(global, ?MODULE),
Mod:init()
end
{error, _} = Err ->
Err;
_ ->
Mod = gen_mod:ram_db_mod(global, ?MODULE),
Mod:init(),
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
ChildSpec = {Proc, {?MODULE, start_link, [Host, Opts]},
transient, infinity, supervisor, [?MODULE]},
supervisor:start_child(ejabberd_sup, ChildSpec)
end.
stop(Host) ->

View File

@ -98,8 +98,9 @@ start(Host, Opts) ->
true ->
ok
end,
register(gen_mod:get_module_proc(Host, ?PROCNAME),
spawn(?MODULE, init, [MyHost, Host, Search])).
Pid = spawn(?MODULE, init, [MyHost, Host, Search]),
register(gen_mod:get_module_proc(Host, ?PROCNAME), Pid),
{ok, Pid}.
init(Host, ServerHost, Search) ->
case Search of