diff --git a/test/test.cronmanager.ts b/test/test.cronmanager.ts index 4032fc2..25cb171 100644 --- a/test/test.cronmanager.ts +++ b/test/test.cronmanager.ts @@ -11,16 +11,38 @@ tap.test('should create a valid instance of cronmanager', async () => { tap.test('should create a valid cronJon', async (tools) => { const done = tools.defer(); + const done2 = tools.defer(); + const done3 = tools.defer(); let counter = 0; - testCronManager.addCronjob('*/2 * * * * *', () => { + let counter2 = 0; + let counter3 = 0; + const cronJob = testCronManager.addCronjob('*/2 * * * * *', async () => { if (counter === 10) { + testCronManager.removeCronjob(cronJob); done.resolve(); } counter++; - console.log(`hey ${counter}`); + console.log(`${new Date().getSeconds()} hey ${counter} -> runs every 2 seconds`); + }); + const cronJob2 = testCronManager.addCronjob('*/3 * * * * *', async () => { + if (counter2 === 10) { + testCronManager.removeCronjob(cronJob2); + done2.resolve(); + } + counter2++; + console.log(`${new Date().getSeconds()} hey ${counter2} -> runs every 3 seconds`); + }); + const cronJob3 = testCronManager.addCronjob('*/4 * * * * *', async () => { + if (counter3 === 10) { + done3.resolve(); + } + counter3++; + console.log(`${new Date().getSeconds()} hey ${counter3} -> runs every 4 seconds`); }); testCronManager.start(); await done.promise; + await done2.promise; + await done3.promise; testCronManager.stop(); }); diff --git a/ts/smarttime.classes.cronjob.ts b/ts/smarttime.classes.cronjob.ts index cadbbb1..efcabe4 100644 --- a/ts/smarttime.classes.cronjob.ts +++ b/ts/smarttime.classes.cronjob.ts @@ -21,11 +21,14 @@ export class CronJob { */ public checkExecution(): number { if (this.nextExecutionUnix === 0) { - this.nextExecutionUnix = this.croner.msToNext(); + this.nextExecutionUnix = Date.now() + this.croner.msToNext(); } if (Date.now() > this.nextExecutionUnix) { - this.jobFunction(); - this.nextExecutionUnix = this.croner.msToNext(); + const maybePromise = this.jobFunction(); + if (maybePromise instanceof Promise) { + maybePromise.catch(e => console.log(e)); + } + this.nextExecutionUnix = Date.now() + this.croner.msToNext(); } return this.nextExecutionUnix; } diff --git a/ts/smarttime.classes.cronmanager.ts b/ts/smarttime.classes.cronmanager.ts index 4673f18..a39a60f 100644 --- a/ts/smarttime.classes.cronmanager.ts +++ b/ts/smarttime.classes.cronmanager.ts @@ -9,7 +9,7 @@ export class CronManager { constructor() {} - public addCronjob(cronIdentifierArg: string, cronFunctionArg: () => any) { + public addCronjob(cronIdentifierArg: string, cronFunctionArg: () => Promise) { const newCronJob = new CronJob(this, cronIdentifierArg, cronFunctionArg); this.cronjobs.add(newCronJob); if (this.status === 'started') { @@ -33,17 +33,13 @@ export class CronManager { for (const cronJob of this.cronjobs.getArray()) { cronJob.start(); } - this.executionTimeout = new plugins.smartdelay.Timeout(0); - - // recursion - const runCheckExecution = () => { - console.log( - `Next CronJob scheduled in ${this.executionTimeout.getTimeLeft()} milliseconds` - ); - this.executionTimeout.promise.then(() => { + const runCronCycle = async () => { + this.executionTimeout = new plugins.smartdelay.Timeout(0); + do { let timeToNextOverallExecution: number; for (const cronJob of this.cronjobs.getArray()) { - const timeToNextJobExecution = cronJob.checkExecution(); + const nextExecutionTime = cronJob.checkExecution(); + const timeToNextJobExecution = nextExecutionTime - Date.now(); if ( timeToNextJobExecution < timeToNextOverallExecution || !timeToNextOverallExecution @@ -52,11 +48,13 @@ export class CronManager { } } this.executionTimeout = new plugins.smartdelay.Timeout(timeToNextOverallExecution); - runCheckExecution(); - }); + console.log( + `Next CronJob scheduled in ${this.executionTimeout.getTimeLeft()} milliseconds` + ); + await this.executionTimeout.promise; + } while (this.status === 'started'); }; - - runCheckExecution(); + runCronCycle(); } }