fix(core): update

This commit is contained in:
2020-05-27 16:59:26 +00:00
parent b77677b089
commit d1ff77d9f5
10 changed files with 86 additions and 60 deletions

View File

@@ -4,48 +4,37 @@ import { CronManager } from './smarttime.classes.cronmanager';
export type TJobFunction = (() => void) | (() => Promise<any>);
export class CronJob {
public croner;
public status: 'started' | 'stopped' | 'initial' = 'initial';
public cronExpression: string;
public jobFunction: TJobFunction;
private cronInterval = plugins.cronParser.parseExpression('* * * * * *');
private nextExecutionUnix: number = 0;
constructor(cronManager: CronManager, cronExpressionArg: string, jobFunction: TJobFunction) {
this.cronExpression = cronExpressionArg;
this.jobFunction = jobFunction;
this.croner = plugins.croner(this.cronExpression);
}
/**
* checks wether the cronjob needs to be executed
*/
public checkExecution() {
public checkExecution(): number {
if (this.nextExecutionUnix === 0) {
this.nextExecutionUnix = this.cronInterval
.next()
.toDate()
.getTime();
return;
this.nextExecutionUnix = this.croner.msToNext();
}
if (Date.now() > this.nextExecutionUnix) {
this.jobFunction();
this.nextExecutionUnix = this.cronInterval
.next()
.toDate()
.getTime();
this.nextExecutionUnix = this.croner.msToNext();
}
return this.nextExecutionUnix;
}
public start() {
this.cronInterval = this.getCronInterval();
this.status = 'started';
}
public stop() {
this.status = 'stopped';
}
private getCronInterval() {
return plugins.cronParser.parseExpression(this.cronExpression);
}
}