From 457182a97a93e90d57355e7569cbcb276f676a04 Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Fri, 4 Sep 2020 06:39:22 +0000 Subject: [PATCH] fix(core): update --- ts/smarttime.classes.cronjob.ts | 15 +++++++++++++-- ts/smarttime.classes.cronmanager.ts | 13 ++++++------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/ts/smarttime.classes.cronjob.ts b/ts/smarttime.classes.cronjob.ts index efcabe4..333c0b3 100644 --- a/ts/smarttime.classes.cronjob.ts +++ b/ts/smarttime.classes.cronjob.ts @@ -21,18 +21,29 @@ export class CronJob { */ public checkExecution(): number { if (this.nextExecutionUnix === 0) { - this.nextExecutionUnix = Date.now() + this.croner.msToNext(); + this.getNextExecutionTime(); } if (Date.now() > this.nextExecutionUnix) { const maybePromise = this.jobFunction(); if (maybePromise instanceof Promise) { maybePromise.catch(e => console.log(e)); } - this.nextExecutionUnix = Date.now() + this.croner.msToNext(); + this.nextExecutionUnix = this.getNextExecutionTime(); } return this.nextExecutionUnix; } + public getNextExecutionTime() { + return this.nextExecutionUnix = Date.now() + this.getTimeToNextExecution(); + } + + /** + * gets the time to next execution + */ + public getTimeToNextExecution() { + return this.croner.msToNext(); + } + public start() { this.status = 'started'; } diff --git a/ts/smarttime.classes.cronmanager.ts b/ts/smarttime.classes.cronmanager.ts index a39a60f..6ad80f7 100644 --- a/ts/smarttime.classes.cronmanager.ts +++ b/ts/smarttime.classes.cronmanager.ts @@ -36,18 +36,17 @@ export class CronManager { const runCronCycle = async () => { this.executionTimeout = new plugins.smartdelay.Timeout(0); do { - let timeToNextOverallExecution: number; + let nextRunningCronjob: CronJob; for (const cronJob of this.cronjobs.getArray()) { - const nextExecutionTime = cronJob.checkExecution(); - const timeToNextJobExecution = nextExecutionTime - Date.now(); + cronJob.checkExecution(); if ( - timeToNextJobExecution < timeToNextOverallExecution || - !timeToNextOverallExecution + !nextRunningCronjob || + cronJob.getTimeToNextExecution() < nextRunningCronjob.getTimeToNextExecution() ) { - timeToNextOverallExecution = timeToNextJobExecution; + nextRunningCronjob = cronJob; } } - this.executionTimeout = new plugins.smartdelay.Timeout(timeToNextOverallExecution); + this.executionTimeout = new plugins.smartdelay.Timeout(nextRunningCronjob.getTimeToNextExecution()); console.log( `Next CronJob scheduled in ${this.executionTimeout.getTimeLeft()} milliseconds` );