fix(core): update

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

View File

@ -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;
}

View File

@ -9,7 +9,7 @@ export class CronManager {
constructor() {}
public addCronjob(cronIdentifierArg: string, cronFunctionArg: () => any) {
public addCronjob(cronIdentifierArg: string, cronFunctionArg: () => Promise<void>) {
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();
}
}