Skip to content

Commit

Permalink
Add a supervisor for domain workers
Browse files Browse the repository at this point in the history
Follow cleaner OTP supervision conventions,
we want these workers supervised.
  • Loading branch information
NelsonVides committed Jul 28, 2023
1 parent 3e8bcca commit f4055ee
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 22 deletions.
2 changes: 1 addition & 1 deletion doc/developers-guide/domain_management.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ It provides the following interfaces:
Any of these lists can be empty, initial list of domain/host\_type pairs can
have some unique host\_types not mentioned in the host\_types list.
The component is initialised by the main MIM supervisor.
Implemented in `mongoose_domain_api:init()`.
Implemented in `mongoose_domain_sup:start_link/0`.
- Insert - adding new domain/host\_type pair.
This function is idempotent. It returns success on an attempt to insert the existing data,
but fails if ETS already has the domain name associated with another host type.
Expand Down
10 changes: 6 additions & 4 deletions src/domain/mongoose_domain_api.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

-include("mongoose_logger.hrl").

-export([init/0,
stop/0,
get_host_type/1]).
-ifdef(TEST).
-export([init/0, stop/0]).
-endif.
-export([get_host_type/1]).

%% external domain API for GraphQL or REST handlers
-export([insert_domain/2,
Expand Down Expand Up @@ -45,7 +46,6 @@

-ignore_xref([get_all_static/0]).
-ignore_xref([get_all_dynamic/0]).
-ignore_xref([stop/0]).

-type status() :: enabled | disabled | deleting.
-type domain() :: jid:lserver().
Expand All @@ -66,6 +66,7 @@

-export_type([status/0, remove_domain_acc/0]).

-ifdef(TEST).
-spec init() -> ok | {error, term()}.
init() ->
mongoose_domain_core:start(),
Expand All @@ -80,6 +81,7 @@ stop() ->
catch mongoose_subdomain_core:stop(),
catch mongoose_lazy_routing:stop(),
ok.
-endif.

-spec insert_domain(domain(), host_type()) -> insert_result().
insert_domain(Domain, HostType) ->
Expand Down
21 changes: 15 additions & 6 deletions src/domain/mongoose_domain_core.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
-include_lib("stdlib/include/ms_transform.hrl").

-export([start/0, start/2, stop/0]).
-export([start_link/2]).
-export([start_link/0, start_link/2]).
-export([get_host_type/1]).
-export([is_static/1]).

Expand All @@ -34,7 +34,7 @@
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).

-ignore_xref([get_start_args/0, start_link/2, start/2, stop/0]).
-ignore_xref([get_start_args/0, start_link/0, start_link/2, start/0, start/2, stop/0]).

-define(TABLE, ?MODULE).
-define(HOST_TYPE_TABLE, mongoose_domain_core_host_types).
Expand All @@ -46,7 +46,11 @@
start() ->
Pairs = get_static_pairs(),
AllowedHostTypes = mongoose_config:get_opt(host_types),
start(Pairs, AllowedHostTypes).
ChildSpec =
{?MODULE,
{?MODULE, start_link, [Pairs, AllowedHostTypes]},
permanent, infinity, worker, [?MODULE]},
just_ok(supervisor:start_child(mongoose_domain_sup, ChildSpec)).

-ifdef(TEST).

Expand All @@ -64,16 +68,21 @@ start(Pairs, AllowedHostTypes) ->
{?MODULE,
{?MODULE, start_link, [Pairs, AllowedHostTypes]},
permanent, infinity, worker, [?MODULE]},
just_ok(supervisor:start_child(ejabberd_sup, ChildSpec)).
just_ok(supervisor:start_child(mongoose_domain_sup, ChildSpec)).

%% required for integration tests
stop() ->
supervisor:terminate_child(ejabberd_sup, ?MODULE),
supervisor:delete_child(ejabberd_sup, ?MODULE),
supervisor:terminate_child(mongoose_domain_sup, ?MODULE),
supervisor:delete_child(mongoose_domain_sup, ?MODULE),
ok.

-endif.

start_link() ->
Pairs = get_static_pairs(),
AllowedHostTypes = mongoose_config:get_opt(host_types),
start_link(Pairs, AllowedHostTypes).

start_link(Pairs, AllowedHostTypes) ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [Pairs, AllowedHostTypes], []).

Expand Down
19 changes: 19 additions & 0 deletions src/domain/mongoose_domain_sup.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-module(mongoose_domain_sup).

-behaviour(supervisor).

-export([start_link/0, init/1]).
-ignore_xref([start_link/0]).

start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).

init([]) ->
DomainCore = worker_spec(mongoose_domain_core),
SubdomainCore = worker_spec(mongoose_subdomain_core),
LazyRouting = worker_spec(mongoose_lazy_routing),
{ok, {{one_for_one, 10, 1},
[DomainCore, SubdomainCore, LazyRouting]}}.

worker_spec(Mod) ->
{Mod, {Mod, start_link, []}, permanent, timer:seconds(5), worker, [Mod]}.
8 changes: 4 additions & 4 deletions src/domain/mongoose_lazy_routing.erl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
terminate/2,
code_change/3]).

