Skip to content
Permalink
Browse files
Code for step 5
  • Loading branch information
akoutmos committed Jul 29, 2020
1 parent 49c0c7d commit eef747e8ad82bf342ea749db0111dedd84e44958
Showing 2 changed files with 34 additions and 0 deletions.
@@ -15,6 +15,8 @@ defmodule BookStore.Application do
{Phoenix.PubSub, name: BookStore.PubSub},
# Start the Book Store Registry
BookStore.BookRegistry.child_spec(),
# Start the Book DynamicSupervisor
BookStore.BookDynamicSupervisor,
# Start the Endpoint (http/https)
BookStoreWeb.Endpoint
# Start a worker by calling: BookStore.Worker.start_link(arg)
@@ -0,0 +1,32 @@
defmodule BookStore.BookDynamicSupervisor do
use DynamicSupervisor

alias BookStore.Books.{Book, BookProcess}

def start_link(opts) do
DynamicSupervisor.start_link(__MODULE__, opts, name: __MODULE__)
end

@impl true
def init(_opts) do
DynamicSupervisor.init(strategy: :one_for_one)
end

def add_book_to_supervisor(%Book{} = book) do
child_spec = %{
id: BookProcess,
start: {BookProcess, :start_link, [book]},
restart: :transient
}

{:ok, _pid} = DynamicSupervisor.start_child(__MODULE__, child_spec)
end

def all_book_pids do
__MODULE__
|> DynamicSupervisor.which_children()
|> Enum.reduce([], fn {_, book_pid, _, _}, acc ->
[book_pid | acc]
end)
end
end

0 comments on commit eef747e

Please sign in to comment.