2828 start_child /2 ,
2929 terminate_child /2 ,
3030 restart_child /2 ,
31- delete_child /2
31+ delete_child /2 ,
32+ which_children /1
3233]).
3334
3435-export ([
8687 start :: {module (), atom (), [any ()] | undefined },
8788 restart :: restart (),
8889 shutdown :: shutdown (),
89- type :: child_type
90+ type :: child_type ,
91+ modules = [] :: [module ()] | dynamic
9092}).
9193-record (state , {restart_strategy :: strategy (), children = [] :: [# child {}]}).
9294
@@ -107,6 +109,9 @@ restart_child(Supervisor, ChildId) ->
107109delete_child (Supervisor , ChildId ) ->
108110 gen_server :call (Supervisor , {delete_child , ChildId }).
109111
112+ which_children (Supervisor ) ->
113+ gen_server :call (Supervisor , which_children ).
114+
110115init ({Mod , Args }) ->
111116 erlang :process_flag (trap_exit , true ),
112117 case Mod :init (Args ) of
@@ -123,13 +128,14 @@ init({Mod, Args}) ->
123128 end .
124129
125130-spec child_spec_to_record (child_spec ()) -> # child {}.
126- child_spec_to_record ({ChildId , MFA , Restart , Shutdown , Type , _Modules }) ->
131+ child_spec_to_record ({ChildId , MFA , Restart , Shutdown , Type , Modules }) ->
127132 # child {
128133 id = ChildId ,
129134 start = MFA ,
130135 restart = Restart ,
131136 shutdown = Shutdown ,
132- type = Type
137+ type = Type ,
138+ modules = Modules
133139 };
134140child_spec_to_record (#{id := ChildId , start := MFA } = ChildMap ) ->
135141 Restart = maps :get (restart , ChildMap , permanent ),
@@ -142,12 +148,21 @@ child_spec_to_record(#{id := ChildId, start := MFA} = ChildMap) ->
142148 supervisor -> infinity
143149 end
144150 ),
151+ Modules = maps :get (
152+ modules ,
153+ ChildMap ,
154+ case MFA of
155+ {M , _ , _ } -> [M ];
156+ _ -> []
157+ end
158+ ),
145159 # child {
146160 id = ChildId ,
147161 start = MFA ,
148162 restart = Restart ,
149163 shutdown = Shutdown ,
150- type = Type
164+ type = Type ,
165+ modules = Modules
151166 }.
152167
153168init_state ([ChildSpec | T ], State ) ->
@@ -262,7 +277,10 @@ handle_call({delete_child, ID}, _From, #state{children = Children} = State) ->
262277 {reply , {error , running }, State };
263278 false ->
264279 {reply , {error , not_found }, State }
265- end .
280+ end ;
281+ handle_call (which_children , _From , # state {children = Children } = State ) ->
282+ ChildrenInfo = lists :map (fun child_to_info /1 , Children ),
283+ {reply , ChildrenInfo , State }.
266284
267285handle_cast (_Msg , State ) ->
268286 {noreply , State }.
@@ -337,6 +355,15 @@ try_start(#child{start = {M, F, Args}} = Record) ->
337355 {error , {{'EXIT' , Error }, Record }}
338356 end .
339357
358+ child_to_info (# child {id = Id , pid = Pid , type = Type , modules = Modules }) ->
359+ Child =
360+ case Pid of
361+ undefined -> undefined ;
362+ {restarting , _ } -> restarting ;
363+ _ when is_pid (Pid ) -> Pid
364+ end ,
365+ {Id , Child , Type , Modules }.
366+
340367do_terminate (# child {pid = Pid , shutdown = brutal_kill }) ->
341368 exit (Pid , kill );
342369do_terminate (# child {pid = Pid , shutdown = infinity }) ->
0 commit comments