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
+5 -13
View File
@@ -16,7 +16,7 @@ import serverTiming from "@elysia/server-timing";
import jwt from "@elysia/jwt";
import { CommerceService } from "./services/commerce";
import cluster from "node:cluster";
import { randomUUIDv7 } from "bun";
import { randomUUIDv7, sleep } from "bun";
import { CalculatorService, CalculatorUnavailableError } from "./services/calculator";
import { StandardsParamsSchema } from "@blade-and-brawn/calculator";
import { StandardsService } from "./services/standards";
@@ -200,9 +200,6 @@ export const app = new Elysia()
filter: {
pProductIds: pProductId ? [pProductId] : null
}
}).then(async (id) => {
await s.Commerce.ProductSync.Queue.drain({ untilId: id });
void s.Commerce.ProductSync.Queue.drain().catch((err) => log.error({ err }));
});
}, { params: t.Object({ pProductId: t.Optional(t.Numeric()) }) }),
)
@@ -231,17 +228,9 @@ export const app = new Elysia()
const pProduct = payload.data.sync_product;
log.info({ productId: pProduct.id }, "printful webhook: product updated");
// Controller layer handles draining the queue, potentionally across multiple workers
// TODO: maybe find better way of handling queue draining
// can cause a worker to hang for long time while draining
// this can cause an issue with webhook sender timing out and sending duplicate payload
// while we wait to process unrelated events
await s.Commerce.ProductSync.Queue.enqueue({
session: { id: randomUUIDv7(), name: "Printful" },
filter: { pProductIds: [pProduct.id] }
}).then(async (id) => {
await s.Commerce.ProductSync.Queue.drain({ untilId: id });
void s.Commerce.ProductSync.Queue.drain().catch((err) => log.error({ err }));
});
break;
}
@@ -316,7 +305,10 @@ app.listen(3000, async () => {
if (cluster.worker?.id === 1) {
log.info({ port: 3000 }, "server started")
await s.Commerce.ProductSync.Queue.drain().catch((err) => log.error({ err }));
while (true) {
await s.Commerce.ProductSync.Queue.drain().catch((err) => log.error(err));
await sleep(1000); // every 1 second;
}
}
});