Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal refactor of cron #250

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 1 addition & 50 deletions __tests__/cron.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { parseCronItem, run, TimestampDigest } from "../src";
import { cronItemMatches } from "../src/cronMatcher";
import { run } from "../src";
import {
ESCAPED_GRAPHILE_WORKER_SCHEMA,
EventMonitor,
Expand Down Expand Up @@ -145,51 +144,3 @@ test("backfills if identifier already registered (25h)", () =>
expect(jobs.every((j) => j.task_identifier === "do_it")).toBe(true);
}
}));

describe("matches datetime", () => {
const makeMatcher = (pattern: string) => (digest: TimestampDigest) => {
return cronItemMatches(
parseCronItem({ pattern, task: "" }, "test"),
digest,
);
};

const _0_0_1_1_0 = { min: 0, hour: 0, date: 1, month: 1, dow: 0 };
const _0_0_1_1_5 = { ..._0_0_1_1_0, dow: 5 };
const _0_0_1_1_4 = { ..._0_0_1_1_0, dow: 4 };
const _0_15_1_7_0 = { ..._0_0_1_1_0, hour: 15, month: 7 };
const _0_15_4_7_2 = { ..._0_0_1_1_0, hour: 15, date: 4, month: 7, dow: 2 };
const _0_15_4_7_5 = { ..._0_0_1_1_0, hour: 15, date: 4, month: 7, dow: 5 };
const _6_15_4_7_5 = { min: 6, hour: 15, date: 4, month: 7, dow: 5 };

test("every minute", () => {
const match = makeMatcher("* * * * *");
expect(match(_0_0_1_1_0)).toBeTruthy();
expect(match(_0_0_1_1_5)).toBeTruthy();
expect(match(_0_0_1_1_4)).toBeTruthy();
expect(match(_0_15_1_7_0)).toBeTruthy();
expect(match(_0_15_4_7_2)).toBeTruthy();
expect(match(_0_15_4_7_5)).toBeTruthy();
expect(match(_6_15_4_7_5)).toBeTruthy();
});
test("dow range", () => {
const match = makeMatcher("* * * * 5-6");
expect(match(_0_0_1_1_0)).toBeFalsy();
expect(match(_0_0_1_1_5)).toBeTruthy();
expect(match(_0_0_1_1_4)).toBeFalsy();
expect(match(_0_15_1_7_0)).toBeFalsy();
expect(match(_0_15_4_7_2)).toBeFalsy();
expect(match(_0_15_4_7_5)).toBeTruthy();
expect(match(_6_15_4_7_5)).toBeTruthy();
});
test("dow and date range", () => {
const match = makeMatcher("0-5 15 3-4 7 0-2");
expect(match(_0_0_1_1_0)).toBeFalsy();
expect(match(_0_0_1_1_5)).toBeFalsy();
expect(match(_0_0_1_1_4)).toBeFalsy();
expect(match(_0_15_1_7_0)).toBeTruthy();
expect(match(_0_15_4_7_2)).toBeTruthy();
expect(match(_0_15_4_7_5)).toBeTruthy();
expect(match(_6_15_4_7_5)).toBeFalsy();
});
});
47 changes: 47 additions & 0 deletions __tests__/cronMatcher.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { cronItemMatches, parseCronString } from "../src/cronMatcher";
import { TimestampDigest } from "../src/interfaces";

