mule/jobs/worker

Types

Encode/decode for a worker’s typed args. encode runs at enqueue (mule.insert); decode runs just before perform to turn the stored opaque args back into the worker’s type.

pub type Codec(a) {
  Codec(encode: fn(a) -> json.Json, decode: decode.Decoder(a))
}

Constructors

The monomorphic form stored in the registry: a has been erased into perform, which decodes job.args with the worker’s codec and either runs the typed perform or discards the job on a decode failure. Keeps the same fn(Job) -> WorkerResult shape the executor already drives.

pub type ErasedWorker {
  ErasedWorker(
    name: String,
    perform: fn(job.Job) -> WorkerResult,
    backoff: fn(job.Job) -> duration.Duration,
    timeout: fn(job.Job) -> option.Option(Int),
  )
}

Constructors

A typed worker. Generic over its args type a: perform receives the decoded args plus the full Job (for metadata like attempt). register erases the type so heterogeneous workers share one registry.

pub type Worker(a) {
  Worker(
    name: String,
    perform: fn(a, job.Job) -> WorkerResult,
    codec: Codec(a),
    backoff: fn(job.Job) -> duration.Duration,
    timeout: fn(job.Job) -> option.Option(Int),
    options: List(job.Option),
  )
}

Constructors

Per-worker option defaults declared at worker definition — the analog of use Oban.Worker, queue: ..., max_attempts: ...’s keyword options. Applied by new_job BEFORE the caller’s options (merge_options), so a caller override wins.

pub type WorkerOptions =
  List(job.Option)
pub type WorkerResult {
  Completed
  Failed(String)
  Cancelled(String)
  Discarded(String)
  Snoozed(duration.Duration)
}

Constructors

  • Completed
  • Failed(String)
  • Cancelled(String)
  • Discarded(String)

Values

pub fn default_backoff(job: job.Job) -> duration.Duration
pub fn erase(worker: Worker(a)) -> ErasedWorker

Erase a typed worker for storage in the registry. The decoder is captured in the closure, so the type parameter a does not escape.

pub fn merge_options(
  base_options base_options: List(job.Option),
  options options: List(job.Option),
) -> List(job.Option)

Oban.Worker.merge_opts/2: combine a worker’s declared defaults with a caller’s options. The defaults are PREPENDED, so job.new_with‘s last-wins fold gives the caller override semantics for free — except unique, where two non-empty sub-option lists deep-merge per sub-option (the caller’s win), exactly merge_opts’ Keyword.merge clause for two unique keyword lists. An empty list on either side falls back to plain override, like merge_opts’ [_ | _] guards.

pub fn new(
  name name: String,
  perform perform: fn(a, job.Job) -> WorkerResult,
  codec codec: Codec(a),
) -> Worker(a)
pub fn new_job(
  worker worker: Worker(a),
  args args: a,
  options options: List(job.Option),
) -> Result(job.NewJob, job.ValidationError)

Build a NewJob for this worker with Job.new/2-style options — the analog of the MyWorker.new(args, opts) that use Oban.Worker generates, including its Worker.merge_opts(__opts__(), opts) fold of the worker’s declared defaults under the caller’s options. Encoding happens here with this worker’s codec, so a mixed-worker batch for mule.insert_all is a plain List(NewJob) with per-worker type safety already spent at construction.

pub fn nil_codec() -> Codec(Nil)

Codec for workers that take no args.

pub fn with_options(
  worker worker: Worker(a),
  options options: List(job.Option),
) -> Worker(a)

Declare per-worker option defaults — use Oban.Worker, queue: :events, max_attempts: 5’s keyword. Every new_job (and so mule.insert) merges them under the caller’s options via merge_options.

Search Document