refactor: move cron schedule logging (#30611)

This commit is contained in:
RahulGautamSingh 2024-08-06 12:54:43 +05:30 committed by GitHub
parent 8e97c90a36
commit 33d8d588c5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 26 additions and 25 deletions

View file

@ -409,29 +409,30 @@ describe('workers/repository/update/branch/schedule', () => {
}); });
}); });
describe('cronstrue', () => { describe('log cron schedules', () => {
it('should correctly convert "0 22 4 * *" to human-readable format', () => { it('should correctly convert "* 22 4 * *" to human-readable format', () => {
const result = cronstrue.toString('0 22 4 * *'); const result = cronstrue.toString('* 22 4 * *');
expect(result).toBe('At 10:00 PM, on day 4 of the month'); 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', () => { it('should correctly convert "* */2 * * *" to human-readable format', () => {
const result = cronstrue.toString('*/2 * * * *'); const result = cronstrue.toString('* */2 * * *');
expect(result).toBe('Every 2 minutes'); expect(result).toBe('Every minute, every 2 hours');
}); });
it('should correctly convert "0 23 * * *" to human-readable format', () => { it('should correctly convert "* 23 * * *" to human-readable format', () => {
const result = cronstrue.toString('0 23 * * *'); const result = cronstrue.toString('* 23 * * *');
expect(result).toBe('At 11:00 PM'); 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"', () => { it('should not throw an error for an invalid cron expression "* * */2 6#1"', () => {
const result = cronstrue.toString('* * */2 6#1', { expect(() => {
cronstrue.toString('* * */2 6#1', {
throwExceptionOnParseError: false, throwExceptionOnParseError: false,
}); });
expect(result).toBe( }).not.toThrow();
'An error occured when generating the expression description. Check the cron expression syntax.',
);
}); });
}); });
}); });

View file

@ -26,10 +26,6 @@ function parseCron(
timezone?: string, timezone?: string,
): CronExpression | undefined { ): CronExpression | undefined {
try { try {
const cronScheduleSummary = cronstrue.toString(scheduleText, {
throwExceptionOnParseError: false,
});
logger.debug(`Human-readable summary for cron:: ${cronScheduleSummary}`);
return parseExpression(scheduleText, { tz: timezone }); return parseExpression(scheduleText, { tz: timezone });
} catch (err) { } catch (err) {
return undefined; return undefined;
@ -203,6 +199,10 @@ export function isScheduledNow(
const isWithinSchedule = configSchedule.some((scheduleText) => { const isWithinSchedule = configSchedule.some((scheduleText) => {
const cronSchedule = parseCron(scheduleText); const cronSchedule = parseCron(scheduleText);
if (cronSchedule) { if (cronSchedule) {
const cronScheduleSummary = cronstrue.toString(scheduleText, {
throwExceptionOnParseError: false,
});
logger.debug(`Human-readable summary for cron:: ${cronScheduleSummary}`);
// We have Cron syntax // We have Cron syntax
if (cronMatches(scheduleText, now, config.timezone)) { if (cronMatches(scheduleText, now, config.timezone)) {
logger.debug(`Matches schedule ${scheduleText}`); logger.debug(`Matches schedule ${scheduleText}`);