From 33d8d588c5f8f4ce4085066afaa90991fb75b181 Mon Sep 17 00:00:00 2001 From: RahulGautamSingh <rahultesnik@gmail.com> Date: Tue, 6 Aug 2024 12:54:43 +0530 Subject: [PATCH] refactor: move cron schedule logging (#30611) --- .../repository/update/branch/schedule.spec.ts | 35 ++++++++++--------- .../repository/update/branch/schedule.ts | 8 ++--- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/lib/workers/repository/update/branch/schedule.spec.ts b/lib/workers/repository/update/branch/schedule.spec.ts index c510a7b559..57c08c6445 100644 --- a/lib/workers/repository/update/branch/schedule.spec.ts +++ b/lib/workers/repository/update/branch/schedule.spec.ts @@ -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(); }); }); }); diff --git a/lib/workers/repository/update/branch/schedule.ts b/lib/workers/repository/update/branch/schedule.ts index 347ef787ae..60ac6e2f18 100644 --- a/lib/workers/repository/update/branch/schedule.ts +++ b/lib/workers/repository/update/branch/schedule.ts @@ -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; @@ -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}`); -- GitLab