Improve how events are drained

This commit is contained in:
Dominic Ferrando
2026-07-11 20:50:14 -04:00
parent d17b3d8960
commit 5265492442
2 changed files with 14 additions and 26 deletions
+9 -13
View File
@@ -19,10 +19,6 @@ export type ProductSyncState = {
syncingPProductIds: number[]
}
type QueueDrainOptions = {
untilId?: string
}
type ProductSyncHandler = (id: string, payload: ProductSyncPayload, setState: (state: ProductSyncState) => Promise<void>) => Promise<void>
class AlreadyClaimedEventError extends Error { }
@@ -89,14 +85,12 @@ class ProductSyncQueueService {
return event.has_failed ? "failed" : "fulfilled";
}
async drain(opt: QueueDrainOptions = {}): Promise<void> {
log.info("draining queue");
async drain(): Promise<void> {
log.trace("draining queue");
while (true) {
try {
const dequeued = await this.dequeue();
if (!dequeued) break;
if (dequeued.id === opt.untilId) break;
const nextActiveEvent = await this.dequeue();
if (!nextActiveEvent) break;
}
catch (err) {
if (err instanceof AlreadyClaimedEventError) {
@@ -116,7 +110,7 @@ class ProductSyncQueueService {
}
async dequeue() {
log.info("dequeuing");
log.trace("attempting to dequeue");
const eventToHandle = await db.transaction().execute(async (trx) => {
const activeEvents = await this.activeEvents(trx);
let activeEventCount = activeEvents.length;
@@ -134,7 +128,7 @@ class ProductSyncQueueService {
throw new ActiveEventLimitReachedError();
const front = await this.front(trx);
if (!front) return log.info("nothing to dequeue");
if (!front) return;
const result = await trx.updateTable("product_syncs")
.set({ started_at: new Date() })
@@ -149,6 +143,7 @@ class ProductSyncQueueService {
});
if (eventToHandle) {
log.trace({ name: eventToHandle.id }, "dequeuing event");
await this.handleEvent(eventToHandle.id, {
session: {
id: eventToHandle.session_id,
@@ -161,6 +156,7 @@ class ProductSyncQueueService {
}
async enqueue(payload: ProductSyncPayload): Promise<string> {
log.info({ payload }, "enqueuing");
const result = await db.insertInto("product_syncs")
.values({
session_id: payload.session.id,
@@ -210,8 +206,8 @@ class ProductSyncQueueService {
}
private async handleEvent(id: string, payload: ProductSyncPayload) {
log.info({ id, payload }, "handling event");
try {
log.info({ id, payload }, "handling event");
await this.handler(id, payload, async (state) => {
// set event state
await db.updateTable("product_syncs")