Fleet.Worker behaviour (fleet v0.1.0)

Copy Markdown View Source

Defines a background job worker.

Example

defmodule MyApp.Workers.SendEmail do
  use Fleet.Worker, queue: :mailers, max_attempts: 3

  @impl Fleet.Worker
  def perform(%Fleet.Job{args: %{"to" => to}}) do
    MyApp.Mailer.send(to)
  end
end

Summary

Callbacks

Processes multiple jobs together in a single batch.

Functions

Calculates standard exponential backoff with jitter in seconds.

Callbacks

backoff(job)

(optional)
@callback backoff(job :: Fleet.Job.t()) :: pos_integer()

perform(job)

(optional)
@callback perform(job :: Fleet.Job.t()) ::
  :ok
  | {:ok, term()}
  | {:error, term()}
  | {:snooze, pos_integer()}
  | {:cancel, term()}
  | term()

perform_batch(jobs)

(optional)
@callback perform_batch(jobs :: [Fleet.Job.t()]) ::
  :ok | {:ok, term()} | {:error, term()} | term()

Processes multiple jobs together in a single batch.

When defined on a worker, Broadway groups messages according to the queue's :batch_size and :batch_timeout settings before calling this callback. Ideal for bulk external API calls or bulk database persistence (e.g. using ecto_unnest).

Functions

default_backoff(attempt)

@spec default_backoff(integer()) :: pos_integer()

Calculates standard exponential backoff with jitter in seconds.