Skip to content
This repository was archived by the owner on Nov 17, 2020. It is now read-only.

Commit 1ac6493

Browse files
authored
Merge pull request #237 from rabbitmq/rabbitmq-server-1216-backport
Start `rabbit` application as transient.
2 parents 81e7d86 + 31ba56c commit 1ac6493

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

src/app_utils.erl

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,19 @@
1515
%%
1616
-module(app_utils).
1717

18-
-export([load_applications/1, start_applications/1, start_applications/2,
18+
-export([load_applications/1,
19+
start_applications/1, start_applications/2, start_applications/3,
1920
stop_applications/1, stop_applications/2, app_dependency_order/2,
2021
app_dependencies/1]).
2122

2223
-type error_handler() :: fun((atom(), any()) -> 'ok').
24+
-type restart_type() :: 'permanent' | 'transient' | 'temporary'.
2325

2426
-spec load_applications([atom()]) -> 'ok'.
2527
-spec start_applications([atom()]) -> 'ok'.
2628
-spec stop_applications([atom()]) -> 'ok'.
2729
-spec start_applications([atom()], error_handler()) -> 'ok'.
30+
-spec start_applications([atom()], error_handler(), [{atom(), restart_type()}]) -> 'ok'.
2831
-spec stop_applications([atom()], error_handler()) -> 'ok'.
2932
-spec app_dependency_order([atom()], boolean()) -> [digraph:vertex()].
3033
-spec app_dependencies(atom()) -> [atom()].
@@ -49,8 +52,11 @@ stop_applications(Apps) ->
4952
end).
5053

5154
start_applications(Apps, ErrorHandler) ->
55+
start_applications(Apps, ErrorHandler, []).
56+
57+
start_applications(Apps, ErrorHandler, RestartTypes) ->
5258
manage_applications(fun lists:foldl/3,
53-
fun application:ensure_all_started/1,
59+
fun(App) -> ensure_all_started(App, RestartTypes) end,
5460
fun application:stop/1,
5561
already_started,
5662
ErrorHandler,
@@ -127,3 +133,39 @@ manage_applications(Iterate, Do, Undo, SkipError, ErrorHandler, Apps) ->
127133
end, [], Apps),
128134
ok.
129135

136+
%% Stops the Erlang VM when the rabbit application stops abnormally
137+
%% i.e. message store reaches its restart limit
138+
default_restart_type(rabbit) -> transient;
139+
default_restart_type(_) -> temporary.
140+
141+
%% Copyright Ericsson AB 1996-2016. All Rights Reserved.
142+
%%
143+
%% Code orignially from Erlang/OTP source lib/kernel/src/application.erl
144+
%% and modified to use RestartTypes
145+
%%
146+
ensure_all_started(Application, RestartTypes) ->
147+
case ensure_all_started(Application, RestartTypes, []) of
148+
{ok, Started} ->
149+
{ok, lists:reverse(Started)};
150+
{error, Reason, Started} ->
151+
_ = [application:stop(App) || App <- Started],
152+
{error, Reason}
153+
end.
154+
155+
ensure_all_started(Application, RestartTypes, Started) ->
156+
RestartType = proplists:get_value(Application, RestartTypes, default_restart_type(Application)),
157+
case application:start(Application, RestartType) of
158+
ok ->
159+
{ok, [Application | Started]};
160+
{error, {already_started, Application}} ->
161+
{ok, Started};
162+
{error, {not_started, Dependency}} ->
163+
case ensure_all_started(Dependency, RestartTypes, Started) of
164+
{ok, NewStarted} ->
165+
ensure_all_started(Application, RestartTypes, NewStarted);
166+
Error ->
167+
Error
168+
end;
169+
{error, Reason} ->
170+
{error, {Application, Reason}, Started}
171+
end.

0 commit comments

Comments
 (0)