Worker: Process Jobs from a Bun Queue
guide · worker
Pull, process, ack.
A worker is a loop you do not have to write. It asks the queue for work, runs your function, reports the outcome, and keeps the job alive while it runs.
Give the Worker a queue name and a processor. Whatever the processor returns becomes the job result; whatever it throws becomes a failed attempt.
Create a worker
Section titled “Create a worker”import { Worker } from 'bunqueue/client';
const worker = new Worker('my-queue', async (job) => { // Process the job; the return value is stored as the job's result return { success: true };}, { embedded: true });import { Worker } from 'bunqueue-client';
const worker = new Worker('my-queue', async (job) => { // Process the job; the return value is stored as the job's result return { success: true };});from bunqueue import Worker
def process(job): # Process the job; the return value is stored as the job's result return {"success": True}
worker = Worker("my-queue", process)use Bunqueue\Worker;
$worker = new Worker('my-queue', function (Bunqueue\Job $job) { // Process the job; the return value is stored as the job's result return ['success' => true];});
$worker->run(); // blocking loop; or $worker->runOnce() from a cron tickworker := bunqueue.NewWorker("my-queue", func(job *bunqueue.Job) (any, error) { // Process the job; the return value is stored as the job's result return map[string]any{"success": true}, nil}, bunqueue.WorkerOptions{})
worker.Run() // blocking pull loopuse bunqueue_client::{Value, Worker, WorkerOptions};
let worker = Worker::new( "my-queue", |_job| { // Process the job; the returned Value is stored as the job's result Ok(Value::from(true)) }, WorkerOptions::default(),);worker.run()?;worker = Bunqueue.Worker.new("my-queue", fn _job -> # Process the job; the result is stored as the job's result {:ok, %{success: true}} end)
Bunqueue.Worker.run(worker)The Bun, TypeScript, and Python workers start polling immediately (autorun); the PHP, Go, Rust, and Elixir workers start when you call their run function. If your processor throws, the job is retried automatically (up to the job’s attempts, default 3) and then moved to the dead letter queue, a holding area for jobs that keep failing.
Where to go next
Section titled “Where to go next”| Worker Concurrency and Batch Pulling | Run jobs in parallel and pull them in batches |
| The Job Object Inside a Worker Processor | Everything the processor receives and can do |
| Worker Events | completed, failed, stalled and the rest |
| Worker Error Handling, Retries and Backoff | Retries, backoff, timeouts and giving up |
| Worker Lifecycle | Pause, resume and shut down without losing work |
| Heartbeats, Stall Detection and Lock Ownership | Heartbeats, stall recovery and lock ownership |
| SandboxedWorker | Experimental isolation for CPU-heavy handlers |
| WorkerOptions Reference | Every WorkerOptions field, with defaults |