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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
%% The contents of this file are subject to the Mozilla Public License
%% Version 1.1 (the "License"); you may not use this file except in
%% compliance with the License. You may obtain a copy of the License at
%% http://www.mozilla.org/MPL/
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
%% License for the specific language governing rights and limitations
%% under the License.
%%
%% The Original Code is RabbitMQ.
%%
%% The Initial Developers of the Original Code are LShift Ltd,
%% Cohesive Financial Technologies LLC, and Rabbit Technologies Ltd.
%%
%% Portions created before 22-Nov-2008 00:00:00 GMT by LShift Ltd,
%% Cohesive Financial Technologies LLC, or Rabbit Technologies Ltd
%% are Copyright (C) 2007-2008 LShift Ltd, Cohesive Financial
%% Technologies LLC, and Rabbit Technologies Ltd.
%%
%% Portions created by LShift Ltd are Copyright (C) 2007-2009 LShift
%% Ltd. Portions created by Cohesive Financial Technologies LLC are
%% Copyright (C) 2007-2009 Cohesive Financial Technologies
%% LLC. Portions created by Rabbit Technologies Ltd are Copyright
%% (C) 2007-2009 Rabbit Technologies Ltd.
%%
%% All Rights Reserved.
%%
%% Contributor(s): ______________________________________.
%%
-module(rabbit).
-behaviour(application).
-export([prepare/0, start/0, stop/0, stop_and_halt/0, status/0, rotate_logs/1]).
-export([start/2, stop/1]).
-export([log_location/1]).
-import(application).
-import(mnesia).
-import(lists).
-import(inet).
-import(gen_tcp).
-include("rabbit_framing.hrl").
-include("rabbit.hrl").
-define(APPS, [os_mon, mnesia, rabbit]).
%%----------------------------------------------------------------------------
-ifdef(use_specs).
-type(log_location() :: 'tty' | 'undefined' | string()).
-type(file_suffix() :: binary()).
-spec(prepare/0 :: () -> 'ok').
-spec(start/0 :: () -> 'ok').
-spec(stop/0 :: () -> 'ok').
-spec(stop_and_halt/0 :: () -> 'ok').
-spec(rotate_logs/1 :: (file_suffix()) -> 'ok' | {'error', any()}).
-spec(status/0 :: () ->
[{running_applications, [{atom(), string(), string()}]} |
{nodes, [erlang_node()]} |
{running_nodes, [erlang_node()]}]).
-spec(log_location/1 :: ('sasl' | 'kernel') -> log_location()).
-endif.
%%----------------------------------------------------------------------------
prepare() ->
ok = ensure_working_log_handlers(),
ok = rabbit_mnesia:ensure_mnesia_dir().
start() ->
try
ok = prepare(),
ok = rabbit_misc:start_applications(?APPS)
after
%%give the error loggers some time to catch up
timer:sleep(100)
end.
stop() ->
ok = rabbit_misc:stop_applications(?APPS).
stop_and_halt() ->
spawn(fun () ->
SleepTime = 1000,
rabbit_log:info("Stop-and-halt request received; "
"halting in ~p milliseconds~n",
[SleepTime]),
timer:sleep(SleepTime),
init:stop()
end),
case catch stop() of _ -> ok end.
status() ->
[{running_applications, application:which_applications()}] ++
rabbit_mnesia:status().
rotate_logs(BinarySuffix) ->
Suffix = binary_to_list(BinarySuffix),
log_rotation_result(rotate_logs(log_location(kernel),
Suffix,
rabbit_error_logger_file_h),
rotate_logs(log_location(sasl),
Suffix,
rabbit_sasl_report_file_h)).
%%--------------------------------------------------------------------
start(normal, []) ->
{ok, SupPid} = rabbit_sup:start_link(),
print_banner(),
lists:foreach(
fun ({Msg, Thunk}) ->
io:format("starting ~-20s ...", [Msg]),
Thunk(),
io:format("done~n");
({Msg, M, F, A}) ->
io:format("starting ~-20s ...", [Msg]),
apply(M, F, A),
io:format("done~n")
end,
[{"database",
fun () -> ok = rabbit_mnesia:init() end},
{"core processes",
fun () ->
ok = start_child(rabbit_log),
ok = rabbit_hooks:start(),
ok = rabbit_binary_generator:
check_empty_content_body_frame_size(),
ok = rabbit_alarm:start(),
{ok, MemoryWatermark} =
application:get_env(vm_memory_high_watermark),
ok = case MemoryWatermark == 0 of
true ->
ok;
false ->
start_child(vm_memory_monitor, [MemoryWatermark])
end,
ok = rabbit_amqqueue:start(),
ok = start_child(rabbit_router),
ok = start_child(rabbit_node_monitor)
end},
{"recovery",
fun () ->
ok = maybe_insert_default_data(),
ok = rabbit_exchange:recover(),
ok = rabbit_amqqueue:recover()
end},
{"persister",
fun () ->
ok = start_child(rabbit_persister)
end},
{"guid generator",
fun () ->
ok = start_child(rabbit_guid)
end},
{"builtin applications",
fun () ->
{ok, DefaultVHost} = application:get_env(default_vhost),
ok = error_logger:add_report_handler(
rabbit_error_logger, [DefaultVHost]),
ok = start_builtin_amq_applications()
end},
{"TCP listeners",
fun () ->
ok = rabbit_networking:start(),
{ok, TcpListeners} = application:get_env(tcp_listeners),
lists:foreach(
fun ({Host, Port}) ->
ok = rabbit_networking:start_tcp_listener(Host, Port)
end,
TcpListeners)
end},
{"SSL listeners",
fun () ->
case application:get_env(ssl_listeners) of
{ok, []} ->
ok;
{ok, SslListeners} ->
ok = rabbit_misc:start_applications([crypto, ssl]),
{ok, SslOpts} = application:get_env(ssl_options),
[rabbit_networking:start_ssl_listener
(Host, Port, SslOpts) || {Host, Port} <- SslListeners],
ok
end
end}]),
io:format("~nbroker running~n"),
{ok, SupPid}.
stop(_State) ->
terminated_ok = error_logger:delete_report_handler(rabbit_error_logger),
ok = rabbit_alarm:stop(),
ok = case rabbit_mnesia:is_clustered() of
true -> rabbit_amqqueue:on_node_down(node());
false -> rabbit_mnesia:empty_ram_only_tables()
end,
ok.
%---------------------------------------------------------------------------
log_location(Type) ->
case application:get_env(Type, case Type of
kernel -> error_logger;
sasl -> sasl_error_logger
end) of
{ok, {file, File}} -> File;
{ok, false} -> undefined;
{ok, tty} -> tty;
{ok, silent} -> undefined;
{ok, Bad} -> throw({error, {cannot_log_to_file, Bad}});
_ -> undefined
end.
app_location() ->
{ok, Application} = application:get_application(),
filename:absname(code:where_is_file(atom_to_list(Application) ++ ".app")).
home_dir() ->
case init:get_argument(home) of
{ok, [[Home]]} -> Home;
Other -> Other
end.
%---------------------------------------------------------------------------
print_banner() ->
{ok, Product} = application:get_key(id),
{ok, Version} = application:get_key(vsn),
ProductLen = string:len(Product),
io:format("~n"
"+---+ +---+~n"
"| | | |~n"
"| | | |~n"
"| | | |~n"
"| +---+ +-------+~n"
"| |~n"
"| ~s +---+ |~n"
"| | | |~n"
"| ~s +---+ |~n"
"| |~n"
"+-------------------+~n"
"AMQP ~p-~p~n~s~n~s~n~n",
[Product, string:right([$v|Version], ProductLen),
?PROTOCOL_VERSION_MAJOR, ?PROTOCOL_VERSION_MINOR,
?COPYRIGHT_MESSAGE, ?INFORMATION_MESSAGE]),
Settings = [{"node", node()},
{"app descriptor", app_location()},
{"home dir", home_dir()},
{"cookie hash", rabbit_misc:cookie_hash()},
{"log", log_location(kernel)},
{"sasl log", log_location(sasl)},
{"database dir", rabbit_mnesia:dir()}],
DescrLen = lists:max([length(K) || {K, _V} <- Settings]),
Format = "~-" ++ integer_to_list(DescrLen) ++ "s: ~s~n",
lists:foreach(fun ({K, V}) -> io:format(Format, [K, V]) end, Settings),
io:nl().
start_child(Mod) ->
start_child(Mod, []).
start_child(Mod, Args) ->
{ok,_} = supervisor:start_child(rabbit_sup,
{Mod, {Mod, start_link, Args},
transient, 100, worker, [Mod]}),
ok.
ensure_working_log_handlers() ->
Handlers = gen_event:which_handlers(error_logger),
ok = ensure_working_log_handler(error_logger_file_h,
rabbit_error_logger_file_h,
error_logger_tty_h,
log_location(kernel),
Handlers),
ok = ensure_working_log_handler(sasl_report_file_h,
rabbit_sasl_report_file_h,
sasl_report_tty_h,
log_location(sasl),
Handlers),
ok.
ensure_working_log_handler(OldFHandler, NewFHandler, TTYHandler,
LogLocation, Handlers) ->
case LogLocation of
undefined -> ok;
tty -> case lists:member(TTYHandler, Handlers) of
true -> ok;
false ->
throw({error, {cannot_log_to_tty,
TTYHandler, not_installed}})
end;
_ -> case lists:member(NewFHandler, Handlers) of
true -> ok;
false -> case rotate_logs(LogLocation, "",
OldFHandler, NewFHandler) of
ok -> ok;
{error, Reason} ->
throw({error, {cannot_log_to_file,
LogLocation, Reason}})
end
end
end.
maybe_insert_default_data() ->
case rabbit_mnesia:is_db_empty() of
true -> insert_default_data();
false -> ok
end.
insert_default_data() ->
{ok, DefaultUser} = application:get_env(default_user),
{ok, DefaultPass} = application:get_env(default_pass),
{ok, DefaultVHost} = application:get_env(default_vhost),
{ok, [DefaultConfigurePerm, DefaultWritePerm, DefaultReadPerm]} =
application:get_env(default_permissions),
ok = rabbit_access_control:add_vhost(DefaultVHost),
ok = rabbit_access_control:add_user(DefaultUser, DefaultPass),
ok = rabbit_access_control:set_permissions(DefaultUser, DefaultVHost,
DefaultConfigurePerm,
DefaultWritePerm,
DefaultReadPerm),
ok.
start_builtin_amq_applications() ->
%%TODO: we may want to create a separate supervisor for these so
%%they don't bring down the entire app when they die and fail to
%%restart
ok.
rotate_logs(File, Suffix, Handler) ->
rotate_logs(File, Suffix, Handler, Handler).
rotate_logs(File, Suffix, OldHandler, NewHandler) ->
case File of
undefined -> ok;
tty -> ok;
_ -> gen_event:swap_handler(
error_logger,
{OldHandler, swap},
{NewHandler, {File, Suffix}})
end.
log_rotation_result({error, MainLogError}, {error, SaslLogError}) ->
{error, {{cannot_rotate_main_logs, MainLogError},
{cannot_rotate_sasl_logs, SaslLogError}}};
log_rotation_result({error, MainLogError}, ok) ->
{error, {cannot_rotate_main_logs, MainLogError}};
log_rotation_result(ok, {error, SaslLogError}) ->
{error, {cannot_rotate_sasl_logs, SaslLogError}};
log_rotation_result(ok, ok) ->
ok.
|