From 8e2e95a8e42e9bea34fd8cac5bde7b26aaa74ac4 Mon Sep 17 00:00:00 2001 From: Marco Milanesi Date: Tue, 6 Feb 2018 21:22:34 +0100 Subject: [PATCH] Use DynamicSupervisor --- lib/tweets_supervisor.ex | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/lib/tweets_supervisor.ex b/lib/tweets_supervisor.ex index 68ec6d3..879166b 100644 --- a/lib/tweets_supervisor.ex +++ b/lib/tweets_supervisor.ex @@ -1,25 +1,18 @@ defmodule Tweetyodel.Worker.Supervisor do - use Supervisor + use DynamicSupervisor def start_link do # We are now registering our supervisor process with a name # so we can reference it in the `start_tweet/1` function - Supervisor.start_link(__MODULE__, [], name: :tweet_supervisor) + DynamicSupervisor.start_link(__MODULE__, [], name: :tweet_supervisor) end def start_tweet(name) do - Supervisor.start_child(:tweet_supervisor, [name]) + spec = Supervisor.Spec.worker(Tweetyodel.Worker, [name]) + DynamicSupervisor.start_child(:tweet_supervisor, spec) end def init(_) do - children = [ - worker(Tweetyodel.Worker, []) - ] - - # We also changed the `strategy` to `simple_one_for_one`. - # With this strategy, we define just a "template" for a child, - # no process is started during the Supervisor initialization, - # just when we call `start_child/2` - supervise(children, strategy: :simple_one_for_one) + DynamicSupervisor.init(strategy: :one_for_one) end end