From 1997630d6e1a2647c04e9384436be9dcb7e73041 Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Fri, 20 Oct 2023 17:50:48 +0100 Subject: [PATCH] Add more detail to cron:backfill event --- RELEASE_NOTES.md | 2 ++ src/cron.ts | 9 +++++++-- src/interfaces.ts | 6 +++++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 03f982b7..2732347a 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -24,6 +24,8 @@ current worker. (This won't be useful until future schema changes.) Schema: trigger a graceful shutdown if a new Graphile Worker process migrates the database schema. +Events: add more detail to `cron:backfill` event. + ### v0.15.1 Fixes issues with graceful worker shutdowns: diff --git a/src/cron.ts b/src/cron.ts index 5cce6993..06e09d5a 100644 --- a/src/cron.ts +++ b/src/cron.ts @@ -9,6 +9,7 @@ import { Cron, CronJob, JobAndCronIdentifier, + JobAndCronIdentifierWithDetails, KnownCrontab, ParsedCronItem, RunnerOptions, @@ -39,6 +40,7 @@ function getBackfillAndUnknownItems( const backfillItemsAndDates: Array<{ item: ParsedCronItem; notBefore: Date; + itemDetails: KnownCrontab; }> = []; const unknownIdentifiers: string[] = []; for (const item of parsedCronItems) { @@ -51,6 +53,7 @@ function getBackfillAndUnknownItems( backfillItemsAndDates.push({ item, notBefore, + itemDetails: known, }); } else { unknownIdentifiers.push(item.identifier); @@ -228,10 +231,10 @@ async function registerAndBackfillItems( // The identifiers in this array are guaranteed to be unique, since cron // items are guaranteed to have unique identifiers. - const itemsToBackfill: Array = []; + const itemsToBackfill: Array = []; // See if anything needs backfilling for this timestamp - for (const { item, notBefore } of backfillItemsAndDates) { + for (const { item, notBefore, itemDetails } of backfillItemsAndDates) { if ( item.options.backfillPeriod >= timeAgo && unsafeTs >= notBefore && @@ -240,6 +243,8 @@ async function registerAndBackfillItems( itemsToBackfill.push({ identifier: item.identifier, job: makeJobForItem(item, ts, true), + known_since: itemDetails.known_since, + last_execution: itemDetails.last_execution, }); } } diff --git a/src/interfaces.ts b/src/interfaces.ts index 1d787da1..beafde39 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -579,6 +579,10 @@ export interface JobAndCronIdentifier { job: CronJob; identifier: string; } +export interface JobAndCronIdentifierWithDetails extends JobAndCronIdentifier { + known_since: Date; + last_execution: Date | null; +} export interface WorkerUtilsOptions extends SharedOptions {} @@ -774,7 +778,7 @@ export type WorkerEventMap = { /** **Experimental** When a number of jobs need backfilling for a particular timestamp. */ "cron:backfill": { cron: Cron; - itemsToBackfill: JobAndCronIdentifier[]; + itemsToBackfill: JobAndCronIdentifierWithDetails[]; timestamp: string; };