15
15
% %
16
16
-module (app_utils ).
17
17
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 ,
19
20
stop_applications /1 , stop_applications /2 , app_dependency_order /2 ,
20
21
app_dependencies /1 ]).
21
22
22
23
-type error_handler () :: fun ((atom (), any ()) -> 'ok' ).
24
+ -type restart_type () :: 'permanent' | 'transient' | 'temporary' .
23
25
24
26
-spec load_applications ([atom ()]) -> 'ok' .
25
27
-spec start_applications ([atom ()]) -> 'ok' .
26
28
-spec stop_applications ([atom ()]) -> 'ok' .
27
29
-spec start_applications ([atom ()], error_handler ()) -> 'ok' .
30
+ -spec start_applications ([atom ()], error_handler (), [{atom (), restart_type ()}]) -> 'ok' .
28
31
-spec stop_applications ([atom ()], error_handler ()) -> 'ok' .
29
32
-spec app_dependency_order ([atom ()], boolean ()) -> [digraph :vertex ()].
30
33
-spec app_dependencies (atom ()) -> [atom ()].
@@ -49,8 +52,11 @@ stop_applications(Apps) ->
49
52
end ).
50
53
51
54
start_applications (Apps , ErrorHandler ) ->
55
+ start_applications (Apps , ErrorHandler , []).
56
+
57
+ start_applications (Apps , ErrorHandler , RestartTypes ) ->
52
58
manage_applications (fun lists :foldl /3 ,
53
- fun application : ensure_all_started / 1 ,
59
+ fun ( App ) -> ensure_all_started ( App , RestartTypes ) end ,
54
60
fun application :stop /1 ,
55
61
already_started ,
56
62
ErrorHandler ,
@@ -127,3 +133,39 @@ manage_applications(Iterate, Do, Undo, SkipError, ErrorHandler, Apps) ->
127
133
end , [], Apps ),
128
134
ok .
129
135
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