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(pgmp_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 |
10 |
supervisor:start_link({local, ?MODULE}, ?MODULE, []). |
30 |
|
|
31 |
|
|
32 |
|
init([]) -> |
33 |
10 |
{ok, configuration()}. |
34 |
|
|
35 |
|
|
36 |
|
configuration() -> |
37 |
10 |
{pgmp_config:sup_flags(?MODULE), children()}. |
38 |
|
|
39 |
|
|
40 |
|
children() -> |
41 |
10 |
[worker(pgmp_telemetry), |
42 |
|
worker(pgmp_message_tags), |
43 |
|
worker(pgmp_error_notice_fields), |
44 |
|
supervisor(pgmp_dbs_sup)]. |
45 |
|
|
46 |
|
|
47 |
|
worker(Arg) -> |
48 |
207 |
child(Arg). |
49 |
|
|
50 |
|
|
51 |
|
supervisor(Arg) -> |
52 |
70 |
maps:merge(child(Arg), #{type => supervisor}). |
53 |
|
|
54 |
|
|
55 |
|
child(#{m := M} = Arg) -> |
56 |
277 |
maps:merge( |
57 |
|
#{id => pgmp_util:tl_snake_case(M), start => mfargs(Arg)}, |
58 |
|
maps:with(keys(), Arg)); |
59 |
|
|
60 |
|
child(Arg) when is_atom(Arg) -> |
61 |
40 |
?FUNCTION_NAME(#{m => Arg}). |
62 |
|
|
63 |
|
|
64 |
|
mfargs(#{m := M} = Arg) -> |
65 |
277 |
{M, maps:get(f, Arg, start_link), maps:get(args, Arg, [])}. |
66 |
|
|
67 |
|
|
68 |
|
keys() -> |
69 |
277 |
[id, start, restart, significant, shutdown, type, modules]. |
70 |
|
|
71 |
|
|
72 |
|
get_child(SupRef, Id) -> |
73 |
102 |
lists:keyfind(Id, 1, supervisor:which_children(SupRef)). |
74 |
|
|
75 |
|
|
76 |
|
get_child_pid(SupRef, Id) -> |
77 |
26 |
element(2, get_child(SupRef, Id)). |