Oban v2.9: Reportable protocol isn't working

To start, define a Reportable protocol with a single reportable?/2 function:

defprotocol MyApp.Reportable do
  @fallback_to_any true
  def reportable?(worker, attempt)
end

defimpl MyApp.Reportable, for: Any do
  def reportable?(_worker, _attempt), do: true
end

I set the threshold and added a Reportable implementation inside the worker module:

defmodule MyApp.Workers.FlakyWorker do
  use Oban.Worker

  defimpl MyApp.Reportable do
    @threshold 3

    def reportable?(_worker, attempt), do: attempt > @threshold
  end

  @impl true
  def perform(%Oban.Job{args: %{"email" => email}}) do
    MyApp.ExternalService.deliver(email)
  end
end

At the end I called reportable?/2 from the application’s error reporter, passing the worker module and the attempt number:

defmodule MyApp.ErrorReporter do
  alias MyApp.Reportable

  def handle_event(_, _,  %{attempt: attempt, worker: worker} = meta, _) do
     context = Map.take(meta, [:id, :args, :queue, :worker])

    if Reportable.reportable?(worker_struct, meta.job.attempt) do
           Logger.error("Error", attempt: attempt, error: meta.error, context: context)

    end
  end

end

final summary

FlakyWorker is not getting the @threshold 3 definition

You’re following an older guide that has some issues we fixed later on. There have been four minor Oban releases since then, and you’ll find a corrected version of the guides for those updated versions: Handling Expected Failures — Oban v2.13.4

1 Like
  • Do you know the difference between before and after?
  • What are these issues fixed?
  • According to the documentation why does the module have to be converted to defstruct?

The history for that recipe will answer all of your questions: History for guides/recipes/expected-failures.md - sorentwo/oban · GitHub