Skip to content

Commit

Permalink
refactor: move cron schedule logging (#30611)
Browse files Browse the repository at this point in the history
  • Loading branch information
RahulGautamSingh authored Aug 6, 2024
1 parent 8e97c90 commit 33d8d58
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
35 changes: 18 additions & 17 deletions lib/workers/repository/update/branch/schedule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,29 +409,30 @@ describe('workers/repository/update/branch/schedule', () => {
});
});

describe('cronstrue', () => {
it('should correctly convert "0 22 4 * *" to human-readable format', () => {
const result = cronstrue.toString('0 22 4 * *');
expect(result).toBe('At 10:00 PM, on day 4 of the month');
describe('log cron schedules', () => {
it('should correctly convert "* 22 4 * *" to human-readable format', () => {
const result = cronstrue.toString('* 22 4 * *');
expect(result).toBe(
'Every minute, between 10:00 PM and 10:59 PM, on day 4 of the month',
);
});

it('should correctly convert "*/2 * * * *" to human-readable format', () => {
const result = cronstrue.toString('*/2 * * * *');
expect(result).toBe('Every 2 minutes');
it('should correctly convert "* */2 * * *" to human-readable format', () => {
const result = cronstrue.toString('* */2 * * *');
expect(result).toBe('Every minute, every 2 hours');
});

it('should correctly convert "0 23 * * *" to human-readable format', () => {
const result = cronstrue.toString('0 23 * * *');
expect(result).toBe('At 11:00 PM');
it('should correctly convert "* 23 * * *" to human-readable format', () => {
const result = cronstrue.toString('* 23 * * *');
expect(result).toBe('Every minute, between 11:00 PM and 11:59 PM');
});

it('should throw an error for an invalid cron expression "* * */2 6#1"', () => {
const result = cronstrue.toString('* * */2 6#1', {
throwExceptionOnParseError: false,
});
expect(result).toBe(
'An error occured when generating the expression description. Check the cron expression syntax.',
);
it('should not throw an error for an invalid cron expression "* * */2 6#1"', () => {
expect(() => {
cronstrue.toString('* * */2 6#1', {
throwExceptionOnParseError: false,
});
}).not.toThrow();
});
});
});
8 changes: 4 additions & 4 deletions lib/workers/repository/update/branch/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ function parseCron(
timezone?: string,
): CronExpression | undefined {
try {
const cronScheduleSummary = cronstrue.toString(scheduleText, {
throwExceptionOnParseError: false,
});
logger.debug(`Human-readable summary for cron:: ${cronScheduleSummary}`);
return parseExpression(scheduleText, { tz: timezone });
} catch (err) {
return undefined;
Expand Down Expand Up @@ -203,6 +199,10 @@ export function isScheduledNow(
const isWithinSchedule = configSchedule.some((scheduleText) => {
const cronSchedule = parseCron(scheduleText);
if (cronSchedule) {
const cronScheduleSummary = cronstrue.toString(scheduleText, {
throwExceptionOnParseError: false,
});
logger.debug(`Human-readable summary for cron:: ${cronScheduleSummary}`);
// We have Cron syntax
if (cronMatches(scheduleText, now, config.timezone)) {
logger.debug(`Matches schedule ${scheduleText}`);
Expand Down

0 comments on commit 33d8d58

Please sign in to comment.