Skip to content

Commit 2d25f31

Browse files
committed
Code for step 8
1 parent 843d645 commit 2d25f31

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

lib/book_store/application.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ defmodule BookStore.Application do
1717
BookStore.BookRegistry.child_spec(),
1818
# Start the Book DynamicSupervisor
1919
BookStore.BookDynamicSupervisor,
20+
# Hydrate process state
21+
BookStore.BookStateHydrator,
2022
# Start the Endpoint (http/https)
2123
BookStoreWeb.Endpoint
2224
# Start a worker by calling: BookStore.Worker.start_link(arg)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
defmodule BookStore.BookStateHydrator do
2+
use GenServer, restart: :transient
3+
4+
require Logger
5+
6+
alias BookStore.Books.Book
7+
alias BookStore.BookDynamicSupervisor
8+
9+
def start_link(state) do
10+
GenServer.start_link(__MODULE__, state, name: __MODULE__, timeout: 10_000)
11+
end
12+
13+
@impl true
14+
def init(_) do
15+
Logger.info("#{inspect(Time.utc_now())} Starting Books process hydration")
16+
17+
BookStore.Books.all()
18+
|> Enum.each(fn %Book{} = book ->
19+
BookDynamicSupervisor.add_book_to_supervisor(book)
20+
end)
21+
22+
Logger.info("#{inspect(Time.utc_now())} Completed Books process hydration")
23+
24+
:ignore
25+
end
26+
end

0 commit comments

Comments
 (0)