Refactor event queue to allow multiple types of events

This commit is contained in:
Dominic Ferrando
2026-07-12 05:21:33 -04:00
parent 4d1b3bc3ef
commit 4c2acfaede
5 changed files with 88 additions and 60 deletions
+6 -4
View File
@@ -20,7 +20,7 @@ import { randomUUIDv7, sleep } from "bun";
import { CalculatorService, CalculatorUnavailableError } from "./services/calculator";
import { StandardsParamsSchema } from "@blade-and-brawn/calculator";
import { StandardsService } from "./services/standards";
import type { EventQueueService } from "./services/event-queue";
import type { EventQueue, EventSpec } from "./services/event-queue";
// CONSTANTS
// -----------------------
@@ -201,6 +201,7 @@ export const app = new Elysia()
// Run sync
.post("/:pProductId?", async ({ params: { pProductId }, sessionId }) => {
await s.Commerce.ProductSync.Queue.enqueue({
type: "product_sync_update",
source: "portal",
payload: {
session: { id: sessionId, name: "Portal" },
@@ -237,6 +238,7 @@ export const app = new Elysia()
log.info({ productId: pProduct.id }, "printful webhook: product updated");
await s.Commerce.ProductSync.Queue.enqueue({
type: "product_sync_update",
source: "printful",
payload: {
session: { id: randomUUIDv7(), name: "Printful" },
@@ -317,19 +319,19 @@ app.listen(3000, async () => {
log.info({ port: 3000 }, "server started")
// MANAGE QUEUES
const queues: EventQueueService<any, any>[] = [s.Commerce.ProductSync.Queue];
const queues: EventQueue<string, Record<string, EventSpec<any, any>>>[] = [s.Commerce.ProductSync.Queue];
for (const queue of queues) {
(async () => {
while (true) {
try {
// clean, if ready
if ((Date.now() - queue.lastCleanDate.getTime()) >= queue.cfg.concurrency.timeoutMs)
await queue.clean().catch((err) => log.error({ name: queue.type, err }));
await queue.clean().catch((err) => log.error({ name: queue.group, err }));
// drain
await queue.drain();
}
catch (err) {
log.error({ name: queue.type, err }, "error occurred during queue management");
log.error({ name: queue.group, err }, "error occurred during queue management");
}
await sleep(EVENT_QUEUE_MANAGE_DELAY_MS);
}