createWorkerRunner

Function

Signature

function createWorkerRunner<TJobTypeDefinitions, TStateAdapter, TProcessorsInput, TRequiredAttemptMiddleware>(options): WorkerRunner;

core/src/worker/worker-runner.ts:146

Create a worker runner for processing jobs.

Per-attempt configuration (attemptMiddleware, backoffConfig) lives on the processor registry (see createProcessors). The worker runner dispatches to whichever processor matches the job's typeName and runs that slice's middleware chain. The worker runner's backoffConfig is the fallback for processors that don't set their own; precedence is processor > processor registry > worker runner > built-in default.

The worker runner registers one worker at a time under workerId, a row with a lease it renews. When the lease is lost, it aborts the attempts' signals, waits for every attempt to end and registers again under the same id.

Type Parameters

Parameters

  • options
    object
    Worker runner configuration.
  • options.backoffConfig?
    Backoff for failed job attempts of processors that don't set their own backoffConfig.
  • options.client
    Client<TJobTypeDefinitions, TStateAdapter>
    The Queuert client to process jobs for.
  • options.concurrency?
    number
    Maximum number of jobs to process in parallel. Must be a positive integer.
  • options.leaseConfig?
    The lease the worker runner registers its worker with. Both fields are required when it is passed.
  • options.pollIntervalMs?
    number
    How often to poll for new jobs when no notify adapter wakes the worker. Must be finite and positive.
  • options.processors
    [ExtraProcessorTypeNames<WorkerProcessorDefs<TProcessorsInput>, TJobTypeDefinitions>] extends [never] ? ValidateRequiredMiddleware<TProcessorsInput, TRequiredAttemptMiddleware> & (TProcessorsInput extends readonly Processors[] ? number extends TProcessorsInput["length"] ? unknown : ValidatedProcessorSlices<TProcessorsInput> : unknown) : `Error: processors contain job types unknown to the client: ${ExtraProcessorTypeNames<WorkerProcessorDefs<TProcessorsInput>, TJobTypeDefinitions> & string}`
    A single Processors (from createProcessors, pickProcessors or omitProcessors), or an array of slices to merge. Slices must not share job types: an overlap is a type error when the array's length is known at compile time, and throws DuplicateJobTypeError at runtime.
  • options.recoveryBackoffConfig?
    Backoff after a failed worker loop iteration, a failed registration or a lost lease. It resets once the worker starts a job.
  • options.requiredAttemptMiddleware?
    TRequiredAttemptMiddleware & readonly AttemptMiddleware<NoInfer<TStateAdapter>, any>[]
    Middleware every processor's slice must include, in this order. The worker runner does not run it.
  • options.workerId?
    GetStateAdapterWorkerId<TStateAdapter>
    Id the worker registers under, kept when it registers again after a lost lease. Pass a fixed id to keep it across process restarts. While a live worker holds it, the worker runner waits for that lease to expire before it starts.

Returns

Throws

  • When concurrency, pollIntervalMs, recoveryBackoffConfig, leaseConfig, backoffConfig or any processor's backoffConfig holds an invalid value; the message lists every invalid field.
  • InvalidWorkerIdError if workerId fails the state adapter's validateWorkerId.
  • DuplicateJobTypeError if slices in a processors array share job types.
  • if a processor's slice is missing requiredAttemptMiddleware or has it out of order.

Used in guides

esc

Search guides, integrations and every exported symbol.

↑ ↓ to navigate↵ to open