JoobQ.Worker behaviour (joobq_ex v0.8.0)

Copy Markdown View Source

A job handler, run by the worker of an instance that lists it in :workers (JoobQ.Config):

defmodule MyApp.Welcome do
  use JoobQ.Worker, queue: "mail", max_retries: 5

  @impl JoobQ.Worker
  def perform(%JoobQ.Job{payload: %{"to" => to}, context: context}) do
    MyApp.Mailer.welcome(to, context["tenant_id"])
  end
end

MyApp.Welcome.enqueue(%{to: "a@example.com"}, context: %{tenant_id: "t1"})

perform/1 gets the leased JoobQ.Job and answers :ok or {:ok, output} (the job completes; output must be JSON) or {:error, reason} (it fails and is retried while retries remain). A raise, an exit or a throw fails it the same way; JoobQ.NonRetryable, raised or returned, sends it to the dead letter queue at once.

Options of use JoobQ.Worker:

  • :queue (required): the queue the job is enqueued on.
  • :job_type: the name the job is enqueued under, the module's name ("MyApp.Welcome") by default; a producer in another language enqueues it by this name.
  • :max_retries: retries enqueue/2 asks for, 0 to 100 (the gateway's default is 3).
  • :timeout: milliseconds perform/1 may run before it is stopped and the run fails, :infinity by default.

Summary

Types

options()

@type options() :: %{
  queue: String.t(),
  job_type: String.t(),
  max_retries: 0..100 | nil,
  timeout: pos_integer() | :infinity
}

Callbacks

perform(t)

@callback perform(JoobQ.Job.t()) :: :ok | {:ok, term()} | {:error, term()}