-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathrabbit_vhost_process.erl
88 lines (71 loc) · 2.98 KB
/
rabbit_vhost_process.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
%% This Source Code Form is subject to the terms of the Mozilla Public
%% License, v. 2.0. If a copy of the MPL was not distributed with this
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
%%
%% Copyright (c) 2007-2024 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
%%
%% This module implements a vhost identity process.
%% On start this process will try to recover the vhost data and
%% processes structure (queues and message stores).
%% If recovered successfully, the process will save it's PID
%% to vhost process registry. If vhost process PID is in the registry and the
%% process is alive - the vhost is considered running.
%% On termination, the ptocess will notify of vhost going down.
%% The process will also check periodically if the vhost is still
%% present in the database and stop the vhost supervision tree when it
%% disappears.
-module(rabbit_vhost_process).
-include_lib("rabbit_common/include/rabbit.hrl").
-define(VHOST_CHECK_INTERVAL, 5000).
-behaviour(gen_server2).
-export([start_link/1]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
code_change/3]).
start_link(VHost) ->
gen_server2:start_link(?MODULE, [VHost], []).
init([VHost]) ->
process_flag(trap_exit, true),
rabbit_log:debug("Recovering data for VHost ~ts", [VHost]),
try
%% Recover the vhost data and save it to vhost registry.
ok = rabbit_vhost:recover(VHost),
rabbit_vhost_sup_sup:save_vhost_process(VHost, self()),
_ = timer:send_interval(?VHOST_CHECK_INTERVAL, check_vhost),
true = erlang:garbage_collect(),
{ok, VHost}
catch _:Reason:Stacktrace ->
rabbit_amqqueue:mark_local_durable_queues_stopped(VHost),
rabbit_log:error("Unable to recover vhost ~tp data. Reason ~tp~n"
" Stacktrace ~tp",
[VHost, Reason, Stacktrace]),
{stop, Reason}
end.
handle_call(_,_,VHost) ->
{reply, ok, VHost}.
handle_cast(_, VHost) ->
{noreply, VHost}.
handle_info(check_vhost, VHost) ->
case rabbit_vhost:exists(VHost) of
true -> {noreply, VHost};
false ->
rabbit_log:warning("Virtual host '~ts' is gone. "
"Stopping its top level supervisor.",
[VHost]),
%% Stop vhost's top supervisor in a one-off process to avoid a deadlock:
%% us (a child process) waiting for supervisor shutdown and our supervisor(s)
%% waiting for us to shutdown.
spawn(
fun() ->
rabbit_vhost_sup_sup:stop_and_delete_vhost(VHost)
end),
{noreply, VHost}
end;
handle_info(_, VHost) ->
{noreply, VHost}.
terminate(shutdown, VHost) ->
%% Notify that vhost is stopped.
rabbit_vhost:vhost_down(VHost);
terminate(_, _VHost) ->
ok.
code_change(_OldVsn, VHost, _Extra) ->
{ok, VHost}.