Skip to content

Commit

Permalink
feat: auto starts Tower.EphemeralReporter
Browse files Browse the repository at this point in the history
  • Loading branch information
grzuy committed Oct 3, 2024
1 parent 4ceb41a commit ee10638
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
1 change: 1 addition & 0 deletions lib/tower/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defmodule Tower.Application do
def start(_type, _args) do
Supervisor.start_link(
[
Tower.EphemeralReporter,
{Task.Supervisor, name: Tower.TaskSupervisor}
],
strategy: :one_for_one,
Expand Down
28 changes: 24 additions & 4 deletions lib/tower/ephemeral_reporter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ defmodule Tower.EphemeralReporter do
## Example
iex> {:ok, pid} = Tower.EphemeralReporter.start_link([])
iex> Tower.EphemeralReporter.events()
[]
iex> Application.put_env(:tower, :reporters, [Tower.EphemeralReporter])
Expand All @@ -22,19 +21,35 @@ defmodule Tower.EphemeralReporter do
%ArithmeticError{message: "bad argument in arithmetic expression"}
iex> Tower.detach()
:ok
iex> Tower.EphemeralReporter.stop(pid)
:ok
"""
@behaviour Tower.Reporter

@default_level :info

use Agent

require Logger

alias Tower.Event

@empty_events []

def start_link(_opts) do
Agent.start_link(fn -> [] end, name: __MODULE__)
Agent.start_link(fn -> @empty_events end, name: __MODULE__)
|> case do
{:error, {:already_started, existing_pid}} ->
Logger.warning("""
An attempt to start Tower.EphemeralReporter when it is already started was ignored.
If you are manually starting Tower.EphemeralReporter, you can safely stop doing it given
it is automatically started by Tower.
""")

{:ok, existing_pid}

on_start ->
on_start
end
end

def stop(pid) do
Expand All @@ -55,4 +70,9 @@ defmodule Tower.EphemeralReporter do
def events do
Agent.get(__MODULE__, & &1)
end

@spec reset() :: :ok
def reset do
Agent.update(__MODULE__, fn _events -> @empty_events end)
end
end
3 changes: 1 addition & 2 deletions test/tower/tower_oban_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ defmodule TowerObanTest do
import ExUnit.CaptureLog, only: [capture_log: 1]

setup do
start_link_supervised!(Tower.EphemeralReporter)

start_link_supervised!({
TestApp.Repo,
database: "tmp/test-#{:rand.uniform(10_000)}.db", journal_mode: :memory
Expand All @@ -26,6 +24,7 @@ defmodule TowerObanTest do

on_exit(fn ->
Tower.detach()
Tower.EphemeralReporter.reset()
end)
end

Expand Down
2 changes: 1 addition & 1 deletion test/tower/tower_plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ defmodule TowerPlugTest do
import ExUnit.CaptureLog, only: [capture_log: 1]

setup do
start_link_supervised!(Tower.EphemeralReporter)
Tower.attach()

on_exit(fn ->
Tower.detach()
Tower.EphemeralReporter.reset()
end)
end

Expand Down
6 changes: 1 addition & 5 deletions test/tower_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ defmodule TowerTest do
import ExUnit.CaptureLog, only: [capture_log: 1]

setup do
start_reporter()
Tower.attach()

on_exit(fn ->
Tower.detach()
Tower.EphemeralReporter.reset()
end)
end

Expand Down Expand Up @@ -622,10 +622,6 @@ defmodule TowerTest do
|> Task.yield()
end

defp start_reporter do
Tower.EphemeralReporter.start_link([])
end

defp reported_events do
Tower.EphemeralReporter.events()
end
Expand Down

0 comments on commit ee10638

Please sign in to comment.