npm.io
0.10.0 • Published yesterday

@open-mercato/queue

Licence
MIT
Version
0.10.0
Deps
2
Size
420 kB
Vulns
0
Weekly
0
Stars
1.9K

@open-mercato/queue

Flexible job queue runtime for local and distributed execution.

@open-mercato/queue provides a unified queue API with strategy support (local or BullMQ), worker contracts, and scalable background processing.

Why this package

  • One API, multiple queue backends
  • Built for async workloads and retries
  • Worker-first design with explicit concurrency
  • Great fit for idempotent business jobs
  • Coalescing that collapses bursts without losing the last trigger

Coalescing

A job that recomputes something from current state — an order total, a cached projection, a search document, a realtime broadcast — does not need to run once per trigger. It needs to run once per burst, on the latest state. Give the enqueue a key and repeated triggers collapse into the job already outstanding for it:

await queue.enqueue({ orderId }, { coalesce: { key: `order-totals:${orderId}` } })

Ten triggers in a minute then produce at most two runs instead of ten — and none of them is lost. Triggers arriving while a job for the key is merely waiting need no run of their own, because that job has not read anything yet. Triggers arriving while it is running are parked, and exactly one follow-up run starts when it finishes, carrying the latest payload.

Declare the key once on the queue and no call site can forget it:

const queue = createModuleQueue<RecalculateTotals>('order-totals', {
  coalesceBy: (payload) => `order-totals:${payload.orderId}`,
})

Both strategies implement this — the async one through BullMQ, the local one on its file-backed store — so development behaves like production. The async path uses BullMQ's keepLastIfActive, which is why the bullmq peer range starts at 5.72.0: older versions ignore the flag and silently fall back to dropping the mid-run trigger.

Do not combine coalesce with delayMs. Collapsing an enqueue collapses its schedule too, and the strategies resolve that differently — local takes the earlier of the two moments, async leaves the surviving job on its own. Nothing is dropped either way, but the delay a burst ends up with is not well defined. Give delayed work its own key, or no key.

Install

yarn add @open-mercato/queue

Learn more

License

MIT