/home/runner/work/mcd/mcd/_site/ct/ct_run.ct_mcd@fv-az773-648.2023-11-24_16.44.07/mcd_sup.COVER.html

1 %% Copyright (c) 2022 Peter Morgan <peter.james.morgan@gmail.com>
2 %%
3 %% Licensed under the Apache License, Version 2.0 (the "License");
4 %% you may not use this file except in compliance with the License.
5 %% You may obtain a copy of the License at
6 %%
7 %% http://www.apache.org/licenses/LICENSE-2.0
8 %%
9 %% Unless required by applicable law or agreed to in writing, software
10 %% distributed under the License is distributed on an "AS IS" BASIS,
11 %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 %% See the License for the specific language governing permissions and
13 %% limitations under the License.
14
15
16 -module(mcd_sup).
17
18
19 -behaviour(supervisor).
20 -export([get_child/2]).
21 -export([init/1]).
22 -export([start_link/0]).
23 -export([supervisor/1]).
24 -export([worker/1]).
25
26
27 start_link() ->
28 11 supervisor:start_link({local, ?MODULE}, ?MODULE, []).
29
30
31 init([]) ->
32 11 {ok, configuration()}.
33
34
35 configuration() ->
36 11 {#{}, children()}.
37
38
39 children() ->
40 11 Callback = mcd_config:protocol(callback),
41
42 11 [worker(mcd_telemetry),
43 worker(mcd_opcode),
44 worker(mcd_status),
45 worker(mcd_stat),
46 worker(#{m => mcd_reaper, args => [#{callback => Callback}]}),
47 supervisor(#{m => mcd_tcp_sup, args => [#{callback => Callback}]}),
48 supervisor(#{m => mcd_udp_sup, args => [#{callback => Callback}]})].
49
50
51 worker(Arg) ->
52 77 child(Arg).
53
54
55 supervisor(Arg) ->
56 33 maps:merge(child(Arg), #{type => supervisor}).
57
58
59 child(#{m := M} = Arg) ->
60 110 maps:merge(
61 #{id => M, start => mfargs(Arg)},
62 maps:with(keys(), Arg));
63
64 child(Arg) when is_atom(Arg) ->
65 55 ?FUNCTION_NAME(#{m => Arg}).
66
67
68 mfargs(#{m := M} = Arg) ->
69 110 {M, maps:get(f, Arg, start_link), maps:get(args, Arg, [])}.
70
71
72 keys() ->
73 110 [id, start, restart, significant, shutdown, type, modules].
74
75
76 get_child(SupRef, Id) ->
77
:-(
lists:keyfind(Id, 1, supervisor:which_children(SupRef)).
Line Hits Source