-
Notifications
You must be signed in to change notification settings - Fork 20
/
supervisor.ex
74 lines (58 loc) · 2.32 KB
/
supervisor.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
defmodule Parent.Supervisor do
@moduledoc """
Supervisor of child processes.
This module works similarly to callbackless supervisors started with `Supervisor.start_link/2`.
To start a supervisor and some children you can do the following:
Parent.Supervisor.start_link([
child_spec1,
child_spec2,
# ...
])
To install a parent supervisor in the supervision tree you can provide child specification in the
shape of `{Parent.Supervisor, {children, parent_options}}`.
To build a dedicate supervisor module you can do:
defmodule MySupervisor do
use Parent.Supervisor
def start_link(children, options),
do: Parent.Supervisor.start_link(children, options)
# ...
end
And now, you can install this supervisor in the supervision tree by passing
`{MySupervisor, {child_specs, parent_options}}` as the child specification to the parent.
You can interact with the running supervisor using functions from the `Parent.Client` module.
Refer to the `Parent` module for detailed explanation of child specifications, parent options,
and behaviour of parent processes.
In case you need more flexibility, take a look `Parent.GenServer`.
"""
use Parent.GenServer
@doc """
Starts the parent process.
This function returns only after all the children have been started. If a child fails to start,
the parent process will terminate all successfully started children, and then itself.
"""
@spec start_link([Parent.start_spec()], Parent.GenServer.options()) :: GenServer.on_start()
def start_link(children, options \\ []),
do: Parent.GenServer.start_link(__MODULE__, children, options)
@impl GenServer
def init(children) do
Parent.start_all_children!(children)
{:ok, nil}
end
@spec child_spec({[Parent.start_spec()], Parent.GenServer.options()}) :: Parent.child_spec()
def child_spec({children, options}) do
[start: {__MODULE__, :start_link, [children, options]}]
|> Parent.parent_spec()
|> Parent.child_spec(id: Keyword.get(options, :name, __MODULE__))
end
@doc false
defmacro __using__(_) do
quote do
def child_spec(arg) do
[start: {__MODULE__, :start_link, [arg]}]
|> Parent.parent_spec()
|> Parent.child_spec(id: __MODULE__)
end
defoverridable child_spec: 1
end
end
end