From fa8fd9fa35a9a86d5901704705d8031c3feb34e8 Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Mon, 7 Sep 2020 16:44:54 +0000 Subject: [PATCH] fix(core): update --- package.json | 1 - ts/smarttime.classes.cronjob.ts | 8 ++-- ts/smarttime.classes.cronparser.ts | 71 ++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 ts/smarttime.classes.cronparser.ts diff --git a/package.json b/package.json index bc51bcd..3be3010 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,6 @@ "@pushrocks/lik": "^4.0.17", "@pushrocks/smartdelay": "^2.0.10", "@pushrocks/smartpromise": "^3.0.2", - "croner": "^1.1.23", "dayjs": "^1.8.35", "is-nan": "^1.3.0" }, diff --git a/ts/smarttime.classes.cronjob.ts b/ts/smarttime.classes.cronjob.ts index d200ffe..8657a35 100644 --- a/ts/smarttime.classes.cronjob.ts +++ b/ts/smarttime.classes.cronjob.ts @@ -1,10 +1,12 @@ import * as plugins from './smarttime.plugins'; import { CronManager } from './smarttime.classes.cronmanager'; +import { CronParser } from './smarttime.classes.cronparser'; + export type TJobFunction = (() => void) | (() => Promise); export class CronJob { - public croner; + public cronParser: CronParser; public status: 'started' | 'stopped' | 'initial' = 'initial'; public cronExpression: string; public jobFunction: TJobFunction; @@ -13,7 +15,7 @@ export class CronJob { constructor(cronManager: CronManager, cronExpressionArg: string, jobFunction: TJobFunction) { this.cronExpression = cronExpressionArg; this.jobFunction = jobFunction; - // this.croner = plugins.croner(this.cronExpression); + this.cronParser = new CronParser(cronExpressionArg); } /** @@ -41,7 +43,7 @@ export class CronJob { * gets the time to next execution */ public getTimeToNextExecution() { - return 600000; + return this.cronParser.getMsToNextTimeMatch(); } public start() { diff --git a/ts/smarttime.classes.cronparser.ts b/ts/smarttime.classes.cronparser.ts new file mode 100644 index 0000000..5686871 --- /dev/null +++ b/ts/smarttime.classes.cronparser.ts @@ -0,0 +1,71 @@ +import * as plugins from './smarttime.plugins'; + +export class CronParser { + public cronExpression: string; + public get cronArray() { + return this.cronExpression.split(' '); + } + constructor(cronExpressionArg: string) { + this.cronExpression = cronExpressionArg; + if (this.cronArray.length < 6) { + throw new Error('CronParser needs second level accuracy'); + } + } + + private getNextPartMatch(cronPart: string, startValue: number, moduloArg: number) { + if (cronPart === '*') { + return startValue; + } + if (cronPart.includes('/')) { + const every = parseInt(cronPart.split('/')[1], 10); + const findEvenMatch = (recursionStartArg: number) => { + if (recursionStartArg % every === 0) { + return recursionStartArg; + } else { + return findEvenMatch(recursionStartArg + 1); + } + }; + return findEvenMatch(startValue); + } + if (parseInt(cronPart, 10)) { + const match = parseInt(cronPart, 10); + return match; + } + } + + public getMsToNextTimeMatch() { + const cronArray = this.cronArray; + const secondExpression = cronArray[0]; + const minuteExpression = cronArray[1]; + const hourExpression = cronArray[2]; + const dayExpression = cronArray[3]; + 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(); + + const targetSecond = this.getNextPartMatch(secondExpression, currentSecond, 59); + const targetMinute = this.getNextPartMatch(minuteExpression, currentMinute, 59); + const targetHour = this.getNextPartMatch(hourExpression, currentHour, 23); + const targetDay = currentDay; + const targetMonth = currentMonth; + const targetYear = currentYear; + + const targetDate = new Date( + targetYear, + targetMonth, + targetDay, + targetHour, + targetMinute, + targetSecond + ); + const targetTime = targetDate.getTime(); + return targetTime - Date.now(); + } +}