Skip to content

Commit

Permalink
Load site asyncly (#400)
Browse files Browse the repository at this point in the history
* Load site asyncly

* hide init/1

* increase timeout for compiling stylesheets
  • Loading branch information
leandrocp authored Jan 30, 2024
1 parent 8a48c2d commit 70c6add
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
53 changes: 27 additions & 26 deletions lib/beacon/loader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,44 +35,45 @@ defmodule Beacon.Loader do
Beacon.Registry.via({site, __MODULE__})
end

@doc false
def init(config) do
{:ok, config, {:continue, :load_site_from_db}}
end

if Code.ensure_loaded?(Mix.Project) and Mix.env() == :test do
@doc false
def init(config) do
%{site: site} = config

def handle_continue(:load_site_from_db, config) do
# avoid compilation warnings
populate_components(nil)

PubSub.subscribe_to_layouts(site)
PubSub.subscribe_to_pages(site)
PubSub.subscribe_to_components(site)
PubSub.subscribe_to_error_pages(site)

{:ok, config}
populate_default_components(nil)
subscribe_to_events(config.site)
{:noreply, config}
end
else
@doc false
def init(config) do
def handle_continue(:load_site_from_db, config) do
%{site: site} = config

with :ok <- populate_components(site),
:ok <- populate_layouts(site),
:ok <- populate_error_pages(site) do
with :ok <- populate_default_components(site),
:ok <- populate_default_layouts(site),
:ok <- populate_default_error_pages(site) do
:ok = load_site_from_db(site)
subscribe_to_events(site)
end

PubSub.subscribe_to_layouts(site)
PubSub.subscribe_to_pages(site)
PubSub.subscribe_to_components(site)
PubSub.subscribe_to_error_pages(site)

{:ok, config}
{:noreply, config}
end
end

defp populate_components(nil), do: :skip
defp subscribe_to_events(site) do
PubSub.subscribe_to_layouts(site)
PubSub.subscribe_to_pages(site)
PubSub.subscribe_to_components(site)
PubSub.subscribe_to_error_pages(site)
end

defp populate_default_components(nil), do: :skip

defp populate_components(site) do
defp populate_default_components(site) do
for attrs <- Content.blueprint_components() do
case Content.list_components_by_name(site, attrs.name) do
[] ->
Expand All @@ -89,7 +90,7 @@ defmodule Beacon.Loader do
end

@doc false
def populate_layouts(site) do
def populate_default_layouts(site) do
case Content.get_layout_by(site, title: "Default") do
nil ->
Content.default_layout()
Expand All @@ -105,7 +106,7 @@ defmodule Beacon.Loader do
end

@doc false
def populate_error_pages(site) do
def populate_default_error_pages(site) do
default_layout = Content.get_layout_by(site, title: "Default")

for attrs <- Content.default_error_pages() do
Expand Down Expand Up @@ -250,7 +251,7 @@ defmodule Beacon.Loader do
:ok
end)
end)
|> Task.await_many(300_000)
|> Task.await_many(60_000)

:ok
end
Expand Down
2 changes: 1 addition & 1 deletion lib/beacon/tailwind_compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ defmodule Beacon.TailwindCompiler do
end)
end)
]
|> Task.await_many()
|> Task.await_many(60_000)
|> List.flatten()
end

Expand Down
4 changes: 2 additions & 2 deletions test/beacon/loader/error_page_module_loader_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ defmodule Beacon.Loader.ErrorPageModuleLoaderTest do
end

setup %{conn: conn} do
:ok = Beacon.Loader.populate_layouts(@site)
:ok = Beacon.Loader.populate_error_pages(@site)
:ok = Beacon.Loader.populate_default_layouts(@site)
:ok = Beacon.Loader.populate_default_error_pages(@site)
error_module = load_error_pages_module(@site)

[conn: build_conn(conn), error_module: error_module]
Expand Down

0 comments on commit 70c6add

Please sign in to comment.