Documents / @faasjs/jobs / JobWorker
Class: JobWorker
Long-running background worker that polls the database for pending jobs, claims them, executes their handlers, and updates their status.
Supports configurable concurrency, polling interval, and lease duration.
Failed jobs are retried according to their retry strategy until
max attempts are reached.
Handler errors are persisted to the job row instead of being thrown from poll().
Example
const worker = new JobWorker(registry, {
concurrency: 5,
pollInterval: 2000,
})
worker.start()
Constructors
Constructor
new JobWorker(
jobs,options?):JobWorker
Parameters
jobs
options?
JobWorkerOptions = {}
Returns
JobWorker
Methods
poll()
poll():
Promise<number>
Execute one polling cycle: claim up to concurrency pending jobs
and run their handlers. No-op if a poll is already in progress.
Handler failures are recorded in PostgreSQL and retried or marked failed.
Returns
Promise<number>
The number of jobs processed in this cycle.
start()
start():
this
Start the worker's polling loop. No-op if already active.
Returns
this
The worker instance (for chaining).
stop()
stop():
Promise<void>
Stop the polling loop. Waits for the current poll to complete before resolving.
Returns
Promise<void>
Properties
concurrency
readonlyconcurrency:number
Maximum number of jobs claimed in each poll.
jobs
readonlyjobs:JobRegistry
Loaded job registry keyed by job path.
leaseSeconds
readonlyleaseSeconds:number
Lease duration in seconds before a running job can be reclaimed.
logger
readonlylogger:Logger
Worker logger.
pollInterval
readonlypollInterval:number
Milliseconds between automatic poll ticks.
queue
readonlyqueue:string
Queue name this worker claims from.
workerId
readonlyworkerId:string
Unique worker id written to claimed rows.