1%% This Source Code Form is subject to the terms of the Mozilla Public
2%% License, v. 2.0. If a copy of the MPL was not distributed with this
3%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
4%%
5%% Copyright (c) 2007-2021 VMware, Inc. or its affiliates.  All rights reserved.
6%%
7
8-module(rabbit_sup).
9
10-behaviour(supervisor).
11
12-export([start_link/0, start_child/1, start_child/2, start_child/3, start_child/4,
13         start_supervisor_child/1, start_supervisor_child/2,
14         start_supervisor_child/3,
15         start_restartable_child/1, start_restartable_child/2,
16         start_delayed_restartable_child/1, start_delayed_restartable_child/2,
17         stop_child/1]).
18
19-export([init/1]).
20
21-include_lib("rabbit_common/include/rabbit.hrl").
22
23-define(SERVER, ?MODULE).
24
25%%----------------------------------------------------------------------------
26
27-spec start_link() -> rabbit_types:ok_pid_or_error().
28
29start_link() -> supervisor:start_link({local, ?SERVER}, ?MODULE, []).
30
31-spec start_child(atom()) -> 'ok'.
32
33start_child(Mod) -> start_child(Mod, []).
34
35-spec start_child(atom(), [any()]) -> 'ok'.
36
37start_child(Mod, Args) -> start_child(Mod, Mod, Args).
38
39-spec start_child(atom(), atom(), [any()]) -> 'ok'.
40
41start_child(ChildId, Mod, Args) ->
42    child_reply(supervisor:start_child(
43                  ?SERVER,
44                  {ChildId, {Mod, start_link, Args},
45                   transient, ?WORKER_WAIT, worker, [Mod]})).
46
47-spec start_child(atom(), atom(), atom(), [any()]) -> 'ok'.
48
49start_child(ChildId, Mod, Fun, Args) ->
50    child_reply(supervisor:start_child(
51                  ?SERVER,
52                  {ChildId, {Mod, Fun, Args},
53                   transient, ?WORKER_WAIT, worker, [Mod]})).
54
55-spec start_supervisor_child(atom()) -> 'ok'.
56
57start_supervisor_child(Mod) -> start_supervisor_child(Mod, []).
58
59-spec start_supervisor_child(atom(), [any()]) -> 'ok'.
60
61start_supervisor_child(Mod, Args) -> start_supervisor_child(Mod, Mod, Args).
62
63-spec start_supervisor_child(atom(), atom(), [any()]) -> 'ok'.
64
65start_supervisor_child(ChildId, Mod, Args) ->
66    child_reply(supervisor:start_child(
67                  ?SERVER,
68                  {ChildId, {Mod, start_link, Args},
69                   transient, infinity, supervisor, [Mod]})).
70
71-spec start_restartable_child(atom()) -> 'ok'.
72
73start_restartable_child(M)            -> start_restartable_child(M, [], false).
74
75-spec start_restartable_child(atom(), [any()]) -> 'ok'.
76
77start_restartable_child(M, A)         -> start_restartable_child(M, A,  false).
78
79-spec start_delayed_restartable_child(atom()) -> 'ok'.
80
81start_delayed_restartable_child(M)    -> start_restartable_child(M, [], true).
82
83-spec start_delayed_restartable_child(atom(), [any()]) -> 'ok'.
84
85start_delayed_restartable_child(M, A) -> start_restartable_child(M, A,  true).
86
87start_restartable_child(Mod, Args, Delay) ->
88    Name = list_to_atom(atom_to_list(Mod) ++ "_sup"),
89    child_reply(supervisor:start_child(
90                  ?SERVER,
91                  {Name, {rabbit_restartable_sup, start_link,
92                          [Name, {Mod, start_link, Args}, Delay]},
93                   transient, infinity, supervisor, [rabbit_restartable_sup]})).
94
95-spec stop_child(atom()) -> rabbit_types:ok_or_error(any()).
96
97stop_child(ChildId) ->
98    case supervisor:terminate_child(?SERVER, ChildId) of
99        ok -> supervisor:delete_child(?SERVER, ChildId);
100        E  -> E
101    end.
102
103init([]) -> {ok, {{one_for_all, 0, 1}, []}}.
104
105
106%%----------------------------------------------------------------------------
107
108child_reply({ok, _}) -> ok;
109child_reply(X)       -> X.
110