From b16e680b6c9737afef889d8286750d4406f3740c Mon Sep 17 00:00:00 2001 From: Benjamin Milde Date: Fri, 8 Apr 2022 21:29:11 +0200 Subject: [PATCH] Replace error_logger handler with logger handler --- lib/shoehorn/application.ex | 2 +- lib/shoehorn/report_handler.ex | 95 +++++++++++++++++++--------------- 2 files changed, 55 insertions(+), 42 deletions(-) diff --git a/lib/shoehorn/application.ex b/lib/shoehorn/application.ex index e5d6c57..b0677b0 100644 --- a/lib/shoehorn/application.ex +++ b/lib/shoehorn/application.ex @@ -7,7 +7,7 @@ defmodule Shoehorn.Application do def start(_type, _args) do if using_shoehorn?() do opts = Application.get_all_env(:shoehorn) - :error_logger.add_report_handler(Shoehorn.ReportHandler, opts) + :logger.add_handler(:shoehorn, Shoehorn.ReportHandler, %{config: opts}) end opts = [strategy: :one_for_one, name: Shoehorn.Supervisor] diff --git a/lib/shoehorn/report_handler.ex b/lib/shoehorn/report_handler.ex index e0f4acf..69c1ec4 100644 --- a/lib/shoehorn/report_handler.ex +++ b/lib/shoehorn/report_handler.ex @@ -1,76 +1,89 @@ defmodule Shoehorn.ReportHandler do @moduledoc false - - @behaviour :gen_event + use GenServer alias Shoehorn.Handler @shutdown_timer 30_000 - @impl :gen_event - def init(opts) do - shutdown_timer = opts[:shutdown_timer] || @shutdown_timer + @doc false + def adding_handler(%{config: opts} = config) do + {:ok, pid} = GenServer.start_link(__MODULE__, opts) + {:ok, %{config | config: %{pid: pid}}} + end - {:ok, - %{ - handler: Handler.init(opts), - shutdown_timer: shutdown_timer - }} + @doc false + def removing_handler(%{config: %{pid: pid}}) do + GenServer.stop(pid) end - @impl :gen_event - def handle_call(_, s) do - {:ok, :ok, s} + @doc false + def log( + %{msg: {:report, %{label: {:application_controller, :progress}, report: report}}}, + config + ) do + application = Keyword.get(report, :application) + GenServer.cast(config.config.pid, {:start, application}) end - @impl :gen_event - def handle_event({:info_report, _pid, {_, :std_info, info}}, s) when is_list(info) do - case Keyword.get(info, :exited) do - nil -> - {:ok, s} + def log(%{msg: {:report, %{label: {:application_controller, :exit}, report: report}}}, config) do + application = Keyword.get(report, :application) + reason = Keyword.get(report, :exited) + GenServer.cast(config.config.pid, {:exit, application, reason}) + end + + def log(_log, _config), do: :ok - reason -> - app = Keyword.get(info, :application) - {:ok, exited(app, reason, s)} - end + @impl GenServer + def init(opts) do + shutdown_timer = opts[:shutdown_timer] || @shutdown_timer + {:ok, %{handler: Handler.init(opts), shutdown_timer: shutdown_timer}} + end + + @impl GenServer + def handle_call(_, _, state) do + {:reply, :ok, state} end - def handle_event({:info_report, _pid, {_, :progress, info}}, s) when is_list(info) do - case Keyword.get(info, :started_at) do - nil -> - {:ok, s} + @impl GenServer + def handle_cast({:start, application}, state) do + {:noreply, started(application, state)} + end + + def handle_cast({:exit, application, reason}, state) do + {:noreply, exited(application, reason, state)} + end - _node -> - app = Keyword.get(info, :application) - {:ok, started(app, s)} - end + def handle_cast(_, state) do + {:noreply, state} end - def handle_event(_event, s) do - {:ok, s} + @impl GenServer + def handle_info(_, state) do + {:noreply, state} end - @impl :gen_event - def terminate(_args, _s) do + @impl GenServer + def terminate(_reason, _state) do :ok end - defp exited(app, reason, s) do - {:ok, shutdown_timer_ref} = :timer.apply_after(s.shutdown_timer, :erlang, :halt, []) + defp exited(app, reason, state) do + {:ok, shutdown_timer_ref} = :timer.apply_after(state.shutdown_timer, :erlang, :halt, []) return = :application_exited - |> Handler.invoke(app, reason, s.handler) - |> react(s) + |> Handler.invoke(app, reason, state.handler) + |> react(state) _ = :timer.cancel(shutdown_timer_ref) return end - defp started(app, s) do + defp started(app, state) do :application_started - |> Handler.invoke(app, s.handler) - |> react(s) + |> Handler.invoke(app, state.handler) + |> react(state) end defp react({:halt, _}, _), do: :erlang.halt()