./ct_report/coverage/mongoose_collector.COVER.html

1 -module(mongoose_collector).
2
3 -include("mongoose_logger.hrl").
4
5 %% gen_server callbacks
6 -behaviour(gen_server).
7 -export([start_common/3, stop_common/2]).
8 -export([start_link/2, init/1, handle_call/3, handle_cast/2, handle_info/2]).
9
10 -ignore_xref([start_link/2]).
11
12 -record(watchdog, {
13 host_type :: mongooseim:host_type(),
14 action :: fun((mongooseim:host_type(), map()) -> term()),
15 opts :: term(),
16 interval :: pos_integer(), %% milliseconds
17 timer_ref :: undefined | reference()
18 }).
19
20 -spec start_common(atom(), mongooseim:host_type(), map()) -> term().
21 start_common(Module, HostType, WOpts) ->
22 59 Name = gen_mod:get_module_proc(HostType, Module),
23 59 MFA = {mongoose_collector, start_link, [Name, WOpts]},
24 59 ChildSpec = {Name, MFA, permanent, 5000, worker, [Module, mongoose_collector]},
25 59 ejabberd_sup:start_child(ChildSpec).
26
27 stop_common(Module, HostType) ->
28 487 Name = gen_mod:get_module_proc(HostType, Module),
29 487 ejabberd_sup:stop_child(Name).
30
31 start_link(Name, Opts) ->
32 59 gen_server:start_link({local, Name}, ?MODULE, Opts, []).
33
34 init(#{host_type := HostType,
35 action := Fun,
36 opts := Opts,
37 interval := Interval}) when is_function(Fun, 2) ->
38 59 State = #watchdog{host_type = HostType,
39 action = Fun,
40 opts = Opts,
41 interval = Interval,
42 timer_ref = undefined},
43 59 {ok, schedule_check(State)}.
44
45 handle_call(Msg, From, State) ->
46
:-(
?UNEXPECTED_CALL(Msg, From),
47
:-(
{reply, ok, State}.
48
49 handle_cast(Msg, State) ->
50
:-(
?UNEXPECTED_CAST(Msg),
51
:-(
{noreply, State}.
52
53 handle_info({timeout, Ref, run_action},
54 #watchdog{timer_ref = Ref} = State) ->
55 17 run_action(State),
56 17 {noreply, schedule_check(State)};
57 handle_info(Info, State) ->
58
:-(
?UNEXPECTED_INFO(Info),
59
:-(
{noreply, State}.
60
61 schedule_check(State = #watchdog{interval = Interval}) ->
62 76 State#watchdog{timer_ref = erlang:start_timer(Interval, self(), run_action)}.
63
64 run_action(#watchdog{host_type = HostType, action = Fun, opts = Opts}) ->
65 17 Fun(HostType, Opts).
Line Hits Source