smarttime/ts/smarttime.classes.cronmanager.ts
2020-05-25 21:45:43 +00:00

50 lines
1.1 KiB
TypeScript

import * as plugins from './smarttime.plugins';
import { CronJob } from './smarttime.classes.cronjob';
import { Timer } from './smarttime.classes.timer';
import { Interval } from './smarttime.classes.interval';
export class CronManager {
public cronInterval = new Interval(1000);
public status: 'started' | 'stopped' = 'stopped';
public cronjobs: CronJob[] = [];
constructor() {
this.cronInterval.addIntervalJob(() => {
for (const cronJob of this.cronjobs) {
cronJob.checkExecution();
}
});
}
public addCronjob(cronIdentifierArg: string, cronFunctionArg: () => any) {
const newCronJob = new CronJob(this, cronIdentifierArg, cronFunctionArg);
this.cronjobs.push(newCronJob);
if (this.status === 'started') {
newCronJob.start();
}
}
/**
* starts the cronjob
*/
public start() {
this.status = 'started';
for (const cron of this.cronjobs) {
cron.start();
}
this.cronInterval.start();
}
/**
* stops all cronjobs
*/
public stop() {
this.status = 'stopped';
for (const cron of this.cronjobs) {
cron.stop();
}
this.cronInterval.stop();
}
}