Move queue management onto all workers and make clean single atomic operation

This commit is contained in:
Dominic Ferrando
2026-07-17 10:24:37 -04:00
parent 903a60fec5
commit 5829021658
2 changed files with 36 additions and 27 deletions
+19 -19
View File
@@ -24,6 +24,7 @@ import { EventsService, EventStatusSchema } from "./services/events";
// CONSTANTS
// -----------------------
const EVENT_QUEUE_MANAGE_DELAY_MS = 1000 // 1 sec
const JWT_EXP = "7d";
// SERVICES
// -----------------------
@@ -124,7 +125,7 @@ export const app = new Elysia()
if (body.password !== env.ADMIN_PASSWORD) throw status(401, "Invalid credentials");
auth.set({
value: await jwt.sign({ sessionId: randomUUIDv7(), exp: "7d" }),
value: await jwt.sign({ sessionId: randomUUIDv7(), exp: JWT_EXP }),
path: "/",
maxAge: 60 * 60 * 24 * 7,
sameSite: "lax",
@@ -345,27 +346,26 @@ export const app = new Elysia()
});
app.listen(3000, async () => {
if (cluster.worker?.id === 1) {
if (cluster.worker?.id === 1)
log.info({ port: 3000 }, "server started")
// MANAGE QUEUES
for (const queue of queues) {
(async () => {
while (true) {
try {
// clean, if ready
if ((Date.now() - queue.lastCleanDate.getTime()) >= EventsService.CONCURRENCY_TIMEOUT_MS)
await queue.clean().catch((err) => log.error({ name: queue.group, err }));
// drain
await queue.drain();
}
catch (err) {
log.error({ name: queue.group, err }, "error occurred during queue management");
}
await sleep(EVENT_QUEUE_MANAGE_DELAY_MS);
// MANAGE QUEUES
for (const queue of queues) {
(async () => {
while (true) {
try {
// clean, if ready
if ((Date.now() - queue.lastCleanDate.getTime()) >= EventsService.CONCURRENCY_TIMEOUT_MS)
await queue.clean().catch((err) => log.error({ name: queue.group, err }));
// drain
await queue.drain();
}
})();
}
catch (err) {
log.error({ name: queue.group, err }, "error occurred during queue management");
}
await sleep(EVENT_QUEUE_MANAGE_DELAY_MS);
}
})();
}
});