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

feat(schedule): use croner library to check schedule #32573

Merged
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
Prev Previous commit
Next Next commit
fix: add support for timezones
  • Loading branch information
RahulGautamSingh committed Nov 18, 2024
commit 37d77004df2f4263a89f31705de723f23edba1e8
5 changes: 2 additions & 3 deletions lib/workers/repository/update/branch/schedule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,11 @@ describe('workers/repository/update/branch/schedule', () => {
});

describe('complex cron schedules', () => {
// eslint-disable-next-line
it.each`
viceice marked this conversation as resolved.
Show resolved Hide resolved
sched | tz | datetime | expected
${'* * 1-7 * 0'} | ${'Asia/Tokyo'} | ${'2024-10-04T10:50:00.000+0900'} | ${true}
${'* * 1-7 * 0'} | ${'Asia/Tokyo'} | ${'2024-10-13T10:50:00.000+0090'} | ${true}
${'* * 1-7 * 0'} | ${'Asia/Tokyo'} | ${'2024-10-16T10:50:00.000+0090'} | ${false}
${'* * 1-7 * 0'} | ${'Asia/Tokyo'} | ${'2024-10-13T10:50:00.000+0900'} | ${true}
${'* * 1-7 * 0'} | ${'Asia/Tokyo'} | ${'2024-10-16T10:50:00.000+0900'} | ${false}
`('$sched, $tz, $datetime', ({ sched, tz, datetime, expected }) => {
config.schedule = [sched];
config.timezone = tz;
Expand Down
9 changes: 5 additions & 4 deletions lib/workers/repository/update/branch/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ export function cronMatches(
timezone?: string,
): boolean {
const parsedCron = parseCron(cron, timezone);

// it will always parse because it is checked beforehand
// istanbul ignore if
if (!parsedCron) {
Expand All @@ -105,14 +104,17 @@ export function cronMatches(

// return the next date which matches the cron schedule
const nextRun = parsedCron.nextRun();

// istanbul ignore if: should not happen
if (!nextRun) {
logger.warn(`Invalid cron schedule ${cron}. No next run is possible`);
return false;
}

const nextDate: DateTime = DateTime.fromISO(nextRun.toISOString());
let nextDate: DateTime = DateTime.fromISO(nextRun.toISOString());
if (timezone) {
nextDate = nextDate.setZone(timezone);
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
}

if (nextDate.hour !== now.hour) {
return false;
}
Expand All @@ -125,7 +127,6 @@ export function cronMatches(
return false;
}

// Match
return true;
}

Expand Down