describe("matches datetime", () => {
const makeMatcher = (pattern: string) => (digest: TimestampDigest) => {
return cronItemMatches(parseCronString(pattern, "test"), digest);
};

const _0_0_1_1_0 = { min: 0, hour: 0, date: 1, month: 1, dow: 0 };
const _0_0_1_1_5 = { ..._0_0_1_1_0, dow: 5 };
const _0_0_1_1_4 = { ..._0_0_1_1_0, dow: 4 };
const _0_15_1_7_0 = { ..._0_0_1_1_0, hour: 15, month: 7 };
const _0_15_4_7_2 = { ..._0_0_1_1_0, hour: 15, date: 4, month: 7, dow: 2 };
const _0_15_4_7_5 = { ..._0_0_1_1_0, hour: 15, date: 4, month: 7, dow: 5 };
const _6_15_4_7_5 = { min: 6, hour: 15, date: 4, month: 7, dow: 5 };

test("every minute", () => {
const match = makeMatcher("* * * * *");
expect(match(_0_0_1_1_0)).toBeTruthy();
expect(match(_0_0_1_1_5)).toBeTruthy();
expect(match(_0_0_1_1_4)).toBeTruthy();
expect(match(_0_15_1_7_0)).toBeTruthy();
expect(match(_0_15_4_7_2)).toBeTruthy();
expect(match(_0_15_4_7_5)).toBeTruthy();
expect(match(_6_15_4_7_5)).toBeTruthy();
});
test("dow range", () => {
const match = makeMatcher("* * * * 5-6");
expect(match(_0_0_1_1_0)).toBeFalsy();
expect(match(_0_0_1_1_5)).toBeTruthy();
expect(match(_0_0_1_1_4)).toBeFalsy();
expect(match(_0_15_1_7_0)).toBeFalsy();
expect(match(_0_15_4_7_2)).toBeFalsy();
expect(match(_0_15_4_7_5)).toBeTruthy();
expect(match(_6_15_4_7_5)).toBeTruthy();
});
test("dow and date range", () => {
const match = makeMatcher("0-5 15 3-4 7 0-2");
expect(match(_0_0_1_1_0)).toBeFalsy();
expect(match(_0_0_1_1_5)).toBeFalsy();
expect(match(_0_0_1_1_4)).toBeFalsy();
expect(match(_0_15_1_7_0)).toBeTruthy();
expect(match(_0_15_4_7_2)).toBeTruthy();
expect(match(_0_15_4_7_5)).toBeTruthy();
expect(match(_6_15_4_7_5)).toBeFalsy();
});
});
22 changes: 20 additions & 2 deletions src/cronMatcher.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import {
CRONTAB_NUMBER,
CRONTAB_RANGE,
CRONTAB_TIME_PARTS,
CRONTAB_WILDCARD,
} from "./cronConstants";
import { ParsedCronItem, TimestampDigest } from "./interfaces";
import { TimestampDigest } from "./interfaces";

// Crontab ranges from the minute, hour, day of month, month and day of week parts of the crontab line
interface CrontabRanges {
minutes: number[];
hours: number[];
dates: number[];
months: number[];
dows: number[];
}
wokalski marked this conversation as resolved.
Show resolved Hide resolved

/**
* Returns true if the cronItem should fire for the given timestamp digest,
* false otherwise.
*/
export function cronItemMatches(
cronItem: ParsedCronItem,
cronItem: CrontabRanges,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to self: this is not a breaking change.

digest: TimestampDigest,
): boolean {
const { min, hour, date, month, dow } = digest;
Expand Down Expand Up @@ -170,3 +180,11 @@ export function parseCrontabRanges(matches: string[], source: string) {
);
return { minutes, hours, dates, months, dows };
}

export const parseCronString = (pattern: string, source: string = "") => {
wokalski marked this conversation as resolved.
Show resolved Hide resolved
const matches = CRONTAB_TIME_PARTS.exec(pattern);
if (!matches) {
throw new Error(`Invalid cron pattern '${pattern}' in ${source}`);
}
return parseCrontabRanges(matches, source);
};
11 changes: 3 additions & 8 deletions src/crontab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import {
CRONTAB_OPTIONS_MAX,
CRONTAB_OPTIONS_PRIORITY,
CRONTAB_OPTIONS_QUEUE,
CRONTAB_TIME_PARTS,
PERIOD_DURATIONS,
TIMEPHRASE_PART,
} from "./cronConstants";
import { parseCrontabRanges } from "./cronMatcher";
import { parseCronString, parseCrontabRanges } from "./cronMatcher";
import { CronItem, CronItemOptions, ParsedCronItem } from "./interfaces";

/**
Expand Down Expand Up @@ -267,12 +266,8 @@ export const parseCronItem = (
payload = {},
identifier = task,
} = cronItem;
const matches = CRONTAB_TIME_PARTS.exec(pattern);
if (!matches) {
throw new Error(`Invalid cron pattern '${pattern}' in ${source}`);
}
const { minutes, hours, dates, months, dows } = parseCrontabRanges(
matches,
const { minutes, hours, dates, months, dows } = parseCronString(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be parseCronRangeString?

pattern,
source,
);
const item: ParsedCronItem = {
Expand Down