Skip to content

Commit

Permalink
Normalize logs and some clean up (#417)
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrocp authored Feb 16, 2024
1 parent 5c6d069 commit 331219f
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 39 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ beacon-*.tar

# LSP
/.elixir_ls
/.elixir-tools

# Local iex config
.iex.exs
2 changes: 1 addition & 1 deletion lib/beacon/content/snippets/tag_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ defmodule Beacon.Content.Snippets.TagHelper do
text =
site
|> Beacon.Loader.snippet_helpers_module_for_site()
|> Beacon.Loader.call_function_with_retry(helper_name, [context.counter_vars])
|> Beacon.Loader.call_function_with_retry!(helper_name, [context.counter_vars])
|> to_string()

{[text: text], context}
Expand Down
26 changes: 13 additions & 13 deletions lib/beacon/loader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ defmodule Beacon.Loader do
def handle_continue(:load_site_from_db, config) do
# avoid compilation warnings
populate_default_components(nil)

# this module is required to render page_live#handle_params
:ok = load_data_source(config.site)

subscribe_to_events(config.site)

{:noreply, config}
Expand Down Expand Up @@ -187,10 +183,10 @@ defmodule Beacon.Loader do
rescue
e ->
if failure_count >= 3 do
Logger.debug("failed to load module #{inspect(module)} after #{failure_count} tries.")
Logger.debug("failed to load module #{inspect(module)} after #{failure_count} tries")

message = """
failed to load module #{inspect(module)}
failed to load module #{inspect(module)} after #{failure_count} tries
Got:
Expand All @@ -200,7 +196,7 @@ defmodule Beacon.Loader do

reraise Beacon.LoaderError, [message: message], __STACKTRACE__
else
Logger.debug("failed to load module #{inspect(module)}, retrying...")
Logger.debug("failed to load module #{inspect(module)} for the #{failure_count + 1}, retrying...")
:timer.sleep(100 * (failure_count * 2))
reload_module!(module, ast, file, failure_count + 1)
end
Expand Down Expand Up @@ -323,27 +319,31 @@ defmodule Beacon.Loader do

# This retry logic exists because a module may be in the process of being reloaded, in which case we want to retry
@doc false
def call_function_with_retry(module, function, args, failure_count \\ 0) do
def call_function_with_retry!(module, function, args, failure_count \\ 0) when is_atom(module) and is_atom(function) and is_list(args) do
apply(module, function, args)
rescue
e in UndefinedFunctionError ->
case {failure_count, e} do
{x, _} when x >= 10 ->
Logger.debug("failed to call #{inspect(module)} #{inspect(function)} after #{failure_count} tries.")
mfa = Exception.format_mfa(module, function, length(args))
Logger.debug("failed to call #{mfa} after #{failure_count} tries")
reraise e, __STACKTRACE__

{_, %UndefinedFunctionError{function: ^function, module: ^module}} ->
Logger.debug("failed to call #{inspect(module)} #{inspect(function)} with #{inspect(args)} for the #{failure_count + 1} time, retrying...")
{_, %UndefinedFunctionError{module: ^module, function: ^function}} ->
mfa = Exception.format_mfa(module, function, length(args))
Logger.debug("failed to call #{mfa} for the #{failure_count + 1} time, retrying...")
:timer.sleep(100 * (failure_count * 2))
call_function_with_retry(module, function, args, failure_count + 1)
call_function_with_retry!(module, function, args, failure_count + 1)

_ ->
reraise e, __STACKTRACE__
end

_e in FunctionClauseError ->
mfa = Exception.format_mfa(module, function, length(args))

error_message = """
could not call #{function} for the given path: #{inspect(List.flatten(args))}.
could not call #{mfa} for the given path: #{inspect(List.flatten(args))}.
Make sure you have created a page for this path.
Expand Down
2 changes: 0 additions & 2 deletions lib/beacon/loader/component_module_loader.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
defmodule Beacon.Loader.ComponentModuleLoader do
@moduledoc false

require Logger

alias Beacon.Content
alias Beacon.Loader

Expand Down
2 changes: 1 addition & 1 deletion lib/beacon/loader/data_source_module_loader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ defmodule Beacon.Loader.DataSourceModuleLoader do
unquote_splicing(live_data_functions)

def live_data(path, params, data) do
Logger.warning("Unhandled Beacon Live Data request for site #{unquote(site)} with path #{inspect(path)} and params #{inspect(params)}")
Logger.warning("live data not found for site #{unquote(site)} with path #{inspect(path)} and params #{inspect(params)}")
data
end
end
Expand Down
2 changes: 0 additions & 2 deletions lib/beacon/loader/layout_module_loader.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
defmodule Beacon.Loader.LayoutModuleLoader do
@moduledoc false

require Logger

alias Beacon.Content
alias Beacon.Loader

Expand Down
2 changes: 1 addition & 1 deletion lib/beacon/loader/page_module_loader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ defmodule Beacon.Loader.PageModuleLoader do
defp dynamic_helper do
quote do
def dynamic_helper(helper_name, args) do
Loader.call_function_with_retry(__MODULE__, String.to_atom(helper_name), [args])
Loader.call_function_with_retry!(__MODULE__, String.to_atom(helper_name), [args])
end
end
end
Expand Down
2 changes: 0 additions & 2 deletions lib/beacon/loader/snippet_module_loader.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
defmodule Beacon.Loader.SnippetModuleLoader do
@moduledoc false

require Logger

alias Beacon.Loader

def load_helpers(_site, [] = _helpers) do
Expand Down
2 changes: 0 additions & 2 deletions lib/beacon/loader/stylesheet_module_loader.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
defmodule Beacon.Loader.StylesheetModuleLoader do
@moduledoc false

require Logger

alias Beacon.Content

def load_stylesheets(_site, [] = _stylesheets) do
Expand Down
1 change: 0 additions & 1 deletion lib/beacon/pub_sub.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
defmodule Beacon.PubSub do
@moduledoc false

require Logger
alias Beacon.Content.Component
alias Beacon.Content.ErrorPage
alias Beacon.Content.Layout
Expand Down
14 changes: 7 additions & 7 deletions lib/beacon_web/components/layouts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ defmodule BeaconWeb.Layouts do
def render_dynamic_layout(%{__dynamic_layout_id__: layout_id} = assigns) do
layout_id
|> Beacon.Loader.layout_module_for_site()
|> Beacon.Loader.call_function_with_retry(:render, [assigns])
|> Beacon.Loader.call_function_with_retry!(:render, [assigns])
end

def live_socket_path(%{__site__: site}) do
Expand All @@ -40,13 +40,13 @@ defmodule BeaconWeb.Layouts do
defp compiled_page_assigns(page_id) do
page_id
|> Beacon.Loader.page_module_for_site()
|> Beacon.Loader.call_function_with_retry(:page_assigns, [])
|> Beacon.Loader.call_function_with_retry!(:page_assigns, [])
end

defp compiled_layout_assigns(layout_id) do
layout_id
|> Beacon.Loader.layout_module_for_site()
|> Beacon.Loader.call_function_with_retry(:layout_assigns, [])
|> Beacon.Loader.call_function_with_retry!(:layout_assigns, [])
end

def render_page_title(assigns) do
Expand All @@ -57,15 +57,15 @@ defmodule BeaconWeb.Layouts do
%{title: page_title} =
page_id
|> Beacon.Loader.page_module_for_site()
|> Beacon.Loader.call_function_with_retry(:page_assigns, [])
|> Beacon.Loader.call_function_with_retry!(:page_assigns, [])

if page_title do
page_title
else
%{title: layout_title} =
layout_id
|> Beacon.Loader.layout_module_for_site()
|> Beacon.Loader.call_function_with_retry(:layout_assigns, [])
|> Beacon.Loader.call_function_with_retry!(:layout_assigns, [])

layout_title || missing_page_title()
end
Expand All @@ -74,7 +74,7 @@ defmodule BeaconWeb.Layouts do
def page_title(_), do: missing_page_title()

defp missing_page_title do
Logger.warning("No page title set")
Logger.warning("no page title was found")
""
end

Expand Down Expand Up @@ -129,7 +129,7 @@ defmodule BeaconWeb.Layouts do
%{raw_schema: raw_schema} =
page_id
|> Beacon.Loader.page_module_for_site()
|> Beacon.Loader.call_function_with_retry(:page_assigns, [])
|> Beacon.Loader.call_function_with_retry!(:page_assigns, [])

is_empty = fn raw_schema ->
raw_schema |> Enum.map(&Map.values/1) |> List.flatten() == []
Expand Down
20 changes: 18 additions & 2 deletions lib/beacon_web/data_source.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,27 @@ defmodule BeaconWeb.DataSource do

require Logger

def live_data(site, path, params) when is_atom(site) do
data_source_module = Beacon.Loader.data_source_module_for_site(site)

if :erlang.module_loaded(data_source_module) do
data_source_module.live_data(path, params)
else
Logger.warning("""
data source module #{data_source_module} for site #{site} and path #{path} is not loaded
returning empty live data for that page
""")

%{}
end
end

def page_title(assigns) do
page =
assigns.__dynamic_page_id__
|> Beacon.Loader.page_module_for_site()
|> Beacon.Loader.call_function_with_retry(:page_assigns, [])
|> Beacon.Loader.call_function_with_retry!(:page_assigns, [])

title = BeaconWeb.Layouts.page_title(assigns)

Expand All @@ -34,7 +50,7 @@ defmodule BeaconWeb.DataSource do
page =
assigns.__dynamic_page_id__
|> Beacon.Loader.page_module_for_site()
|> Beacon.Loader.call_function_with_retry(:page_assigns, [])
|> Beacon.Loader.call_function_with_retry!(:page_assigns, [])

assigns
|> BeaconWeb.Layouts.meta_tags()
Expand Down
16 changes: 11 additions & 5 deletions lib/beacon_web/live/page_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule BeaconWeb.PageLive do
defp lookup_route!(site, path) do
Beacon.Router.lookup_path(site, path) ||
raise BeaconWeb.NotFoundError, """
route not found for path #{inspect(path)}
route not found for site #{site} and path #{inspect(path)}
Make sure a page was created for that path.
"""
Expand All @@ -54,13 +54,20 @@ defmodule BeaconWeb.PageLive do
{:noreply, socket}
end

def handle_info(_msg, socket) do
def handle_info(msg, socket) do
Logger.warning("""
unhandled message:
#{inspect(msg)}
""")

{:noreply, socket}
end

def handle_event(event_name, event_params, socket) do
socket.assigns.__beacon_page_module__
|> Beacon.Loader.call_function_with_retry(
|> Beacon.Loader.call_function_with_retry!(
:handle_event,
[event_name, event_params, socket]
)
Expand All @@ -77,8 +84,7 @@ defmodule BeaconWeb.PageLive do
%{"path" => path} = params
%{__site__: site} = socket.assigns

data_source_module = Beacon.Loader.data_source_module_for_site(site)
live_data = data_source_module.live_data(path, Map.drop(params, ["path"]))
live_data = BeaconWeb.DataSource.live_data(site, path, Map.drop(params, ["path"]))
{{_site, beacon_page_path}, {page_id, layout_id, _format, page_module, component_module}} = lookup_route!(site, path)

Process.put(:__beacon_site__, site)
Expand Down

0 comments on commit 331219f

Please sign in to comment.