Skip to content

Commit

Permalink
Code for step 8
Browse files Browse the repository at this point in the history
  • Loading branch information
akoutmos committed Jul 29, 2020
1 parent 843d645 commit 2d25f31
Show file tree
Hide file tree
Showing 2 changed files with 28 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 @@ -17,6 +17,8 @@ defmodule BookStore.Application do
BookStore.BookRegistry.child_spec(),
# Start the Book DynamicSupervisor
BookStore.BookDynamicSupervisor,
# Hydrate process state
BookStore.BookStateHydrator,
# Start the Endpoint (http/https)
BookStoreWeb.Endpoint
# Start a worker by calling: BookStore.Worker.start_link(arg)
Expand Down
26 changes: 26 additions & 0 deletions lib/book_store/book_state_hydrator.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule BookStore.BookStateHydrator do
use GenServer, restart: :transient

require Logger

alias BookStore.Books.Book
alias BookStore.BookDynamicSupervisor

def start_link(state) do
GenServer.start_link(__MODULE__, state, name: __MODULE__, timeout: 10_000)
end

@impl true
def init(_) do
Logger.info("#{inspect(Time.utc_now())} Starting Books process hydration")

BookStore.Books.all()
|> Enum.each(fn %Book{} = book ->
BookDynamicSupervisor.add_book_to_supervisor(book)
end)

Logger.info("#{inspect(Time.utc_now())} Completed Books process hydration")

:ignore
end
end

0 comments on commit 2d25f31

Please sign in to comment.