-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add and configure Oban and setup email job
- Install Oban and configure according to docs: https://hexdocs.pm/oban/Oban.html - Setup error handling according to docs: https://hexdocs.pm/oban/expected-failures.html#content - Add reporting to Sentry via: https://hexdocs.pm/oban/Oban.html#module-reporting-errors
- Loading branch information
Showing
18 changed files
with
276 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,12 @@ config :phoenix, :json_library, Jason | |
config :phoenix_starter, PhoenixStarter.Email, | ||
default_from: {"PhoenixStarter", "[email protected]"} | ||
|
||
# Configures Oban | ||
config :phoenix_starter, Oban, | ||
repo: PhoenixStarter.Repo, | ||
plugins: [Oban.Plugins.Pruner], | ||
queues: [default: 10, emails: 10] | ||
|
||
# Configures Sentry | ||
config :sentry, | ||
enable_source_code_context: true, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
defmodule PhoenixStarter.Workers.ErrorReporter do | ||
@moduledoc """ | ||
Receives exception events in `Oban.Worker` for error reporting. | ||
""" | ||
alias PhoenixStarter.Workers.Reportable | ||
|
||
@spec handle_event( | ||
:telemetry.event_name(), | ||
:telemetry.event_measurements(), | ||
:telemetry.event_metadata(), | ||
:telemetry.handler_config() | ||
) :: any() | ||
def handle_event( | ||
[:oban, :job, :exception], | ||
measure, | ||
%{attempt: attempt, worker: worker} = meta, | ||
_ | ||
) do | ||
if Reportable.reportable?(worker, attempt) do | ||
extra = | ||
meta | ||
|> Map.take([:id, :args, :queue, :worker]) | ||
|> Map.merge(measure) | ||
|
||
Sentry.capture_exception(meta.error, stacktrace: meta.stacktrace, extra: extra) | ||
end | ||
end | ||
|
||
def handle_event([:oban, :circuit, :trip], _measure, meta, _) do | ||
Sentry.capture_exception(meta.error, stacktrace: meta.stacktrace, extra: meta) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
defprotocol PhoenixStarter.Workers.Reportable do | ||
@moduledoc """ | ||
Determines if errors encountered by `Oban.Worker` should be reported. | ||
By default all errors are reported. However some workers have an | ||
expectation of transient errors, such as those that send email, because of | ||
flaky third-party providers. By implementing this protocol for a given | ||
worker, you can customize error reporting. | ||
## Example | ||
defmodule PhoenixStarter.Workers.EmailWorker do | ||
use Oban.Worker | ||
defimpl PhoenixStarter.Workers.Reportable do | ||
@threshold 3 | ||
# Will only report the error after 3 attempts | ||
def reportable?(_worker, attempt), do: attempt > @threshold | ||
end | ||
@impl true | ||
def perform(%{args: %{"email" => email}}) do | ||
PhoenixStarter.Email.deliver(email) | ||
end | ||
end | ||
""" | ||
@fallback_to_any true | ||
@spec reportable?(Oban.Worker.t(), integer) :: boolean | ||
def reportable?(worker, attempt) | ||
end | ||
|
||
defimpl PhoenixStarter.Workers.Reportable, for: Any do | ||
def reportable?(_worker, _attempt), do: true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
defmodule PhoenixStarter.Workers.UserEmailWorker do | ||
@moduledoc """ | ||
Schedules an email from `PhoenixStarter.Users.UserEmail` to be sent. | ||
""" | ||
use Oban.Worker, queue: :emails, tags: ["email", "user-email"] | ||
|
||
alias PhoenixStarter.Users | ||
alias PhoenixStarter.Users.UserEmail | ||
alias PhoenixStarter.Mailer | ||
|
||
@emails :functions | ||
|> UserEmail.__info__() | ||
|> Keyword.keys() | ||
|> Enum.reject(fn f -> f == :render end) | ||
|> Enum.map(&Atom.to_string/1) | ||
|
||
@impl true | ||
@spec perform(Oban.Job.t()) :: Oban.Worker.result() | ||
def perform(%Oban.Job{args: %{"email" => email} = args}) when email in @emails do | ||
{email, args} = Map.pop(args, "email") | ||
|
||
%{"user_id" => user_id, "url" => url} = args | ||
user = Users.get_user!(user_id) | ||
|
||
email = apply(UserEmail, String.to_existing_atom(email), [user, url]) | ||
{:ok, Mailer.deliver_now(email)} | ||
end | ||
|
||
def perform(_job), do: {:discard, :invalid_email} | ||
|
||
defimpl PhoenixStarter.Workers.Reportable do | ||
@threshold 3 | ||
|
||
@spec reportable?(Oban.Worker.t(), integer) :: boolean | ||
def reportable?(_worker, attempt), do: attempt > @threshold | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.