Permalink
Browse files

dynamic worker_spec definition

  • Loading branch information...
davidsulc committed Sep 8, 2018
1 parent b2e3c20 commit da3532092cb75654e288a7080e5ac9c15a993c71
Showing with 11 additions and 3 deletions.
  1. +1 −1 lib/pool_toy/application.ex
  2. +10 −2 lib/pool_toy/pool_man.ex
@@ -3,7 +3,7 @@ defmodule PoolToy.Application do
def start(_type, _args) do
children = [
{PoolToy.PoolSup, [name: :poolio, size: 3]}
{PoolToy.PoolSup, [name: :poolio, worker_spec: Doubler, size: 3]}
]
opts = [strategy: :one_for_one]
View
@@ -3,9 +3,9 @@ defmodule PoolToy.PoolMan do
defmodule State do
defstruct [
:name, :size, :pool_sup,
:name, :size, :worker_spec, :pool_sup,
:monitors, :worker_sup,
worker_spec: Doubler, workers: []
workers: []
]
end
@@ -44,6 +44,10 @@ defmodule PoolToy.PoolMan do
init(rest, %{state | pool_sup: pid})
end
defp init([{:worker_spec, spec} | rest], %State{} = state) do
init(rest, %{state | worker_spec: spec})
end
defp init([{:pool_sup, _} | _], _state) do
{:stop, {:invalid_args, {:pool_sup, "must be provided"}}}
end
@@ -56,6 +60,10 @@ defmodule PoolToy.PoolMan do
{:stop, {:missing_args, {:size, "positive integer `size` is required"}}}
end
defp init([], %State{worker_spec: nil}) do
{:stop, {:missing_args, {:worker_spec, "child spec `worker_spec` is required"}}}
end
defp init([], %State{name: _, size: _} = state) do
Process.flag(:trap_exit, true)
send(self(), :start_worker_sup)

0 comments on commit da35320

Please sign in to comment.