-ignore_xref([start_link/0, stop/0, sync/0]).
-ignore_xref([start_link/0, start/0, stop/0, sync/0]).

-define(IQ_TABLE, mongoose_lazy_routing_iqs).
-define(ROUTING_TABLE, mongoose_lazy_routing).
Expand Down Expand Up @@ -80,12 +80,12 @@ stop() ->
start() ->
ChildSpec = {?MODULE, {?MODULE, start_link, []},
permanent, infinity, worker, [?MODULE]},
just_ok(supervisor:start_child(ejabberd_sup, ChildSpec)).
just_ok(supervisor:start_child(mongoose_domain_sup, ChildSpec)).

Check warning on line 83 in src/domain/mongoose_lazy_routing.erl

View check run for this annotation

Codecov / codecov/patch

src/domain/mongoose_lazy_routing.erl#L83

Added line #L83 was not covered by tests

%% required for integration tests
stop() ->
supervisor:terminate_child(ejabberd_sup, ?MODULE),
supervisor:delete_child(ejabberd_sup, ?MODULE),
supervisor:terminate_child(mongoose_domain_sup, ?MODULE),
supervisor:delete_child(mongoose_domain_sup, ?MODULE),

Check warning on line 88 in src/domain/mongoose_lazy_routing.erl

View check run for this annotation

Codecov / codecov/patch

src/domain/mongoose_lazy_routing.erl#L87-L88

Added lines #L87 - L88 were not covered by tests
ok.

-endif.
Expand Down
8 changes: 4 additions & 4 deletions src/domain/mongoose_subdomain_core.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
code_change/3,
terminate/2]).

-ignore_xref([start_link/0, stop/0, sync/0]).
-ignore_xref([start_link/0, start/0, stop/0, sync/0]).

-ifdef(TEST).

Expand Down Expand Up @@ -85,12 +85,12 @@ log_error(_Function, _Error) -> ok.
start() ->
ChildSpec = {?MODULE, {?MODULE, start_link, []},
permanent, infinity, worker, [?MODULE]},
just_ok(supervisor:start_child(ejabberd_sup, ChildSpec)).
just_ok(supervisor:start_child(mongoose_domain_sup, ChildSpec)).

Check warning on line 88 in src/domain/mongoose_subdomain_core.erl

View check run for this annotation

Codecov / codecov/patch

src/domain/mongoose_subdomain_core.erl#L88

Added line #L88 was not covered by tests

%% required for integration tests
stop() ->
supervisor:terminate_child(ejabberd_sup, ?MODULE),
supervisor:delete_child(ejabberd_sup, ?MODULE),
supervisor:terminate_child(mongoose_domain_sup, ?MODULE),
supervisor:delete_child(mongoose_domain_sup, ?MODULE),

Check warning on line 93 in src/domain/mongoose_subdomain_core.erl

View check run for this annotation

Codecov / codecov/patch

src/domain/mongoose_subdomain_core.erl#L92-L93

Added lines #L92 - L93 were not covered by tests
ok.

-endif.
Expand Down
1 change: 0 additions & 1 deletion src/ejabberd_app.erl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ start(normal, _Args) ->
mongoose_logs:set_global_loglevel(mongoose_config:get_opt(loglevel)),
mongoose_deprecations:start(),
{ok, _} = Sup = ejabberd_sup:start_link(),
mongoose_domain_api:init(),
mongoose_wpool:ensure_started(),
mongoose_wpool:start_configured_pools(),
%% ejabberd_sm is started separately because it may use one of the outgoing_pools
Expand Down
7 changes: 6 additions & 1 deletion src/ejabberd_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ init([]) ->
{mongoose_shaper_sup,
{mongoose_shaper_sup, start_link, []},
permanent, infinity, supervisor, [mongoose_shaper_sup]},
DomainSup =
{mongoose_domain_sup,
{mongoose_domain_sup, start_link, []},
permanent, infinity, supervisor, [mongoose_domain_sup]},
PG =
{pg,
{pg, start_link, [mim_scope]},
Expand All @@ -170,7 +174,8 @@ init([]) ->
IQSupervisor,
Listener,
MucIQ,
ShaperSup]}}.
ShaperSup,
DomainSup]}}.

start_child(ChildSpec) ->
case supervisor:start_child(ejabberd_sup, ChildSpec) of
Expand Down
8 changes: 7 additions & 1 deletion test/mongoose_cleanup_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ init_per_suite(Config) ->
ok = mnesia:create_schema([node()]),
ok = mnesia:start(),
[mongoose_config:set_opt(Key, Value) || {Key, Value} <- opts()],
mongoose_domain_api:init(),
Self = self(),
spawn(fun() ->
supervisor:start_link({local, mongoose_domain_sup}, mongoose_domain_sup, []),
Self ! ready,
receive stop -> ok end
end),
receive ready -> ok after timer:seconds(30) -> ct:fail(mongoose_domain_sup_not_ready) end,
Config.

end_per_suite(Config) ->
Expand Down

0 comments on commit f4055ee

Please sign in to comment.