JobQueue class

A job queue that both pushes and handles jobs in the same process — the default export, and the one to start with.

Scheduling is per job and optional: push with no options to run as soon as a worker is free, with PushOptions.delay to run later, and with PushOptions.ttl to stop retrying after a while. Register the handler with JobQueue.onPop() before JobQueue.start() — this class insists on it, because a combined queue with no handler would enqueue work that nothing consumes.

Split the two ends into JobQueuePublisher and JobQueueWorker when they belong in different processes, which is what scaling the workers out requires.

Signature:

export default class JobQueue<T> extends BaseJobQueue<JobQueue<T>, T> implements AnyJobQueueWorker<JobQueue<T>, T>, AnyJobQueuePublisher<JobQueue<T>, T> 

Extends: BaseJobQueue<JobQueue<T>, T>

Implements: AnyJobQueueWorker<JobQueue<T>, T>, AnyJobQueuePublisher<JobQueue<T>, T>

Remarks

On SIGTERM, SIGINT or SIGABRT the underlying @imqueue/core queue releases its watcher locks and exits the process. That is orderly, but it is not a drain: a handler still running is not awaited, so the job it was working on loses that attempt. Do the draining yourself if a half-finished job would leave a mess.

Example

import JobQueue from '@imqueue/job';

const queue = new JobQueue<Email>({ name: 'Email' });

queue.onPop(async (email: Email) => {
    await send(email);
});

await queue.start();

queue.push({ to: 'a@b.c', subject: 'Hi' });

Constructors

Constructor

Modifiers

Description

(constructor)(options)

Creates a queue that both publishes and consumes the named job queue.

Methods

Method

Modifiers

Description

onPop(handler)

Registers the handler called for each job popped from this queue.

push(job, options)

Enqueues one job, optionally delayed or time-limited, refusing to enqueue without a handler.

start()

Starts processing the job queue, refusing to start without a handler.

Read this page as plain markdown — no HTML, no navigation. For pasting into an LLM, or for an agent to fetch.