1 |
|
%% Copyright (c) 2023 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(msc_sup). |
17 |
|
|
18 |
|
|
19 |
|
-behaviour(supervisor). |
20 |
|
-export([get_child/2]). |
21 |
|
-export([get_child_pid/2]). |
22 |
|
-export([init/1]). |
23 |
|
-export([start_link/0]). |
24 |
|
-export([supervisor/1]). |
25 |
|
-export([worker/1]). |
26 |
|
|
27 |
|
|
28 |
|
start_link() -> |
29 |
1 |
supervisor:start_link({local, ?MODULE}, ?MODULE, []). |
30 |
|
|
31 |
|
|
32 |
|
init([]) -> |
33 |
1 |
Procs = [worker(msc_telemetry), |
34 |
|
supervisor(msc_connections_sup)], |
35 |
1 |
{ok, {#{}, Procs}}. |
36 |
|
|
37 |
|
|
38 |
|
worker(Arg) -> |
39 |
5 |
child(Arg). |
40 |
|
|
41 |
|
|
42 |
|
supervisor(Arg) -> |
43 |
2 |
maps:merge(child(Arg), #{type => supervisor}). |
44 |
|
|
45 |
|
|
46 |
|
child(#{m := M} = Arg) -> |
47 |
7 |
maps:merge( |
48 |
|
#{id => msc_util:tl_snake_case(M), start => mfargs(Arg)}, |
49 |
|
maps:with(keys(), Arg)); |
50 |
|
|
51 |
|
child(Arg) when is_atom(Arg) -> |
52 |
2 |
?FUNCTION_NAME(#{m => Arg}). |
53 |
|
|
54 |
|
|
55 |
|
mfargs(#{m := M} = Arg) -> |
56 |
7 |
{M, maps:get(f, Arg, start_link), maps:get(args, Arg, [])}. |
57 |
|
|
58 |
|
|
59 |
|
keys() -> |
60 |
7 |
[id, start, restart, significant, shutdown, type, modules]. |
61 |
|
|
62 |
|
|
63 |
|
get_child(SupRef, Id) -> |
64 |
6 |
lists:keyfind(Id, 1, supervisor:which_children(SupRef)). |
65 |
|
|
66 |
|
|
67 |
|
get_child_pid(SupRef, Id) -> |
68 |
2 |
element(2, get_child(SupRef, Id)). |