4343
4444-export_type ([
4545 child_spec / 0 ,
46+ startchild_ret / 0 ,
47+ startlink_ret / 0 ,
4648 strategy / 0 ,
4749 sup_flags / 0
4850]).
5355 | temporary
5456 | {terminating , permanent | transient | temporary , gen_server :from ()}.
5557-type shutdown () :: brutal_kill | timeout ().
56- -type child_type () :: worker | supervisor .
58+ -type worker () :: worker | supervisor .
59+ -type child_id () :: term ().
60+ -type child () :: undefined | pid ().
5761
5862-type strategy () :: one_for_all | one_for_one .
5963-type sup_flags () ::
7074 start := {module (), atom (), [any ()]},
7175 restart => restart (),
7276 shutdown => shutdown (),
73- type => child_type (),
77+ type => worker (),
7478 modules => [module ()] | dynamic
7579 }
7680 | {
7781 Id :: any (),
7882 StartFunc :: {module (), atom (), [any ()]},
7983 Restart :: restart (),
8084 Shutdown :: shutdown (),
81- Type :: child_type (),
85+ Type :: worker (),
8286 Modules :: [module ()] | dynamic
8387 }.
8488
89+ -type startlink_ret () :: {ok , pid ()} | ignore | {error , startlink_err ()}.
90+ -type startlink_err () :: {already_started , pid ()} | {shutdown , term ()} | term ().
91+ -type startchild_ret () ::
92+ {ok , Child :: child ()} | {ok , Child :: child (), Info :: term ()} | {error , startchild_err ()}.
93+ -type startchild_err () :: already_present | {already_started , Child :: child ()} | term ().
94+ -type sup_name () :: {local , Name :: atom ()}.
95+ -type sup_ref () ::
96+ (Name :: atom ())
97+ | {Name :: atom (), Node :: node ()}
98+ | {global , Name :: term ()}
99+ | {via , Module :: module (), Name :: any ()}
100+ | pid ().
101+
85102-record (child , {
86103 pid = undefined :: pid () | undefined | {restarting , pid ()} | {restarting , undefined },
87104 id :: any (),
88105 start :: {module (), atom (), [any ()] | undefined },
89106 restart :: restart (),
90107 shutdown :: shutdown (),
91- type :: child_type (),
108+ type :: worker (),
92109 modules = [] :: [module ()] | dynamic
93110}).
94111% % note: the list of children should always be kept in order, with first to start at the head.
108125% % could be used to set a sane default for the platform (OTP uses 1000).
109126-define (STALE_RESTART_LIMIT , 100 ).
110127
128+ -spec start_link (Module :: module (), Args :: [any ()]) -> startlink_ret ().
111129start_link (Module , Args ) ->
112130 gen_server :start_link (? MODULE , {Module , Args }, []).
113131
132+ -spec start_link (SupName :: sup_name (), Module :: module (), Args :: [any ()]) -> startlink_ret ().
114133start_link (SupName , Module , Args ) ->
115134 gen_server :start_link (SupName , ? MODULE , {Module , Args }, []).
116135
136+ -spec start_child (Supervisor :: sup_ref (), ChildSpec :: child_spec ()) -> startchild_ret ().
117137start_child (Supervisor , ChildSpec ) ->
118138 gen_server :call (Supervisor , {start_child , ChildSpec }).
119139
140+ -spec terminate_child (Supervisor :: sup_ref (), ChildId :: any ()) -> ok | {error , not_found }.
120141terminate_child (Supervisor , ChildId ) ->
121142 gen_server :call (Supervisor , {terminate_child , ChildId }).
122143
144+ -spec restart_child (Supervisor :: sup_ref (), ChildId :: any ()) ->
145+ {ok , Child :: child ()}
146+ | {ok , Child :: child (), Info :: term ()}
147+ | {error , Reason :: running | restarting | not_found | term ()}.
123148restart_child (Supervisor , ChildId ) ->
124149 gen_server :call (Supervisor , {restart_child , ChildId }).
125150
151+ -spec delete_child (Supervisor :: sup_ref (), ChildId :: any ()) ->
152+ ok | {error , Reason :: running | restarting | not_found }.
126153delete_child (Supervisor , ChildId ) ->
127154 gen_server :call (Supervisor , {delete_child , ChildId }).
128155
156+ -spec which_children (Supervisor :: sup_ref ()) ->
157+ [
158+ {
159+ Id :: child_id () | undefined ,
160+ Child :: child () | restarting ,
161+ Type :: worker (),
162+ Modules :: [module ()]
163+ }
164+ ].
129165which_children (Supervisor ) ->
130166 gen_server :call (Supervisor , which_children ).
131167
168+ -spec count_children (Supervisor :: sup_ref ()) ->
169+ [
170+ {specs , ChildSpecCount :: non_neg_integer ()}
171+ | {active , ActiveProcessCount :: non_neg_integer ()}
172+ | {supervisors , ChildSupervisorCount :: non_neg_integer ()}
173+ | {workers , ChildWorkerCount :: non_neg_integer ()}
174+ ].
132175count_children (Supervisor ) ->
133176 gen_server :call (Supervisor , count_children ).
134177
178+ % @hidden
179+ -spec init ({Mod :: module (), Args :: [any ()]}) ->
180+ {ok , State :: # state {}} | {stop , {bad_return , {Mod :: module (), init , Reason :: term ()}}}.
135181init ({Mod , Args }) ->
136182 erlang :process_flag (trap_exit , true ),
137183 case Mod :init (Args ) of
@@ -196,13 +242,17 @@ child_spec_to_record(#{id := ChildId, start := MFA} = ChildMap) ->
196242 modules = Modules
197243 }.
198244
245+ % @hidden
246+ -spec init_state (ChildSpecs :: [child_spec ()], State :: # state {}) -> State :: # state {}.
199247init_state ([ChildSpec | T ], State ) ->
200248 Child = child_spec_to_record (ChildSpec ),
201249 NewChildren = [Child | State # state .children ],
202250 init_state (T , State # state {children = NewChildren });
203251init_state ([], State ) ->
204252 State # state {children = lists :reverse (State # state .children )}.
205253
254+ -spec start_children (ChildSpecs :: [child_spec ()], State :: # state {}) ->
255+ ChildSpecs :: [child_spec ()].
206256start_children ([Child | T ], StartedC ) ->
207257 case try_start (Child ) of
208258 {ok , Pid , _Result } ->
@@ -212,6 +262,7 @@ start_children([], StartedC) ->
212262 % % We should always keep the start list in order for later one_for_all restarts.
213263 lists :reverse (StartedC ).
214264
265+ % @hidden
215266handle_call ({start_child , ChildSpec }, _From , # state {children = Children } = State ) ->
216267 Child = child_spec_to_record (ChildSpec ),
217268 # child {id = ID } = Child ,
@@ -280,9 +331,11 @@ handle_call(count_children, _From, #state{children = Children} = State) ->
280331 Reply = [{specs , Specs }, {active , Active }, {supervisors , Supers }, {workers , Workers }],
281332 {reply , Reply , State }.
282333
334+ % @hidden
283335handle_cast (_Msg , State ) ->
284336 {noreply , State }.
285337
338+ % @hidden
286339handle_info ({'EXIT' , Pid , Reason }, State ) ->
287340 case handle_child_exit (Pid , Reason , State ) of
288341 {ok , State1 } ->
0 commit comments