@plinthjs/queue
The Plinth equivalent of the usual queue API: jobs, two deterministic drivers (sync + array), chains, batches, and retries.
Nothing in this package sleeps or reads the wall clock. later(delaySeconds, job) only records the requested delay on the queued envelope, so every code path is deterministic in tests.
Jobs
ts
import { Job } from '@plinthjs/queue'
class SendInvoice extends Job {
tries = 3 // attempt up to three times before failing
constructor(private readonly invoiceId: number) {
super()
}
async handle(): Promise<void> {
// ...do the work; throwing triggers a retry
}
}Drivers
ts
import { SyncQueue, ArrayQueue } from '@plinthjs/queue'
// Sync: runs immediately on push, applying retries up to job.tries.
const sync = new SyncQueue()
await sync.push(new SendInvoice(1))
// Failures (after exhausting tries) land in sync.failed, or pass { rethrow: true } to throw.
// Array: stores jobs in memory; work() drains them FIFO.
const array = new ArrayQueue()
await array.push(new SendInvoice(1))
await array.later(60, new SendInvoice(2)) // delay is recorded, not slept
array.size() // => 2
const { processed, failed } = await array.work() // alias: process()Dispatcher: single, chains, batches
ts
import { Dispatcher } from '@plinthjs/queue'
const dispatcher = new Dispatcher() // defaults to an inline SyncQueue
// Single dispatch (optionally onto an explicit queue).
await dispatcher.dispatch(new SendInvoice(1))
// Chain: runs sequentially, stopping at the first failure.
const result = await dispatcher.dispatchChain([new StepA(), new StepB()])
result.completed // false if a job threw
result.ran // how many ran before stopping
// Batch: runs a group, tracks counters, fires callbacks. The handlers are named
// onSuccess/onFailure/onFinally so the batch is not a
// thenable, which would otherwise hang `await batch.dispatch()`.
const batch = await dispatcher
.batch([new SendInvoice(1), new SendInvoice(2)])
.onSuccess((b) => {
/* all succeeded */
})
.onFailure((b, error) => {
/* at least one failed; error is the first failure */
})
.onFinally((b) => {
/* always */
})
.dispatch()
batch.totalJobs // 2
batch.processedJobs // succeeded
batch.failedJobs // threw
batch.pendingJobs // 0 once dispatched