Supervisors in elixir
{:ok, pid} = Supervisor.start_link([Supervisor.Spec.worker(GenServerDB.Worker, [], [])], strategy: :simple_one_for_one)
Key Function | Type Spec | Notes |
---|---|---|
Supervisor.start_link/2 | start_link([Supervisor.Spec.spec], options) :: on_start | [Supervisor.Spec.spec] is usually just a list of workers or supervisors made using Supervisor.Spec.worker/3 or Supervisor.Spec.supervisor/3 . options need to at least include a strategy: option. To dynamically add workers at runtime, use the :simple_one_for_one strategy. |
iex(3)> {:ok, pid} = Supervisor.start_link([Supervisor.Spec.worker(GenServerDB.Worker, [], [])], strategy: :simple_one_for_one)
iex(4)> Supervisor.start_child(pid, [1])
Supervisor.start_child(pid, [1])
{:ok, #PID<0.128.0>}
iex(6)> Supervisor.start_child(pid, [2])
Supervisor.start_child(pid, [2])
{:ok, #PID<0.131.0>}
iex(7)> Supervisor.start_child(pid, [3])
Supervisor.start_child(pid, [3])
{:ok, #PID<0.133.0>}
iex(8)> Supervisor.which_children(pid)
Supervisor.which_children(pid)
[{:undefined, #PID<0.128.0>, :worker, [GenServerDB.Worker]},
{:undefined, #PID<0.131.0>, :worker, [GenServerDB.Worker]},
{:undefined, #PID<0.133.0>, :worker, [GenServerDB.Worker]}]
iex(1)> {:ok, pid} = Supervisor.start_link([Supervisor.Spec.worker(GenServerDB.Worker, [], [])], strategy: :simple_one_for_one)
{:ok, pid} = Supervisor.start_link([Supervisor.Spec.worker(GenServerDB.Worker, [], [])], strategy: :simple_one_for_one)
{:ok, #PID<0.120.0>}
iex(2)> for n <- 1..20, do: Supervisor.start_child(pid, [n])
for n <- 1..20, do: Supervisor.start_child(pid, [n])
[ok: #PID<0.122.0>, ok: #PID<0.123.0>, ok: #PID<0.124.0>, ok: #PID<0.125.0>,
ok: #PID<0.126.0>, ok: #PID<0.127.0>, ok: #PID<0.128.0>, ok: #PID<0.129.0>,
ok: #PID<0.130.0>, ok: #PID<0.131.0>, ok: #PID<0.132.0>, ok: #PID<0.133.0>,
ok: #PID<0.134.0>, ok: #PID<0.135.0>, ok: #PID<0.136.0>, ok: #PID<0.137.0>,
ok: #PID<0.138.0>, ok: #PID<0.139.0>, ok: #PID<0.140.0>, ok: #PID<0.141.0>]
Key Function | Type Spec | Notes |
---|---|---|
Supervisor.start_child/2 | start_child(supervisor, [term]) :: on_start_child OR start_child(supervisor, Supervisor.Spec.spec) :: on_start_child | supervisor is the supervisor pid, while [term] means a list of additional args to be concatenated to the original worker's start function (GenServer.start_link/3 by default) - use this when using the strategy :simple_one_for_one . |
PID
iex(2)> for n <- 1..20, do: Supervisor.start_child(pid, [n])
for n <- 1..20, do: Supervisor.start_child(pid, [n])
[ok: #PID<0.122.0>, ok: #PID<0.123.0>, ok: #PID<0.124.0>, ok: #PID<0.125.0>,
ok: #PID<0.126.0>, ok: #PID<0.127.0>, ok: #PID<0.128.0>, ok: #PID<0.129.0>,
ok: #PID<0.130.0>, ok: #PID<0.131.0>, ok: #PID<0.132.0>, ok: #PID<0.133.0>,
ok: #PID<0.134.0>, ok: #PID<0.135.0>, ok: #PID<0.136.0>, ok: #PID<0.137.0>,
ok: #PID<0.138.0>, ok: #PID<0.139.0>, ok: #PID<0.140.0>, ok: #PID<0.141.0>]
iex(5)> :global.whereis_name({:name, GenServer.DB.Worker, 15})
:global.whereis_name({:name, GenServer.DB.Worker, 15})
#PID<0.136.0>
Key Function | Type Spec | Notes |
---|---|---|
:global.whereis_name/1 | whereis_name(name) :: pid OR whereis_name(name) :: undefined | The process name is created inside the worker module - for example as GenServer.start_link(__MODULE__, :ok, [name: {:global, {:name, name}}] . It is then called as the term in {:global, {:name, name}} , i.e. {:name, name} . |
iex(16)> item = %{'name' => {:name, GenServer.DB.Worker, 7}, 'checkout-window' => %{'from' => Timex.now(), 'to' => Timex.shift(Timex.now, minutes: 10)}}
item = %{'name' => {:name, GenServer.DB.Worker, 7}, 'checkout-window' => %{'from' => Timex.now(), 'to' => Timex.shift(Timex.now, minutes: 10)}}
iex(19)> %{'checkout-windows' => [item]}
%{'checkout-windows' => [item]}
%{'checkout-windows' => [%{'checkout-window' => %{'from' => #<DateTime(2017-05-02T06:25:55.198733Z Etc/UTC)>,
'to' => #<DateTime(2017-05-02T06:35:55.198832Z Etc/UTC)>},
'name' => {:name, GenServer.DB.Worker, 7}}]}