Skip to content

Commit

Permalink
Merge pull request #3625 from esl/options-only-in-maps
Browse files Browse the repository at this point in the history
Service and module options only in maps
  • Loading branch information
Premwoik authored Apr 8, 2022
2 parents 40d41ee + d539679 commit 5d9089b
Show file tree
Hide file tree
Showing 41 changed files with 229 additions and 292 deletions.
6 changes: 3 additions & 3 deletions big_tests/tests/dynamic_domains_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ init_per_group(with_mod_dynamic_domains_test, Config) ->
[ok = rpc(mim(), meck, new, [Module, [passthrough, no_link]])
|| Module <- MockedModules],
dynamic_modules:start(?HOST_TYPE, mod_dynamic_domains_test,
[{host1, subhost_pattern("subdomain1.@HOST@")},
{host2, subhost_pattern("subdomain2.@HOST@")},
{namespace, <<"dummy.namespace">>}]),
#{host1 => subhost_pattern("subdomain1.@HOST@"),
host2 => subhost_pattern("subdomain2.@HOST@"),
namespace => <<"dummy.namespace">>}),
[{reset_meck, MockedModules} | Config];
init_per_group(_, Config) ->
Config.
Expand Down
2 changes: 2 additions & 0 deletions big_tests/tests/service_mongoose_system_metrics_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ module_opts_are_reported(_Config) ->
check_module_backend(mod_bosh, mnesia),
check_module_backend(mod_event_pusher, push),
check_module_backend(mod_event_pusher_push, Backend),
check_module_backend(mod_http_upload, s3),
check_module_backend(mod_last, Backend),
check_module_backend(mod_muc, Backend),
check_module_backend(mod_muc_light, Backend),
Expand Down Expand Up @@ -341,6 +342,7 @@ modules_to_test(module_opts_are_reported) ->
[required_module(mod_bosh),
required_module(mod_event_pusher,
#{push => config([modules, mod_event_pusher, push], #{backend => Backend})}),
required_module(mod_http_upload, s3),
required_module(mod_last, Backend),
required_module(mod_muc, Backend),
required_module(mod_muc_light, Backend),
Expand Down
3 changes: 3 additions & 0 deletions doc/migrations/5.0.0_5.1.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ The archive functionality recently introduced has been extended to support many
## Removal of deprecated modules
* `mod_revproxy` - removed from the code base as it was unsupported since 4.2.0.
* `mod_aws_sns` - its functionality is fully covered by [`mod_event_pusher`](../modules/mod_event_pusher.md).

## Internal module configuration rework
If you are using your own extension modules (or services), you need to update the code. The most important change is that configuration options were stored in proplists before, and now they are stored in maps, so e.g. the `start/2` function of your module should expect a map as the second argument.
7 changes: 7 additions & 0 deletions doc/modules/mod_event_pusher_push.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ with 100 asynchronous workers that will handle all push notification related wor

## Options

### `modules.mod_event_pusher.push.iqdisc.type`
* **Syntax:** string, one of `"one_queue"`, `"no_queue"`, `"queues"`, `"parallel"`
* **Default:** `"one_queue"`

Strategy to handle incoming stanzas. For details, please refer to
[IQ processing policies](../configuration/Modules.md#iq-processing-policies).

### `modules.mod_event_pusher.push.backend`
* **Syntax:** string, one of `"mnesia"`, `"rdbms"`
* **Default:** `"mnesia"`
Expand Down
10 changes: 1 addition & 9 deletions src/admin_extra/service_admin_extra_vcard.erl
Original file line number Diff line number Diff line change
Expand Up @@ -170,18 +170,10 @@ set_vcard(User, Host, Name, Subname, SomeContent) ->
%%
%% Internal vcard

-spec get_module_resource(jid:server()) -> string().
get_module_resource(Server) ->
case gen_mod:get_module_opt(Server, ?MODULE, module_resource, none) of
none -> atom_to_list(?MODULE);
R when is_list(R) -> R
end.


-spec get_vcard_content(jid:jid(), any()) ->
{error, string()} | list(binary()).
get_vcard_content(#jid{lserver = LServer} = NoResJID, Data) ->
JID = jid:replace_resource(NoResJID, list_to_binary(get_module_resource(LServer))),
JID = jid:replace_resource(NoResJID, atom_to_binary(?MODULE)),
IQ = #iq{type = get, xmlns = ?NS_VCARD, sub_el = []},
{ok, HostType} = mongoose_domain_api:get_domain_host_type(LServer),
Acc = mongoose_acc:new(#{ location => ?LOCATION,
Expand Down
13 changes: 2 additions & 11 deletions src/config/mongoose_config_parser.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
%% state API
-export([build_state/3, get_opts/1]).

%% config post-processing
-export([unfold_opts/1]).

-callback parse_file(FileName :: string()) -> state().

-include("mongoose.hrl").
Expand Down Expand Up @@ -138,7 +135,7 @@ post_process_services(State = #state{opts = Opts}) ->

post_process_services_opt({services, Services}) ->
ServicesWithDeps = mongoose_service_deps:resolve_deps(Services),
{services, unfold_all_opts(ServicesWithDeps)};
{services, ServicesWithDeps};
post_process_services_opt(Other) ->
Other.

Expand All @@ -149,16 +146,10 @@ post_process_modules(State = #state{opts = Opts}) ->

post_process_modules_opt({{modules, HostType}, Modules}) ->
ModulesWithDeps = gen_mod_deps:resolve_deps(HostType, Modules),
{{modules, HostType}, unfold_all_opts(ModulesWithDeps)};
{{modules, HostType}, ModulesWithDeps};
post_process_modules_opt(Other) ->
Other.

unfold_all_opts(Modules) ->
maps:map(fun(_Mod, Opts) -> unfold_opts(Opts) end, Modules).

unfold_opts(Opts) when is_map(Opts) -> Opts;
unfold_opts(Opts) -> proplists:unfold(Opts).

%% local functions

-spec halt_with_msg(string(), [any()]) -> no_return().
Expand Down
9 changes: 2 additions & 7 deletions src/config/mongoose_config_spec.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
-export([process_root/1,
process_host/1,
process_general/1,
process_sm_backend/1,
process_ctl_access_rule/1,
process_listener/2,
process_verify_peer/1,
Expand Down Expand Up @@ -184,7 +183,6 @@ general() ->
wrap = global_config},
<<"sm_backend">> => #option{type = atom,
validate = {module, ejabberd_sm},
process = fun ?MODULE:process_sm_backend/1,
wrap = global_config},
<<"max_fsm_queue">> => #option{type = integer,
validate = positive,
Expand Down Expand Up @@ -222,7 +220,7 @@ general_defaults() ->
<<"registration_timeout">> => 600,
<<"language">> => <<"en">>,
<<"all_metrics_are_global">> => false,
<<"sm_backend">> => {mnesia, []},
<<"sm_backend">> => mnesia,
<<"rdbms_server_type">> => generic,
<<"mongooseimctl_access_commands">> => [],
<<"routing_modules">> => mongoose_router:default_routing_modules(),
Expand Down Expand Up @@ -795,7 +793,7 @@ modules() ->
|| Module <- configurable_modules()],
Items = maps:from_list(Modules),
#section{
items = Items#{default => #section{items = #{}}},
items = Items#{default => #section{items = #{}, format_items = map}},
validate_keys = module,
format_items = map,
wrap = host_config
Expand Down Expand Up @@ -1062,9 +1060,6 @@ process_ctl_access_rule(KVs) ->
ArgRestrictions = proplists:get_value(argument_restrictions, KVs, []),
{Commands, ArgRestrictions}.

process_sm_backend(Backend) ->
{Backend, []}.

process_host(Host) ->
Node = jid:nodeprep(Host),
true = Node =/= error,
Expand Down
18 changes: 9 additions & 9 deletions src/ejabberd_cowboy.erl
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ execute(Req, Env) ->
%%--------------------------------------------------------------------

start_cowboy(Ref, Opts) ->
{Retries, SleepTime} = gen_mod:get_opt(retries, Opts, {20, 50}),
{Retries, SleepTime} = proplists:get_value(retries, Opts, {20, 50}),
do_start_cowboy(Ref, Opts, Retries, SleepTime).


Expand All @@ -148,14 +148,14 @@ do_start_cowboy(Ref, Opts, Retries, SleepTime) ->
end.

do_start_cowboy(Ref, Opts) ->
SSLOpts = gen_mod:get_opt(ssl, Opts, undefined),
NumAcceptors = gen_mod:get_opt(num_acceptors, Opts, 100),
TransportOpts0 = gen_mod:get_opt(transport_options, Opts, #{}),
SSLOpts = proplists:get_value(ssl, Opts),
NumAcceptors = proplists:get_value(num_acceptors, Opts, 100),
TransportOpts0 = proplists:get_value(transport_options, Opts, #{}),
TransportOpts = TransportOpts0#{num_acceptors => NumAcceptors},
Modules = gen_mod:get_opt(modules, Opts),
Modules = proplists:get_value(modules, Opts),
Dispatch = cowboy_router:compile(get_routes(Modules)),
ProtocolOpts = [{env, [{dispatch, Dispatch}]} |
gen_mod:get_opt(protocol_options, Opts, [])],
proplists:get_value(protocol_options, Opts, [])],
ok = trails_store(Modules),
case catch start_http_or_https(SSLOpts, Ref, TransportOpts, ProtocolOpts) of
{error, {{shutdown,
Expand Down Expand Up @@ -194,7 +194,7 @@ add_common_middleware(Map) ->
Map#{ middlewares => [cowboy_router, ?MODULE, cowboy_handler] }.

reload_dispatch(Ref, Opts) ->
Dispatch = cowboy_router:compile(get_routes(gen_mod:get_opt(modules, Opts))),
Dispatch = cowboy_router:compile(get_routes(proplists:get_value(modules, Opts))),
cowboy:set_env(Ref, dispatch, Dispatch).

stop_cowboy(Ref) ->
Expand Down Expand Up @@ -267,7 +267,7 @@ filter_options(_, []) ->
[].

maybe_set_verify_fun(SSLOptions) ->
case proplists:get_value(verify_mode, SSLOptions, undefined) of
case proplists:get_value(verify_mode, SSLOptions) of
undefined ->
SSLOptions;
Mode ->
Expand All @@ -279,7 +279,7 @@ maybe_set_verify_fun(SSLOptions) ->
% used max_connections tuple for all ejabberd_cowboy listeners
maybe_insert_max_connections(TransportOpts, Opts) ->
Key = max_connections,
case gen_mod:get_opt(Key, Opts, undefined) of
case proplists:get_value(Key, Opts) of
undefined ->
TransportOpts;
Value ->
Expand Down
4 changes: 2 additions & 2 deletions src/ejabberd_sm.erl
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,8 @@ node_cleanup(Acc, Node) ->
%%--------------------------------------------------------------------
-spec init(_) -> {ok, state()}.
init([]) ->
{Backend, Opts} = mongoose_config:get_opt(sm_backend),
ejabberd_sm_backend:init([{backend, Backend}|Opts]),
Backend = mongoose_config:get_opt(sm_backend),
ejabberd_sm_backend:init(#{backend => Backend}),

ets:new(sm_iqtable, [named_table, protected, {read_concurrency, true}]),
ejabberd_hooks:add(node_cleanup, global, ?MODULE, node_cleanup, 50),
Expand Down
6 changes: 3 additions & 3 deletions src/ejabberd_sm_backend.erl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
%% Generic module to access SM backend modules
-module(ejabberd_sm_backend).

-callback init(list()) ->
-callback init(map()) ->
any().
-callback get_sessions() ->
[ejabberd_sm:session()].
Expand Down Expand Up @@ -29,7 +29,7 @@
-callback total_count() -> integer().
-callback unique_count() -> integer().

-export([init/1,
-export([init/1,
get_sessions/0, get_sessions/1, get_sessions/2, get_sessions/3,
create_session/4, update_session/4, delete_session/4, cleanup/1,
total_count/0, unique_count/0]).
Expand All @@ -38,7 +38,7 @@

-define(MAIN_MODULE, ejabberd_sm).

-spec init(list()) -> any().
-spec init(map()) -> any().
init(Opts) ->
Args = [Opts],
mongoose_backend:init(global, ?MAIN_MODULE, [], Opts),
Expand Down
2 changes: 1 addition & 1 deletion src/ejabberd_sm_mnesia.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
total_count/0,
unique_count/0]).

-spec init(list()) -> any().
-spec init(map()) -> any().
init(_Opts) ->
mnesia:create_table(session,
[{ram_copies, [node()]},
Expand Down
2 changes: 1 addition & 1 deletion src/ejabberd_sm_redis.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

-ignore_xref([maybe_initial_cleanup/2]).

-spec init(list()) -> any().
-spec init(map()) -> any().
init(_Opts) ->
%% Clean current node's sessions from previous life
{Elapsed, RetVal} = timer:tc(?MODULE, maybe_initial_cleanup, [node(), true]),
Expand Down
9 changes: 5 additions & 4 deletions src/event_pusher/mod_event_pusher_push.erl
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ start(HostType, Opts) ->
start_pool(HostType, #{wpool := WpoolOpts}) ->
{ok, _} = mongoose_wpool:start(generic, HostType, pusher_push, maps:to_list(WpoolOpts)).

init_iq_handlers(HostType, Opts) ->
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
init_iq_handlers(HostType, #{iqdisc := IQDisc}) ->
gen_iq_handler:add_iq_handler(ejabberd_local, HostType, ?NS_PUSH, ?MODULE,
iq_handler, IQDisc),
gen_iq_handler:add_iq_handler(ejabberd_sm, HostType, ?NS_PUSH, ?MODULE,
Expand All @@ -96,11 +95,13 @@ config_spec() ->
VirtPubSubHost = #option{type = string, validate = subdomain_template,
process = fun mongoose_subdomain_utils:make_subdomain_pattern/1},
#section{
items = #{<<"backend">> => #option{type = atom, validate = {module, ?MODULE}},
items = #{<<"iqdisc">> => mongoose_config_spec:iqdisc(),
<<"backend">> => #option{type = atom, validate = {module, ?MODULE}},
<<"wpool">> => wpool_spec(),
<<"plugin_module">> => #option{type = atom, validate = module},
<<"virtual_pubsub_hosts">> => #list{items = VirtPubSubHost}},
defaults = #{<<"backend">> => mnesia,
defaults = #{<<"iqdisc">> => one_queue,
<<"backend">> => mnesia,
<<"plugin_module">> => mod_event_pusher_push_plugin:default_plugin_module(),
<<"virtual_pubsub_hosts">> => []},
format_items = map
Expand Down
Loading

0 comments on commit 5d9089b

Please sign in to comment.