Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: async reporting #17

Merged
merged 8 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions lib/tower.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,32 @@ defmodule Tower do

def report_exception(exception, stacktrace, meta \\ %{})
when is_exception(exception) and is_list(stacktrace) do
reporters()
|> Enum.each(fn reporter ->
each_reporter(fn reporter ->
reporter.report_exception(exception, stacktrace, meta)
end)
end

def report(type, reason, stacktrace, meta \\ %{}) when is_atom(type) and is_list(stacktrace) do
each_reporter(fn reporter ->
reporter.report(type, reason, stacktrace, meta)
end)
end

defp each_reporter(fun) when is_function(fun, 1) do
reporters()
|> Enum.each(fn reporter ->
reporter.report(type, reason, stacktrace, meta)
async(fn ->
fun.(reporter)
end)
end)
end

def reporters do
Application.get_env(:tower, :reporters, @default_reporters)
end

defp async(fun) do
Tower.TaskSupervisor
|> Task.Supervisor.start_child(fun)
end
end
18 changes: 18 additions & 0 deletions lib/tower/application.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule Tower.Application do
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
@moduledoc false

use Application

@impl true
def start(_type, _args) do
Supervisor.start_link(
[
{Task.Supervisor, name: Tower.TaskSupervisor}
],
strategy: :one_for_one,
name: Tower.Supervisor
)
end
end
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ defmodule Tower.MixProject do
# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
extra_applications: [:logger],
mod: {Tower.Application, []}
]
end

Expand Down
Loading