Skip to content

Commit 6684e2a

Browse files
authored
Make backfillPeriod optional in exported CronItem type (#516)
2 parents c70e9db + 774e1eb commit 6684e2a

File tree

4 files changed

+43
-9
lines changed

4 files changed

+43
-9
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Read more:
4343
experimental!
4444
- `helpers.abortPromise` added; will reject when `abortSignal` aborts (useful
4545
for `Promise.race()`)
46+
- `backfillPeriod` is now marked as optional in TypeScript (defaults to 0).
4647

4748
## v0.16.6
4849

__tests__/crontab.test.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CronItemOptions, ParsedCronMatch } from "../src";
2-
import { parseCrontab } from "../src/crontab";
2+
import { parseCronItem, parseCrontab } from "../src/crontab";
33

44
// 0...59
55
const ALL_MINUTES = Array.from(Array(60).keys());
@@ -237,3 +237,26 @@ describe("gives error on syntax error", () => {
237237
);
238238
});
239239
});
240+
241+
describe("parses JS cron items correctly", () => {
242+
test("defaults backfillPeriod to 0", () => {
243+
expect(
244+
parseCronItem({
245+
task: "task",
246+
match: "* * * * *",
247+
options: {},
248+
}).options.backfillPeriod,
249+
).toEqual(0);
250+
});
251+
252+
test("validates match strings", () => {
253+
const matchString = "foo";
254+
expect(() =>
255+
parseCronItem({
256+
task: "task",
257+
match: matchString,
258+
options: {},
259+
}),
260+
).toThrow(`Invalid cron pattern '${matchString}' in parseCronItem call`);
261+
});
262+
});

src/crontab.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
CronItem,
2121
CronItemOptions,
2222
ParsedCronItem,
23+
ParsedCronItemOptions,
2324
} from "./interfaces";
2425
import { coerceError } from "./lib";
2526

@@ -60,7 +61,7 @@ const parseTimePhrase = (timePhrase: string): number => {
6061
const parseCrontabOptions = (
6162
lineNumber: number,
6263
optionsString: string | undefined,
63-
): { options: CronItemOptions; identifier: string | undefined } => {
64+
): { options: ParsedCronItemOptions; identifier: string | undefined } => {
6465
const parsed = optionsString != null ? parse(optionsString) : {};
6566
let backfillPeriod: number | undefined = undefined;
6667
let maxAttempts: number | undefined = undefined;
@@ -149,9 +150,8 @@ const parseCrontabOptions = (
149150
}
150151
});
151152

152-
if (!backfillPeriod) {
153-
backfillPeriod = 0;
154-
}
153+
// Apply some sensible defaults
154+
backfillPeriod ??= 0;
155155
if (!jobKeyMode && jobKey) {
156156
jobKeyMode = "replace";
157157
}
@@ -294,7 +294,7 @@ export const parseCronItem = (
294294
const {
295295
match: rawMatch,
296296
task,
297-
options = {} as CronItemOptions,
297+
options,
298298
payload = {},
299299
identifier = task,
300300
} = cronItem;
@@ -308,11 +308,17 @@ export const parseCronItem = (
308308
if (typeof match !== "function") {
309309
throw new Error("Invalid 'match' configuration");
310310
}
311+
312+
const parsedOptions: ParsedCronItemOptions = {
313+
...options,
314+
backfillPeriod: options?.backfillPeriod ?? 0,
315+
};
316+
311317
return {
312318
[$$isParsed]: true,
313319
match,
314320
task,
315-
options,
321+
options: parsedOptions,
316322
payload,
317323
identifier,
318324
};

src/interfaces.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ export interface WatchedCronItems {
331331
*/
332332
export interface CronItemOptions {
333333
/** How far back (in milliseconds) should we backfill jobs when worker starts? (Only backfills since when the identifier was first used.) */
334-
backfillPeriod: number;
334+
backfillPeriod?: number;
335335

336336
/** Optionally override the default job max_attempts */
337337
maxAttempts?: number;
@@ -353,6 +353,10 @@ export interface CronItemOptions {
353353
jobKeyMode?: "replace" | "preserve_run_at";
354354
}
355355

356+
export interface ParsedCronItemOptions extends CronItemOptions {
357+
backfillPeriod: number;
358+
}
359+
356360
/**
357361
* Crontab ranges from the minute, hour, day of month, month and day of week
358362
* parts of the crontab line
@@ -419,7 +423,7 @@ export interface ParsedCronItem {
419423
task: string;
420424

421425
/** Options influencing backfilling and properties of the scheduled job */
422-
options: CronItemOptions;
426+
options: ParsedCronItemOptions;
423427

424428
/** A payload object to merge into the default cron payload object for the scheduled job */
425429
payload: { [key: string]: unknown } | null;

0 commit comments

Comments
 (0)