Skip to content

Commit

Permalink
core: ensure that lager metadata is set for spawned processes and que…
Browse files Browse the repository at this point in the history
…ries (#3167)
  • Loading branch information
mworrell committed Nov 3, 2022
1 parent 0150017 commit f8e82b9
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/db/z_db.erl
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ with_connection(F, none, _Context) ->
with_connection(F, Connection, Context) when is_pid(Connection) ->
exometer:update([zotonic, z_context:site(Context), db, requests], 1),
try
case lager:md() of
[] -> z_context:lager_md(Context);
_ -> ok
end,
{Time, Result} = timer:tc(F, [Connection]),
exometer:update([zotonic, z_context:site(Context), db, duration], Time),
Result
Expand Down
3 changes: 2 additions & 1 deletion src/support/z_datamodel.erl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ manage(Module, Datamodel, Context) ->
%% @doc Install / update a set of named, predefined resources, categories, predicates, media and edges.
-spec manage(atom(), #datamodel{}, datamodel_options(), #context{}) -> ok.
manage(Module, Datamodel, Options, Context) ->
lager:info("~p: installing datamodel for ~p", [ z_context:site(Context), Module ]),
F = fun(Ctx) ->
[manage_category(Module, Cat, Options, Ctx) || Cat <- Datamodel#datamodel.categories],
[manage_predicate(Module, Pred, Options, Ctx) || Pred <- Datamodel#datamodel.predicates],
Expand Down Expand Up @@ -228,7 +229,7 @@ update_new_props(Module, Id, NewProps, Options, Context) ->
end.


maybe_force_update(K, V, Props, Module, Id, Options, Context) ->
maybe_force_update(K, V, Props, Module, Id, Options, _Context) ->
case lists:member(force_update, Options) of
true ->
lager:info("~p: ~p of ~p changed in database, forced update.", [Module, K, Id]),
Expand Down
4 changes: 4 additions & 0 deletions src/support/z_file_entry.erl
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ lookup(RequestPath, Context) when is_binary(RequestPath) ->
%%% ------------------------------------------------------------------------------------

init([InitialState, RequestPath, Root, OptFilterProps, Minify, Site]) ->
lager:md([
{site, Site},
{module, ?MODULE}
]),
State = #state{
site=Site,
request_path=RequestPath,
Expand Down
3 changes: 2 additions & 1 deletion src/support/z_module_manager.erl
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,8 @@ is_module(Module) ->
start_child(ManagerPid, Module, ModuleSup, Spec, Exports, Context) ->
StartPid = spawn_link(
fun() ->
Result = case catch manage_schema(Module, Exports, Context) of
z_context:lager_md(Context),
Result = case catch manage_schema(Module, Exports, Context) of
ok ->
% Try to start it
z_supervisor:start_child(ModuleSup, Spec#child_spec.name, ?MODULE_START_TIMEOUT);
Expand Down
11 changes: 9 additions & 2 deletions src/support/z_notifier.erl
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,10 @@ notify(Msg, #context{dbc = undefined} = Context) ->
case get_observers(Msg, Context) of
[] -> ok;
Observers ->
MD = lager:md(),
F = fun() ->
lists:foreach(fun(Obs) -> notify_observer(Msg, Obs, false, Context) end, Observers)
lager:md(MD),
lists:foreach(fun(Obs) -> notify_observer(Msg, Obs, false, Context) end, Observers)
end,
spawn(F),
ok
Expand All @@ -204,7 +206,11 @@ notify1(Msg, #context{dbc = undefined} = Context) ->
case get_observers(Msg, Context) of
[] -> ok;
[Obs|_] ->
F = fun() -> notify_observer(Msg, Obs, false, Context) end,
MD = lager:md(),
F = fun() ->
lager:md(MD),
notify_observer(Msg, Obs, false, Context)
end,
spawn(F)
end;
notify1(Msg, _Context) ->
Expand Down Expand Up @@ -416,6 +422,7 @@ handle_info({tick, Msg}, #state{host=Host} = State) ->
case catch z_context:new(Host) of
#context{} = Context ->
spawn(fun() ->
z_context:lager_md(Context),
?MODULE:notify(Msg, Context)
end);
_ ->
Expand Down

0 comments on commit f8e82b9

Please sign in to comment.