./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 cets_dist_blocker:add_cleaner(self()),
38 53 case net_kernel:monitor_nodes(true) of
39 ok ->
40 53 {ok, #state{}};
41 Error ->
42
:-(
?LOG_ERROR(#{what => cleaner_monitor_failed,
43 text => <<"mongoose_cleaner failed to monitor nodes">>,
44
:-(
reason => Error}),
45
:-(
{stop, Error}
46 end.
47
48 handle_call(_Request, _From, State) ->
49
:-(
Reply = ok,
50
:-(
{reply, Reply, State}.
51
52 handle_cast(_Msg, State) ->
53
:-(
{noreply, State}.
54
55 handle_info({nodedown, Node}, State) ->
56
:-(
?LOG_WARNING(#{what => cleaner_nodedown,
57 text => <<"mongoose_cleaner received nodenown event">>,
58
:-(
down_node => Node}),
59
:-(
cleanup_modules(Node),
60
:-(
cets_dist_blocker:cleaning_done(self(), Node),
61
:-(
{noreply, State};
62 handle_info(_Info, State) ->
63 6 {noreply, State}.
64
65 terminate(_Reason, _State) ->
66
:-(
ok.
67
68 code_change(_OldVsn, State, _Extra) ->
69
:-(
{ok, State}.
70
71 %%%===================================================================
72 %%% Internal functions
73 %%%===================================================================
74
75 cleanup_modules(Node) ->
76
:-(
LockKey = ?NODE_CLEANUP_LOCK(Node),
77
:-(
LockRequest = {LockKey, self()},
78
:-(
C = fun () -> run_node_cleanup(Node) end,
79
:-(
Nodes = [node() | nodes()],
80
:-(
Retries = 10,
81
:-(
case global:trans(LockRequest, C, Nodes, Retries) of
82 aborted ->
83
:-(
?LOG_WARNING(#{what => cleaner_trans_aborted,
84 text => <<"mongoose_cleaner failed to get the global lock, run cleanup anyway">>,
85
:-(
remote_node => Node, lock_key => LockKey, retries => Retries}),
86
:-(
C();
87 Result ->
88
:-(
Result
89 end.
90
91 run_node_cleanup(Node) ->
92
:-(
{Elapsed, RetVal} = timer:tc(fun() ->
93
:-(
mongoose_hooks:node_cleanup(Node),
94
:-(
[mongoose_hooks:node_cleanup_for_host_type(HostType, Node) || HostType <- ?ALL_HOST_TYPES],
95
:-(
ok
96 end),
97
:-(
?LOG_NOTICE(#{what => cleaner_done,
98 text => <<"Finished cleaning after dead node">>,
99 duration => erlang:round(Elapsed / 1000),
100
:-(
down_node => Node, result => RetVal}),
101
:-(
RetVal.
Line Hits Source