Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use DynamicSupervisor #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions lib/tweets_supervisor.ex
Original file line number Diff line number Diff line change
@@ -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