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(pgec_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 |
8 |
supervisor:start_link({local, ?MODULE}, ?MODULE, []). |
29 |
|
|
30 |
|
|
31 |
|
init([]) -> |
32 |
8 |
{ok, configuration()}. |
33 |
|
|
34 |
|
configuration() -> |
35 |
8 |
{pgec_config:sup_flags(?MODULE), children()}. |
36 |
|
|
37 |
|
|
38 |
|
children() -> |
39 |
8 |
[worker(pgec_telemetry), |
40 |
|
supervisor(pgec_storage_sup)]. |
41 |
|
|
42 |
|
|
43 |
|
worker(Arg) -> |
44 |
24 |
child(Arg). |
45 |
|
|
46 |
|
|
47 |
|
supervisor(Arg) -> |
48 |
8 |
maps:merge(child(Arg), #{type => supervisor}). |
49 |
|
|
50 |
|
|
51 |
|
child(#{m := M} = Arg) -> |
52 |
32 |
maps:merge( |
53 |
|
#{id => pgec_util:tl_snake_case(M), start => mfargs(Arg)}, |
54 |
|
maps:with(keys(), Arg)); |
55 |
|
|
56 |
|
child(Arg) when is_atom(Arg) -> |
57 |
24 |
?FUNCTION_NAME(#{m => Arg}). |
58 |
|
|
59 |
|
|
60 |
|
mfargs(#{m := M} = Arg) -> |
61 |
32 |
{M, maps:get(f, Arg, start_link), maps:get(args, Arg, [])}. |
62 |
|
|
63 |
|
|
64 |
|
keys() -> |
65 |
32 |
[id, start, restart, significant, shutdown, type, modules]. |
66 |
|
|
67 |
|
|
68 |
|
get_child(SupRef, Id) -> |
69 |
16 |
lists:keyfind(Id, 1, supervisor:which_children(SupRef)). |