smarttime/ts/smarttime.classes.cronmanager.ts

50 lines
1.1 KiB
TypeScript
Raw Normal View History

2019-04-10 09:34:30 +00:00
import * as plugins from './smarttime.plugins';
2020-05-25 21:45:43 +00:00
import { CronJob } from './smarttime.classes.cronjob';
import { Timer } from './smarttime.classes.timer';
import { Interval } from './smarttime.classes.interval';
2019-04-10 09:34:30 +00:00
2019-04-10 12:06:20 +00:00
export class CronManager {
2020-05-25 21:45:43 +00:00
public cronInterval = new Interval(1000);
2019-04-10 12:06:20 +00:00
public status: 'started' | 'stopped' = 'stopped';
2020-05-25 21:45:43 +00:00
public cronjobs: CronJob[] = [];
2019-04-10 12:06:20 +00:00
2020-05-25 21:45:43 +00:00
constructor() {
this.cronInterval.addIntervalJob(() => {
for (const cronJob of this.cronjobs) {
cronJob.checkExecution();
}
});
}
2019-04-10 12:06:20 +00:00
2020-05-25 21:45:43 +00:00
public addCronjob(cronIdentifierArg: string, cronFunctionArg: () => any) {
const newCronJob = new CronJob(this, cronIdentifierArg, cronFunctionArg);
this.cronjobs.push(newCronJob);
2019-04-10 12:06:20 +00:00
if (this.status === 'started') {
newCronJob.start();
}
2019-06-17 14:54:39 +00:00
}
2019-04-10 12:06:20 +00:00
/**
* starts the cronjob
*/
public start() {
this.status = 'started';
for (const cron of this.cronjobs) {
cron.start();
}
2020-05-25 21:45:43 +00:00
this.cronInterval.start();
2019-04-10 12:06:20 +00:00
}
/**
* stops all cronjobs
*/
public stop() {
this.status = 'stopped';
for (const cron of this.cronjobs) {
cron.stop();
}
2020-05-25 21:45:43 +00:00
this.cronInterval.stop();
2019-04-10 12:06:20 +00:00
}
}