fix(core): update

This commit is contained in:
Philipp Kunz 2020-09-03 20:06:02 +00:00
parent f129ce42b6
commit 8e43ce7e9b
3 changed files with 42 additions and 19 deletions

View File

@ -11,16 +11,38 @@ tap.test('should create a valid instance of cronmanager', async () => {
tap.test('should create a valid cronJon', async (tools) => { tap.test('should create a valid cronJon', async (tools) => {
const done = tools.defer(); const done = tools.defer();
const done2 = tools.defer();
const done3 = tools.defer();
let counter = 0; let counter = 0;
testCronManager.addCronjob('*/2 * * * * *', () => { let counter2 = 0;
let counter3 = 0;
const cronJob = testCronManager.addCronjob('*/2 * * * * *', async () => {
if (counter === 10) { if (counter === 10) {
testCronManager.removeCronjob(cronJob);
done.resolve(); done.resolve();
} }
counter++; 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(); testCronManager.start();
await done.promise; await done.promise;
await done2.promise;
await done3.promise;
testCronManager.stop(); testCronManager.stop();
}); });

View File

@ -21,11 +21,14 @@ export class CronJob {
*/ */
public checkExecution(): number { public checkExecution(): number {
if (this.nextExecutionUnix === 0) { if (this.nextExecutionUnix === 0) {
this.nextExecutionUnix = this.croner.msToNext(); this.nextExecutionUnix = Date.now() + this.croner.msToNext();
} }
if (Date.now() > this.nextExecutionUnix) { if (Date.now() > this.nextExecutionUnix) {
this.jobFunction(); const maybePromise = this.jobFunction();
this.nextExecutionUnix = this.croner.msToNext(); if (maybePromise instanceof Promise) {
maybePromise.catch(e => console.log(e));
}
this.nextExecutionUnix = Date.now() + this.croner.msToNext();
} }
return this.nextExecutionUnix; return this.nextExecutionUnix;
} }

View File

@ -9,7 +9,7 @@ export class CronManager {
constructor() {} constructor() {}
public addCronjob(cronIdentifierArg: string, cronFunctionArg: () => any) { public addCronjob(cronIdentifierArg: string, cronFunctionArg: () => Promise<void>) {
const newCronJob = new CronJob(this, cronIdentifierArg, cronFunctionArg); const newCronJob = new CronJob(this, cronIdentifierArg, cronFunctionArg);
this.cronjobs.add(newCronJob); this.cronjobs.add(newCronJob);
if (this.status === 'started') { if (this.status === 'started') {
@ -33,17 +33,13 @@ export class CronManager {
for (const cronJob of this.cronjobs.getArray()) { for (const cronJob of this.cronjobs.getArray()) {
cronJob.start(); cronJob.start();
} }
this.executionTimeout = new plugins.smartdelay.Timeout(0); const runCronCycle = async () => {
this.executionTimeout = new plugins.smartdelay.Timeout(0);
// recursion do {
const runCheckExecution = () => {
console.log(
`Next CronJob scheduled in ${this.executionTimeout.getTimeLeft()} milliseconds`
);
this.executionTimeout.promise.then(() => {
let timeToNextOverallExecution: number; let timeToNextOverallExecution: number;
for (const cronJob of this.cronjobs.getArray()) { for (const cronJob of this.cronjobs.getArray()) {
const timeToNextJobExecution = cronJob.checkExecution(); const nextExecutionTime = cronJob.checkExecution();
const timeToNextJobExecution = nextExecutionTime - Date.now();
if ( if (
timeToNextJobExecution < timeToNextOverallExecution || timeToNextJobExecution < timeToNextOverallExecution ||
!timeToNextOverallExecution !timeToNextOverallExecution
@ -52,11 +48,13 @@ export class CronManager {
} }
} }
this.executionTimeout = new plugins.smartdelay.Timeout(timeToNextOverallExecution); 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');
}; };
runCronCycle();
runCheckExecution();
} }
} }