From 3aecbfe62727ba1298e03eb376373823efc2d738 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl-Gerhard=20Lindesva=CC=88rd?= Date: Sun, 5 May 2024 15:20:55 +0200 Subject: [PATCH] get oldest delayed job --- apps/worker/src/jobs/events.incoming-event.ts | 2 +- packages/queue/src/utils.ts | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/apps/worker/src/jobs/events.incoming-event.ts b/apps/worker/src/jobs/events.incoming-event.ts index dcfd923e..8e8d9aaa 100644 --- a/apps/worker/src/jobs/events.incoming-event.ts +++ b/apps/worker/src/jobs/events.incoming-event.ts @@ -181,7 +181,7 @@ export async function incomingEvent(job: Job) { if (payload.name === 'screen_view') { if (duration < 0) { - job.log(`prevEvent ${JSON.stringify(prevEvent, null, 2)}`); + logger.info({ prevEvent, payload }, 'Duration is negative'); } else { try { // Skip update duration if it's wrong diff --git a/packages/queue/src/utils.ts b/packages/queue/src/utils.ts index 51afd443..3fb6527e 100644 --- a/packages/queue/src/utils.ts +++ b/packages/queue/src/utils.ts @@ -5,5 +5,20 @@ export async function findJobByPrefix( matcher: string ) { const delayed = await queue.getJobs('delayed'); - return delayed.find((job) => job?.opts?.jobId?.startsWith(matcher)); + const filtered = delayed.filter((job) => + job?.opts?.jobId?.startsWith(matcher) + ); + const getTime = (val?: string) => { + if (!val) return null; + const match = val.match(/:(\d+)$/); + return match?.[1] ? parseInt(match[1], 10) : null; + }; + filtered.sort((a, b) => { + const aTime = getTime(a?.opts?.jobId); + const bTime = getTime(b?.opts?.jobId); + if (aTime === null) return 1; + if (bTime === null) return -1; + return aTime - bTime; + }); + return filtered[0]; }