Packages
rabbit_common
3.8.12-rc.2
4.2.1
4.2.0
retired
4.2.0-rc.1
retired
4.1.6
4.1.5
retired
4.1.5-rc.2
retired
4.1.5-rc.1
4.0.3
4.0.3-rc.1
4.0.2
4.0.2-rc.2
4.0.2-rc.1
4.0.1
4.0.0
4.0.0-rc.2
4.0.0-rc.1
3.13.7
3.13.6
3.13.5
3.13.4
3.13.3
3.13.2
3.13.2-rc.1
3.13.1
3.13.0
3.13.0-rc.6
3.13.0-rc.5
3.13.0-rc.4
3.13.0-rc.3
3.13.0-rc.2
3.13.0-rc.1
3.12.14
3.12.13
3.12.12
3.12.11
3.12.10
3.12.9
3.12.8
3.12.7
3.12.6
3.12.5
3.12.4
3.12.3
3.12.2
3.12.1
3.12.0
3.12.0-rc.4
3.12.0-rc.3
3.12.0-rc.2
3.12.0-rc.1
3.11.28
3.11.27
3.11.26
3.11.25
3.11.24
3.11.23
3.11.22
3.11.21
3.11.20
3.11.19
3.11.18
3.11.17
3.11.16
3.11.15
3.11.14
3.11.13
3.11.12
3.11.11
3.11.10
3.11.9
3.11.8
3.11.7
3.11.6
3.11.5
3.11.4
3.11.3
3.11.2
3.11.1
3.11.0
3.11.0-rc.2
3.11.0-rc.1
3.11.0-1
3.10.25
3.10.24
3.10.23
3.10.22
3.10.21
3.10.20
3.10.19
3.10.18
3.10.17
3.10.16
3.10.15
3.10.14
3.10.13
3.10.12
3.10.11
3.10.10
3.10.9
3.10.8
3.10.7
3.10.6
3.10.5
3.10.4
3.10.3
3.10.2
3.10.1
3.10.0
3.10.0-rc.6
3.10.0-rc.5
3.9.29
3.9.28
3.9.27
3.9.26
3.9.25
3.9.24
3.9.23
3.9.22
3.9.21
3.9.20
3.9.19
3.9.18
3.9.17
3.9.16
3.9.15
3.9.11
3.9.10
3.9.9
3.9.8
3.9.7
3.9.6
3.9.5
3.9.4
3.9.3
3.9.2
3.9.1
3.8.35
3.8.34
3.8.33
3.8.32
3.8.31
3.8.30
3.8.26
3.8.25
3.8.24
3.8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7.0-rc.2
3.7.0-alpha.544
3.7.0-alpha.542
3.6.16
3.6.16-rc.1
3.6.15
3.6.15-rc.1
3.6.14
3.6.13
3.6.12
3.6.11
3.6.10
3.6.9
3.6.8
3.6.7
3.6.7-pre.1
3.5.6
3.5.0
3.4.0
3.3.5
3.0.2
0.0.0-rc.1
Modules shared by rabbitmq-server and rabbitmq-erlang-client
Security advisory:
This version has known vulnerabilities.
View advisories
Current section
Files
Jump to
Current section
Files
src/app_utils.erl
%% 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-2021 VMware, Inc. or its affiliates. All rights reserved.
%%
-module(app_utils).
-export([load_applications/1,
start_applications/1, start_applications/2, start_applications/3,
stop_applications/1, stop_applications/2, app_dependency_order/2,
app_dependencies/1]).
-type error_handler() :: fun((atom(), any()) -> 'ok' | no_return()).
-type restart_type() :: 'permanent' | 'transient' | 'temporary'.
-spec load_applications([atom()]) -> 'ok'.
-spec start_applications([atom()]) -> 'ok'.
-spec stop_applications([atom()]) -> 'ok'.
-spec start_applications([atom()], error_handler()) -> 'ok'.
-spec start_applications([atom()], error_handler(), #{atom() => restart_type()}) -> 'ok'.
-spec stop_applications([atom()], error_handler()) -> 'ok'.
-spec app_dependency_order([atom()], boolean()) -> [digraph:vertex()].
-spec app_dependencies(atom()) -> [atom()].
-spec failed_to_start_app(atom(), any()) -> no_return().
-spec failed_to_stop_app(atom(), any()) -> no_return().
%%---------------------------------------------------------------------------
%% Public API
load_applications(Apps) ->
load_applications(queue:from_list(Apps), sets:new()),
ok.
start_applications(Apps) ->
start_applications(
Apps, fun failed_to_start_app/2).
stop_applications(Apps) ->
stop_applications(
Apps, fun failed_to_stop_app/2).
failed_to_start_app(App, Reason) ->
throw({error, {cannot_start_application, App, Reason}}).
failed_to_stop_app(App, Reason) ->
throw({error, {cannot_stop_application, App, Reason}}).
start_applications(Apps, ErrorHandler) ->
start_applications(Apps, ErrorHandler, #{}).
start_applications(Apps, ErrorHandler, RestartTypes) ->
manage_applications(fun lists:foldl/3,
fun(App) -> ensure_all_started(App, RestartTypes) end,
fun application:stop/1,
already_started,
ErrorHandler,
Apps).
stop_applications(Apps, ErrorHandler) ->
manage_applications(fun lists:foldr/3,
fun(App) ->
rabbit_log:info("Stopping application '~s'", [App]),
application:stop(App)
end,
fun(App) -> ensure_all_started(App, #{}) end,
not_started,
ErrorHandler,
Apps).
app_dependency_order(RootApps, StripUnreachable) ->
{ok, G} = rabbit_misc:build_acyclic_graph(
fun ({App, _Deps}) -> [{App, App}] end,
fun ({App, Deps}) -> [{Dep, App} || Dep <- Deps] end,
[{App, app_dependencies(App)} ||
{App, _Desc, _Vsn} <- application:loaded_applications()]),
try
case StripUnreachable of
true -> digraph:del_vertices(G, digraph:vertices(G) --
digraph_utils:reachable(RootApps, G));
false -> ok
end,
digraph_utils:topsort(G)
after
true = digraph:delete(G)
end.
%%---------------------------------------------------------------------------
%% Private API
load_applications(Worklist, Loaded) ->
case queue:out(Worklist) of
{empty, _WorkList} ->
ok;
{{value, App}, Worklist1} ->
case sets:is_element(App, Loaded) of
true -> load_applications(Worklist1, Loaded);
false -> case application:load(App) of
ok -> ok;
{error, {already_loaded, App}} -> ok;
Error -> throw(Error)
end,
load_applications(
queue:join(Worklist1,
queue:from_list(app_dependencies(App))),
sets:add_element(App, Loaded))
end
end.
app_dependencies(App) ->
case application:get_key(App, applications) of
undefined -> [];
{ok, Lst} -> Lst
end.
manage_applications(Iterate, Do, Undo, SkipError, ErrorHandler, Apps) ->
Iterate(fun (App, Acc) ->
case Do(App) of
ok -> [App | Acc];
{ok, []} -> Acc;
{ok, [App]} -> [App | Acc];
{ok, StartedApps} -> StartedApps ++ Acc;
{error, {SkipError, _}} -> Acc;
{error, Reason} ->
lists:foreach(Undo, Acc),
ErrorHandler(App, Reason)
end
end, [], Apps),
ok.
%% Stops the Erlang VM when the rabbit application stops abnormally
%% i.e. message store reaches its restart limit
default_restart_type(rabbit) -> transient;
default_restart_type(_) -> temporary.
%% Copyright Ericsson AB 1996-2016. All Rights Reserved.
%%
%% Code originally from Erlang/OTP source lib/kernel/src/application.erl
%% and modified to use RestartTypes map
%%
ensure_all_started(Application, RestartTypes) ->
case ensure_all_started(Application, RestartTypes, []) of
{ok, Started} ->
{ok, lists:reverse(Started)};
{error, Reason, Started} ->
_ = [application:stop(App) || App <- Started],
{error, Reason}
end.
ensure_all_started(Application, RestartTypes, Started) ->
RestartType = maps:get(Application, RestartTypes, default_restart_type(Application)),
case application:start(Application, RestartType) of
ok ->
{ok, [Application | Started]};
{error, {already_started, Application}} ->
{ok, Started};
{error, {not_started, Dependency}} ->
case ensure_all_started(Dependency, RestartTypes, Started) of
{ok, NewStarted} ->
ensure_all_started(Application, RestartTypes, NewStarted);
Error ->
Error
end;
{error, Reason} ->
{error, {Application, Reason}, Started}
end.