-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |