Skip to content

Commit

Permalink
Add more detail to cron:backfill event
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie committed Oct 20, 2023
1 parent 1febd3d commit 1997630
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
2 changes: 2 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 7 additions & 2 deletions src/cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Cron,
CronJob,
JobAndCronIdentifier,
JobAndCronIdentifierWithDetails,
KnownCrontab,
ParsedCronItem,
RunnerOptions,
Expand Down Expand Up @@ -39,6 +40,7 @@ function getBackfillAndUnknownItems(
const backfillItemsAndDates: Array<{
item: ParsedCronItem;
notBefore: Date;
itemDetails: KnownCrontab;
}> = [];
const unknownIdentifiers: string[] = [];
for (const item of parsedCronItems) {
Expand All @@ -51,6 +53,7 @@ function getBackfillAndUnknownItems(
backfillItemsAndDates.push({
item,
notBefore,
itemDetails: known,
});
} else {
unknownIdentifiers.push(item.identifier);
Expand Down Expand Up @@ -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<JobAndCronIdentifier> = [];
const itemsToBackfill: Array<JobAndCronIdentifierWithDetails> = [];

// 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 &&
Expand All @@ -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,
});
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}

Expand Down Expand Up @@ -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;
};

Expand Down

0 comments on commit 1997630

Please sign in to comment.