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 |
104 |
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []). |
31 |
|
|
32 |
|
%%%=================================================================== |
33 |
|
%%% gen_server callbacks |
34 |
|
%%%=================================================================== |
35 |
|
|
36 |
|
init([]) -> |
37 |
104 |
case net_kernel:monitor_nodes(true) of |
38 |
|
ok -> |
39 |
104 |
{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 |
8 |
?LOG_WARNING(#{what => cleaner_nodedown, |
56 |
|
text => <<"mongoose_cleaner received nodenown event">>, |
57 |
:-( |
down_node => Node}), |
58 |
8 |
cleanup_modules(Node), |
59 |
8 |
{noreply, State}; |
60 |
|
handle_info(_Info, State) -> |
61 |
14 |
{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 |
8 |
LockKey = ?NODE_CLEANUP_LOCK(Node), |
75 |
8 |
LockRequest = {LockKey, self()}, |
76 |
8 |
C = fun () -> run_node_cleanup(Node) end, |
77 |
8 |
Nodes = [node() | nodes()], |
78 |
8 |
Retries = 1, |
79 |
8 |
case global:trans(LockRequest, C, Nodes, Retries) of |
80 |
|
aborted -> |
81 |
:-( |
?LOG_INFO(#{what => cleaner_trans_aborted, |
82 |
|
text => <<"mongoose_cleaner failed to get global lock">>, |
83 |
:-( |
lock_key => LockKey}), |
84 |
:-( |
{ok, aborted}; |
85 |
|
Result -> |
86 |
8 |
{ok, Result} |
87 |
|
end. |
88 |
|
|
89 |
|
run_node_cleanup(Node) -> |
90 |
8 |
{Elapsed, RetVal} = timer:tc(fun() -> |
91 |
8 |
mongoose_hooks:node_cleanup(Node), |
92 |
8 |
[mongoose_hooks:node_cleanup_for_host_type(HostType, Node) || HostType <- ?ALL_HOST_TYPES], |
93 |
8 |
ok |
94 |
|
end), |
95 |
8 |
?LOG_NOTICE(#{what => cleaner_done, |
96 |
|
text => <<"Finished cleaning after dead node">>, |
97 |
|
duration => erlang:round(Elapsed / 1000), |
98 |
8 |
down_node => Node, result => RetVal}), |
99 |
8 |
RetVal. |