Skip to content

Commit

Permalink
Code for step 5
Browse files Browse the repository at this point in the history
  • Loading branch information
akoutmos committed Jul 29, 2020
1 parent 49c0c7d commit eef747e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/book_store/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions lib/book_store/book_dynamic_supervisor.ex
Original file line number Diff line number Diff line change
@@ -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.