Skip to content

queues

import { enqueue, processBatch, type QueueMessageHandler } from "louise-toolkit/queues";

A thin wrapper over Cloudflare Queues. No peers.

function enqueue<T>(queue: Queue<T>, message: T): Promise<void>;

Sends one message onto a queue binding. A send failure is wrapped in LouiseQueueError (original as cause).

await enqueue(env.COMMERCE_QUEUE, { type: "order.created", id });
function processBatch<T>(batch: MessageBatch<T>, handler: QueueMessageHandler<T>): Promise<void>;
type QueueMessageHandler<T> = (message: T, context: { attempts: number }) => void | Promise<void>;

Drains a batch, running handler once per message. Each message is acked or retried independently — one failing message doesn’t block the rest from acking. processBatch never throws: a handler’s own error is caught and turned into a retry(), so a Worker’s queue() export can be the whole body.

export default {
async queue(batch: MessageBatch, env: Env) {
await processBatch(batch, async (msg, { attempts }) => {
// Throwing marks THIS message for retry; returning acks it.
await handleEvent(msg, env);
});
},
};