I'm writing an ejabberd module and need to perform different cleanup actions on stop in the two following cases:
I consider the following solution:
is_there_running_instances() ->
{Results, _} = rpc:multicall(nodes(), ?MODULE, ejabberd_loaded_modules, []),
lists:member(?MODULE, lists:append(Results)).
ejabberd_loaded_modules() -> ???.
But don't know how to get the list of loaded modules. How can I do it? Or is my problem has a better solution?
code:all_loaded/0
returns a list of tuple of the type {ModuleName,ModuleAbsolutePath}
where ModuleName is an atom and ModuleAbsolutePath a string. You can filter this list with the path information, and unzip it to get only the module it contains. I test it with the eunit library after loading all modules:
26> Filter = fun(Path,Lib) -> string:str(Path,Lib) > 0 end.
#Fun<erl_eval.12.80484245>
27> [Mod || {Mod,Path} <- code:all_loaded() , is_list(Path), Filter(Path,"eunit")].
[eunit,eunit_proc,eunit_test,eunit_data,eunit_surefire,
eunit_serial,eunit_tests,eunit_autoexport,eunit_tty,
eunit_server,eunit_striptests,eunit_lib,eunit_listener]
try if
gen_mod:loaded_modules(Host)
is what you need
First of all get all loaded modules list with code:all_loaded/0 and than filter result with "mod_" preffix.
ejabberd_loaded_modules() ->
AllLoaded = code:all_loaded(),
Filter = fun({Module, _}) ->
case re:run(atom_to_list(Module), "mod_") of
{match,[{0,4}]} -> true;
_ -> false
end
end,
lists:filter(Filter, AllLoaded).
Also you can perform behaviour check. It should be gen_mod.
is_gen_mod(Module) ->
BehavioursList =
case lists:getkey(1, behaviour, Module:module_info(attributes)) of
{behaviour, L} -> L;
false -> []
end,
lists:member(gen_mod, BehavioursList).
In this case ejabberd_loaded_modules/0 function will be:
ejabberd_loaded_modules() ->
...
ModPreffixed = lists:filter(Filter, AllLoaded),
lists:filter(fun is_gen_mod/1, ModPreffixed).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.