Skip to content
Permalink
Browse files
Code for step 8
  • Loading branch information
akoutmos committed Jul 29, 2020
1 parent 843d645 commit 2d25f312784c19a277aa3cac97dba683fc56f34e
Showing 2 changed files with 28 additions and 0 deletions.
@@ -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)
@@ -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.