From db92311daa045c780ae17014d59c527c7733ba3e Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Mon, 7 Sep 2020 17:16:11 +0000 Subject: [PATCH] fix(core): update --- test/test.cronmanager.ts | 11 ++++++++++ ts/smarttime.classes.cronparser.ts | 33 ++++++++++++++++++++++-------- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/test/test.cronmanager.ts b/test/test.cronmanager.ts index 25cb171..9bd216d 100644 --- a/test/test.cronmanager.ts +++ b/test/test.cronmanager.ts @@ -44,6 +44,17 @@ tap.test('should create a valid cronJon', async (tools) => { await done2.promise; await done3.promise; testCronManager.stop(); + testCronManager.removeCronjob(cronJob3); +}); + +tap.test('runs every full minute', async (tools) => { + const done = tools.defer(); + const cronJob = testCronManager.addCronjob('0 * * * * *', async () => { + done.resolve(); + }); + testCronManager.start(); + await done.promise; + testCronManager.stop(); }); tap.start(); diff --git a/ts/smarttime.classes.cronparser.ts b/ts/smarttime.classes.cronparser.ts index 5686871..03333cc 100644 --- a/ts/smarttime.classes.cronparser.ts +++ b/ts/smarttime.classes.cronparser.ts @@ -27,7 +27,7 @@ export class CronParser { }; return findEvenMatch(startValue); } - if (parseInt(cronPart, 10)) { + if (parseInt(cronPart, 10) || cronPart === '0') { const match = parseInt(cronPart, 10); return match; } @@ -42,19 +42,36 @@ export class CronParser { const monthExpression = cronArray[4]; const yearExpression = cronArray[5]; - const currentDate = new Date(); - const currentSecond = currentDate.getSeconds() + 1; - const currentMinute = currentDate.getMinutes(); - const currentHour = currentDate.getHours(); - const currentDay = currentDate.getDate(); - const currentMonth = currentDate.getMonth(); - const currentYear = currentDate.getFullYear(); + let currentDate = new Date(); + let currentSecond = currentDate.getSeconds() + 1; + let currentMinute = currentDate.getMinutes(); + let currentHour = currentDate.getHours(); + let currentDay = currentDate.getDate(); + let currentMonth = currentDate.getMonth(); + let currentYear = currentDate.getFullYear(); const targetSecond = this.getNextPartMatch(secondExpression, currentSecond, 59); + if (targetSecond < currentSecond) { + currentMinute = (currentMinute + 1) % 59; + } const targetMinute = this.getNextPartMatch(minuteExpression, currentMinute, 59); + if (targetMinute < currentMinute) { + currentHour = (currentHour + 1) % 23; + } const targetHour = this.getNextPartMatch(hourExpression, currentHour, 23); + if (targetHour < currentHour) { + currentDay = (currentDay + 1) % 30; + } + const targetDay = currentDay; + if (targetDay < currentDay) { + currentMonth = (currentMonth + 1) % 11; + } + const targetMonth = currentMonth; + if (targetMonth < currentMonth) { + currentYear = (currentYear + 1); + } const targetYear = currentYear; const targetDate = new Date(