Skip to content

Commit 331219f

Browse files
authored
Normalize logs and some clean up (#417)
1 parent 5c6d069 commit 331219f

13 files changed

+53
-39
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ beacon-*.tar
4343

4444
# LSP
4545
/.elixir_ls
46+
/.elixir-tools
4647

4748
# Local iex config
4849
.iex.exs

lib/beacon/content/snippets/tag_helper.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ defmodule Beacon.Content.Snippets.TagHelper do
2828
text =
2929
site
3030
|> Beacon.Loader.snippet_helpers_module_for_site()
31-
|> Beacon.Loader.call_function_with_retry(helper_name, [context.counter_vars])
31+
|> Beacon.Loader.call_function_with_retry!(helper_name, [context.counter_vars])
3232
|> to_string()
3333

3434
{[text: text], context}

lib/beacon/loader.ex

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ defmodule Beacon.Loader do
4646
def handle_continue(:load_site_from_db, config) do
4747
# avoid compilation warnings
4848
populate_default_components(nil)
49-
50-
# this module is required to render page_live#handle_params
51-
:ok = load_data_source(config.site)
52-
5349
subscribe_to_events(config.site)
5450

5551
{:noreply, config}
@@ -187,10 +183,10 @@ defmodule Beacon.Loader do
187183
rescue
188184
e ->
189185
if failure_count >= 3 do
190-
Logger.debug("failed to load module #{inspect(module)} after #{failure_count} tries.")
186+
Logger.debug("failed to load module #{inspect(module)} after #{failure_count} tries")
191187

192188
message = """
193-
failed to load module #{inspect(module)}
189+
failed to load module #{inspect(module)} after #{failure_count} tries
194190
195191
Got:
196192
@@ -200,7 +196,7 @@ defmodule Beacon.Loader do
200196

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

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

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

340338
_ ->
341339
reraise e, __STACKTRACE__
342340
end
343341

344342
_e in FunctionClauseError ->
343+
mfa = Exception.format_mfa(module, function, length(args))
344+
345345
error_message = """
346-
could not call #{function} for the given path: #{inspect(List.flatten(args))}.
346+
could not call #{mfa} for the given path: #{inspect(List.flatten(args))}.
347347
348348
Make sure you have created a page for this path.
349349

lib/beacon/loader/component_module_loader.ex

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
defmodule Beacon.Loader.ComponentModuleLoader do
22
@moduledoc false
33

4-
require Logger
5-
64
alias Beacon.Content
75
alias Beacon.Loader
86

lib/beacon/loader/data_source_module_loader.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ defmodule Beacon.Loader.DataSourceModuleLoader do
2323
unquote_splicing(live_data_functions)
2424

2525
def live_data(path, params, data) do
26-
Logger.warning("Unhandled Beacon Live Data request for site #{unquote(site)} with path #{inspect(path)} and params #{inspect(params)}")
26+
Logger.warning("live data not found for site #{unquote(site)} with path #{inspect(path)} and params #{inspect(params)}")
2727
data
2828
end
2929
end

lib/beacon/loader/layout_module_loader.ex

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
defmodule Beacon.Loader.LayoutModuleLoader do
22
@moduledoc false
33

4-
require Logger
5-
64
alias Beacon.Content
75
alias Beacon.Loader
86

lib/beacon/loader/page_module_loader.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ defmodule Beacon.Loader.PageModuleLoader do
241241
defp dynamic_helper do
242242
quote do
243243
def dynamic_helper(helper_name, args) do
244-
Loader.call_function_with_retry(__MODULE__, String.to_atom(helper_name), [args])
244+
Loader.call_function_with_retry!(__MODULE__, String.to_atom(helper_name), [args])
245245
end
246246
end
247247
end

lib/beacon/loader/snippet_module_loader.ex

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
defmodule Beacon.Loader.SnippetModuleLoader do
22
@moduledoc false
33

4-
require Logger
5-
64
alias Beacon.Loader
75

86
def load_helpers(_site, [] = _helpers) do

lib/beacon/loader/stylesheet_module_loader.ex

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
defmodule Beacon.Loader.StylesheetModuleLoader do
22
@moduledoc false
33

4-
require Logger
5-
64
alias Beacon.Content
75

86
def load_stylesheets(_site, [] = _stylesheets) do

lib/beacon/pub_sub.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
defmodule Beacon.PubSub do
22
@moduledoc false
33

4-
require Logger
54
alias Beacon.Content.Component
65
alias Beacon.Content.ErrorPage
76
alias Beacon.Content.Layout

lib/beacon_web/components/layouts.ex

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ defmodule BeaconWeb.Layouts do
3030
def render_dynamic_layout(%{__dynamic_layout_id__: layout_id} = assigns) do
3131
layout_id
3232
|> Beacon.Loader.layout_module_for_site()
33-
|> Beacon.Loader.call_function_with_retry(:render, [assigns])
33+
|> Beacon.Loader.call_function_with_retry!(:render, [assigns])
3434
end
3535

3636
def live_socket_path(%{__site__: site}) do
@@ -40,13 +40,13 @@ defmodule BeaconWeb.Layouts do
4040
defp compiled_page_assigns(page_id) do
4141
page_id
4242
|> Beacon.Loader.page_module_for_site()
43-
|> Beacon.Loader.call_function_with_retry(:page_assigns, [])
43+
|> Beacon.Loader.call_function_with_retry!(:page_assigns, [])
4444
end
4545

4646
defp compiled_layout_assigns(layout_id) do
4747
layout_id
4848
|> Beacon.Loader.layout_module_for_site()
49-
|> Beacon.Loader.call_function_with_retry(:layout_assigns, [])
49+
|> Beacon.Loader.call_function_with_retry!(:layout_assigns, [])
5050
end
5151

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

6262
if page_title do
6363
page_title
6464
else
6565
%{title: layout_title} =
6666
layout_id
6767
|> Beacon.Loader.layout_module_for_site()
68-
|> Beacon.Loader.call_function_with_retry(:layout_assigns, [])
68+
|> Beacon.Loader.call_function_with_retry!(:layout_assigns, [])
6969

7070
layout_title || missing_page_title()
7171
end
@@ -74,7 +74,7 @@ defmodule BeaconWeb.Layouts do
7474
def page_title(_), do: missing_page_title()
7575

7676
defp missing_page_title do
77-
Logger.warning("No page title set")
77+
Logger.warning("no page title was found")
7878
""
7979
end
8080

@@ -129,7 +129,7 @@ defmodule BeaconWeb.Layouts do
129129
%{raw_schema: raw_schema} =
130130
page_id
131131
|> Beacon.Loader.page_module_for_site()
132-
|> Beacon.Loader.call_function_with_retry(:page_assigns, [])
132+
|> Beacon.Loader.call_function_with_retry!(:page_assigns, [])
133133

134134
is_empty = fn raw_schema ->
135135
raw_schema |> Enum.map(&Map.values/1) |> List.flatten() == []

lib/beacon_web/data_source.ex

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,27 @@ defmodule BeaconWeb.DataSource do
33

44
require Logger
55

6+
def live_data(site, path, params) when is_atom(site) do
7+
data_source_module = Beacon.Loader.data_source_module_for_site(site)
8+
9+
if :erlang.module_loaded(data_source_module) do
10+
data_source_module.live_data(path, params)
11+
else
12+
Logger.warning("""
13+
data source module #{data_source_module} for site #{site} and path #{path} is not loaded
14+
15+
returning empty live data for that page
16+
""")
17+
18+
%{}
19+
end
20+
end
21+
622
def page_title(assigns) do
723
page =
824
assigns.__dynamic_page_id__
925
|> Beacon.Loader.page_module_for_site()
10-
|> Beacon.Loader.call_function_with_retry(:page_assigns, [])
26+
|> Beacon.Loader.call_function_with_retry!(:page_assigns, [])
1127

1228
title = BeaconWeb.Layouts.page_title(assigns)
1329

@@ -34,7 +50,7 @@ defmodule BeaconWeb.DataSource do
3450
page =
3551
assigns.__dynamic_page_id__
3652
|> Beacon.Loader.page_module_for_site()
37-
|> Beacon.Loader.call_function_with_retry(:page_assigns, [])
53+
|> Beacon.Loader.call_function_with_retry!(:page_assigns, [])
3854

3955
assigns
4056
|> BeaconWeb.Layouts.meta_tags()

lib/beacon_web/live/page_live.ex

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ defmodule BeaconWeb.PageLive do
3333
defp lookup_route!(site, path) do
3434
Beacon.Router.lookup_path(site, path) ||
3535
raise BeaconWeb.NotFoundError, """
36-
route not found for path #{inspect(path)}
36+
route not found for site #{site} and path #{inspect(path)}
3737
3838
Make sure a page was created for that path.
3939
"""
@@ -54,13 +54,20 @@ defmodule BeaconWeb.PageLive do
5454
{:noreply, socket}
5555
end
5656

57-
def handle_info(_msg, socket) do
57+
def handle_info(msg, socket) do
58+
Logger.warning("""
59+
unhandled message:
60+
61+
#{inspect(msg)}
62+
63+
""")
64+
5865
{:noreply, socket}
5966
end
6067

6168
def handle_event(event_name, event_params, socket) do
6269
socket.assigns.__beacon_page_module__
63-
|> Beacon.Loader.call_function_with_retry(
70+
|> Beacon.Loader.call_function_with_retry!(
6471
:handle_event,
6572
[event_name, event_params, socket]
6673
)
@@ -77,8 +84,7 @@ defmodule BeaconWeb.PageLive do
7784
%{"path" => path} = params
7885
%{__site__: site} = socket.assigns
7986

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

8490
Process.put(:__beacon_site__, site)

0 commit comments

Comments
 (0)