From fa2f895d3eb7a42ec17f37bb747210234d138b2b Mon Sep 17 00:00:00 2001 From: Leandro Pereira Date: Tue, 30 Jan 2024 17:53:51 -0500 Subject: [PATCH 1/3] Load site asyncly --- lib/beacon/loader.ex | 52 +++++++++---------- lib/beacon/tailwind_compiler.ex | 2 +- .../loader/error_page_module_loader_test.exs | 4 +- 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/lib/beacon/loader.ex b/lib/beacon/loader.ex index 4764515c..9d5fb543 100644 --- a/lib/beacon/loader.ex +++ b/lib/beacon/loader.ex @@ -35,44 +35,44 @@ defmodule Beacon.Loader do Beacon.Registry.via({site, __MODULE__}) end + 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 [] -> @@ -89,7 +89,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() @@ -105,7 +105,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 @@ -250,7 +250,7 @@ defmodule Beacon.Loader do :ok end) end) - |> Task.await_many(300_000) + |> Task.await_many(60_000) :ok end diff --git a/lib/beacon/tailwind_compiler.ex b/lib/beacon/tailwind_compiler.ex index cc974e73..7fe47c6c 100644 --- a/lib/beacon/tailwind_compiler.ex +++ b/lib/beacon/tailwind_compiler.ex @@ -204,7 +204,7 @@ defmodule Beacon.TailwindCompiler do end) end) ] - |> Task.await_many() + |> Task.await_many(30_000) |> List.flatten() end diff --git a/test/beacon/loader/error_page_module_loader_test.exs b/test/beacon/loader/error_page_module_loader_test.exs index d64c8d14..5417ed19 100644 --- a/test/beacon/loader/error_page_module_loader_test.exs +++ b/test/beacon/loader/error_page_module_loader_test.exs @@ -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] From cb1909f2f2a757e30d1fe665a7e40b13c3a095af Mon Sep 17 00:00:00 2001 From: Leandro Pereira Date: Tue, 30 Jan 2024 18:05:10 -0500 Subject: [PATCH 2/3] hide init/1 --- lib/beacon/loader.ex | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/beacon/loader.ex b/lib/beacon/loader.ex index 9d5fb543..54741886 100644 --- a/lib/beacon/loader.ex +++ b/lib/beacon/loader.ex @@ -35,6 +35,7 @@ defmodule Beacon.Loader do Beacon.Registry.via({site, __MODULE__}) end + @doc false def init(config) do {:ok, config, {:continue, :load_site_from_db}} end From d2169965338bc5a477b239e0916464836d69c5f3 Mon Sep 17 00:00:00 2001 From: Leandro Pereira Date: Tue, 30 Jan 2024 18:06:04 -0500 Subject: [PATCH 3/3] increase timeout for compiling stylesheets --- lib/beacon/tailwind_compiler.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/beacon/tailwind_compiler.ex b/lib/beacon/tailwind_compiler.ex index 7fe47c6c..1633ebdf 100644 --- a/lib/beacon/tailwind_compiler.ex +++ b/lib/beacon/tailwind_compiler.ex @@ -204,7 +204,7 @@ defmodule Beacon.TailwindCompiler do end) end) ] - |> Task.await_many(30_000) + |> Task.await_many(60_000) |> List.flatten() end