get oldest delayed job

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-05-05 15:20:55 +02:00
parent c0c80e7979
commit 3aecbfe627
2 changed files with 17 additions and 2 deletions

View File

@@ -181,7 +181,7 @@ export async function incomingEvent(job: Job<EventsQueuePayloadIncomingEvent>) {
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

View File

@@ -5,5 +5,20 @@ export async function findJobByPrefix<T>(
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];
}