Finish up event queue abstraction
This commit is contained in:
+28
-9
@@ -21,8 +21,13 @@ import { CalculatorService, CalculatorUnavailableError } from "./services/calcul
|
||||
import { StandardsParamsSchema } from "@blade-and-brawn/calculator";
|
||||
import { StandardsService } from "./services/standards";
|
||||
|
||||
// CONSTANTS
|
||||
// -----------------------
|
||||
const EVENT_QUEUE_MANAGE_DELAY_MS = 1000 // 1 sec
|
||||
const EVENT_QUEUE_CLEAN_DELAY_MS = 600000 // 10 min
|
||||
|
||||
// SERVICES
|
||||
// -----------
|
||||
// -----------------------
|
||||
const s = (() => {
|
||||
const Standards = new StandardsService();
|
||||
const Calculator = new CalculatorService(DEFAULT_NAME);
|
||||
@@ -31,7 +36,7 @@ const s = (() => {
|
||||
})();
|
||||
|
||||
// ELYSIA
|
||||
// -----------
|
||||
// -----------------------
|
||||
export const app = new Elysia()
|
||||
.use(serverTiming())
|
||||
.use(
|
||||
@@ -196,9 +201,12 @@ export const app = new Elysia()
|
||||
// Run sync
|
||||
.post("/:pProductId?", async ({ params: { pProductId }, sessionId }) => {
|
||||
await s.Commerce.ProductSync.Queue.enqueue({
|
||||
session: { id: sessionId, name: "Portal" },
|
||||
filter: {
|
||||
pProductIds: pProductId ? [pProductId] : null
|
||||
source: "portal",
|
||||
payload: {
|
||||
session: { id: sessionId, name: "Portal" },
|
||||
filter: {
|
||||
pProductIds: pProductId ? [pProductId] : undefined
|
||||
}
|
||||
}
|
||||
});
|
||||
}, { params: t.Object({ pProductId: t.Optional(t.Numeric()) }) }),
|
||||
@@ -229,8 +237,11 @@ export const app = new Elysia()
|
||||
log.info({ productId: pProduct.id }, "printful webhook: product updated");
|
||||
|
||||
await s.Commerce.ProductSync.Queue.enqueue({
|
||||
session: { id: randomUUIDv7(), name: "Printful" },
|
||||
filter: { pProductIds: [pProduct.id] }
|
||||
source: "printful",
|
||||
payload: {
|
||||
session: { id: randomUUIDv7(), name: "Printful" },
|
||||
filter: { pProductIds: [pProduct.id] }
|
||||
}
|
||||
});
|
||||
break;
|
||||
}
|
||||
@@ -305,9 +316,17 @@ app.listen(3000, async () => {
|
||||
if (cluster.worker?.id === 1) {
|
||||
log.info({ port: 3000 }, "server started")
|
||||
|
||||
// MANAGE QUEUES
|
||||
const queues = [s.Commerce.ProductSync.Queue];
|
||||
while (true) {
|
||||
await s.Commerce.ProductSync.Queue.drain().catch((err) => log.error(err));
|
||||
await sleep(1000); // every 1 second;
|
||||
for (const queue of queues) {
|
||||
// clean, if ready
|
||||
if ((Date.now() - queue.lastCleanDate.getTime()) >= EVENT_QUEUE_CLEAN_DELAY_MS)
|
||||
await queue.clean();
|
||||
// drain
|
||||
await queue.drain().catch((err) => log.error(err));
|
||||
}
|
||||
await sleep(EVENT_QUEUE_MANAGE_DELAY_MS);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user