Skip to content

Commit eef747e

Browse files
committed
Code for step 5
1 parent 49c0c7d commit eef747e

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

lib/book_store/application.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ defmodule BookStore.Application do
1515
{Phoenix.PubSub, name: BookStore.PubSub},
1616
# Start the Book Store Registry
1717
BookStore.BookRegistry.child_spec(),
18+
# Start the Book DynamicSupervisor
19+
BookStore.BookDynamicSupervisor,
1820
# Start the Endpoint (http/https)
1921
BookStoreWeb.Endpoint
2022
# Start a worker by calling: BookStore.Worker.start_link(arg)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
defmodule BookStore.BookDynamicSupervisor do
2+
use DynamicSupervisor
3+
4+
alias BookStore.Books.{Book, BookProcess}
5+
6+
def start_link(opts) do
7+
DynamicSupervisor.start_link(__MODULE__, opts, name: __MODULE__)
8+
end
9+
10+
@impl true
11+
def init(_opts) do
12+
DynamicSupervisor.init(strategy: :one_for_one)
13+
end
14+
15+
def add_book_to_supervisor(%Book{} = book) do
16+
child_spec = %{
17+
id: BookProcess,
18+
start: {BookProcess, :start_link, [book]},
19+
restart: :transient
20+
}
21+
22+
{:ok, _pid} = DynamicSupervisor.start_child(__MODULE__, child_spec)
23+
end
24+
25+
def all_book_pids do
26+
__MODULE__
27+
|> DynamicSupervisor.which_children()
28+
|> Enum.reduce([], fn {_, book_pid, _, _}, acc ->
29+
[book_pid | acc]
30+
end)
31+
end
32+
end

0 commit comments

Comments
 (0)