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 27, 2023
1 parent 3e8bcca commit 342e9b9
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 24 deletions.
16 changes: 9 additions & 7 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,11 +66,12 @@

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

-ifdef(TEST).
-spec init() -> ok | {error, term()}.
init() ->
mongoose_domain_core:start(),
mongoose_subdomain_core:start(),
mongoose_lazy_routing:start().
mongoose_domain_core:start_link(),
mongoose_subdomain_core:start_link(),
mongoose_lazy_routing:start_link().

%% Stops gen_servers, that are started from init/0
%% Does not fail, even if servers are already stopped
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
2 changes: 1 addition & 1 deletion test/mongoose_cleanup_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ 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(),
spawn(fun() -> mongoose_domain_api:init() end),
Config.

end_per_suite(Config) ->
Expand Down

0 comments on commit 342e9b9

Please sign in to comment.