./ct_report/coverage/mongoose_cleaner.COVER.html

1 -module(mongoose_cleaner).
2
3 -behaviour(gen_server).
4
5 %% API
6 -export([start_link/0]).
7
8 %% gen_server callbacks
9 -export([init/1,
10 handle_call/3,
11 handle_cast/2,
12 handle_info/2,
13 terminate/2,
14 code_change/3]).
15
16 -ignore_xref([start_link/0]).
17
18 -include("mongoose.hrl").
19
20 -define(NODE_CLEANUP_LOCK(Node), {node_cleanup_lock, Node}).
21 -define(SERVER, ?MODULE).
22
23 -record(state, {}).
24
25 %%%===================================================================
26 %%% API
27 %%%===================================================================
28
29 start_link() ->
30 53 gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
31
32 %%%===================================================================
33 %%% gen_server callbacks
34 %%%===================================================================
35
36 init([]) ->
37 53 case net_kernel:monitor_nodes(true) of
38 ok ->
39 53 {ok, #state{}};
40 Error ->
41
:-(
?LOG_ERROR(#{what => cleaner_monitor_failed,
42 text => <<"mongoose_cleaner failed to monitor nodes">>,
43
:-(
reason => Error}),
44
:-(
{stop, Error}
45 end.
46
47 handle_call(_Request, _From, State) ->
48
:-(
Reply = ok,
49
:-(
{reply, Reply, State}.
50
51 handle_cast(_Msg, State) ->
52
:-(
{noreply, State}.
53
54 handle_info({nodedown, Node}, State) ->
55
:-(
?LOG_WARNING(#{what => cleaner_nodedown,
56 text => <<"mongoose_cleaner received nodenown event">>,
57
:-(
down_node => Node}),
58
:-(
cleanup_modules(Node),
59
:-(
{noreply, State};
60 handle_info(_Info, State) ->
61 6 {noreply, State}.
62
63 terminate(_Reason, _State) ->
64
:-(
ok.
65
66 code_change(_OldVsn, State, _Extra) ->
67
:-(
{ok, State}.
68
69 %%%===================================================================
70 %%% Internal functions
71 %%%===================================================================
72
73 cleanup_modules(Node) ->
74
:-(
LockKey = ?NODE_CLEANUP_LOCK(Node),
75
:-(
LockRequest = {LockKey, self()},
76
:-(
C = fun () -> run_node_cleanup(Node) end,
77
:-(
Nodes = [node() | nodes()],
78
:-(
Retries = 10,
79
:-(
case global:trans(LockRequest, C, Nodes, Retries) of
80 aborted ->
81
:-(
?LOG_WARNING(#{what => cleaner_trans_aborted,
82 text => <<"mongoose_cleaner failed to get the global lock, run cleanup anyway">>,
83
:-(
remote_node => Node, lock_key => LockKey, retries => Retries}),
84
:-(
C();
85 Result ->
86
:-(
Result
87 end.
88
89 run_node_cleanup(Node) ->
90
:-(
{Elapsed, RetVal} = timer:tc(fun() ->
91
:-(
mongoose_hooks:node_cleanup(Node),
92
:-(
[mongoose_hooks:node_cleanup_for_host_type(HostType, Node) || HostType <- ?ALL_HOST_TYPES],
93
:-(
ok
94 end),
95
:-(
?LOG_NOTICE(#{what => cleaner_done,
96 text => <<"Finished cleaning after dead node">>,
97 duration => erlang:round(Elapsed / 1000),
98
:-(
down_node => Node, result => RetVal}),
99
:-(
RetVal.
Line Hits Source