Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

get oldest delayed job

Carl-Gerhard Lindesvärd3aecbfe62727
diff --git a/apps/worker/src/jobs/events.incoming-event.ts b/apps/worker/src/jobs/events.incoming-event.ts
index dcfd923e2..8e8d9aaad 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<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
diff --git a/packages/queue/src/utils.ts b/packages/queue/src/utils.ts
index 51afd4434..3fb6527e1 100644
--- a/packages/queue/src/utils.ts
+++ b/packages/queue/src/utils.ts
@@ -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];
}