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';
|
2019-04-10 09:34:30 +00:00
|
|
|
|
2019-04-10 12:06:20 +00:00
|
|
|
export class CronManager {
|
2020-05-27 16:59:26 +00:00
|
|
|
public executionTimeout: plugins.smartdelay.Timeout<void>;
|
2020-05-25 21:45:43 +00:00
|
|
|
|
2019-04-10 12:06:20 +00:00
|
|
|
public status: 'started' | 'stopped' = 'stopped';
|
2020-07-11 23:36:24 +00:00
|
|
|
public cronjobs = new plugins.lik.ObjectMap<CronJob>();
|
2019-04-10 12:06:20 +00:00
|
|
|
|
2020-05-27 16:59:26 +00:00
|
|
|
constructor() {}
|
2019-04-10 12:06:20 +00:00
|
|
|
|
2020-09-03 20:06:02 +00:00
|
|
|
public addCronjob(cronIdentifierArg: string, cronFunctionArg: () => Promise<void>) {
|
2020-05-25 21:45:43 +00:00
|
|
|
const newCronJob = new CronJob(this, cronIdentifierArg, cronFunctionArg);
|
2020-07-11 23:36:24 +00:00
|
|
|
this.cronjobs.add(newCronJob);
|
2019-04-10 12:06:20 +00:00
|
|
|
if (this.status === 'started') {
|
|
|
|
newCronJob.start();
|
|
|
|
}
|
2020-07-11 23:31:09 +00:00
|
|
|
|
|
|
|
return newCronJob;
|
2019-06-17 14:54:39 +00:00
|
|
|
}
|
2019-04-10 12:06:20 +00:00
|
|
|
|
2020-07-11 23:36:24 +00:00
|
|
|
public removeCronjob(cronjobArg: CronJob) {
|
|
|
|
cronjobArg.stop();
|
|
|
|
this.cronjobs.remove(cronjobArg);
|
|
|
|
}
|
|
|
|
|
2019-04-10 12:06:20 +00:00
|
|
|
/**
|
|
|
|
* starts the cronjob
|
|
|
|
*/
|
|
|
|
public start() {
|
2020-07-12 00:25:55 +00:00
|
|
|
if (this.status !== 'started') {
|
|
|
|
this.status = 'started';
|
|
|
|
for (const cronJob of this.cronjobs.getArray()) {
|
|
|
|
cronJob.start();
|
|
|
|
}
|
2020-09-03 20:06:02 +00:00
|
|
|
const runCronCycle = async () => {
|
|
|
|
this.executionTimeout = new plugins.smartdelay.Timeout(0);
|
|
|
|
do {
|
2020-09-04 06:39:22 +00:00
|
|
|
let nextRunningCronjob: CronJob;
|
2020-07-12 00:25:55 +00:00
|
|
|
for (const cronJob of this.cronjobs.getArray()) {
|
2020-09-04 06:39:22 +00:00
|
|
|
cronJob.checkExecution();
|
2020-07-12 00:25:55 +00:00
|
|
|
if (
|
2020-09-04 06:39:22 +00:00
|
|
|
!nextRunningCronjob ||
|
|
|
|
cronJob.getTimeToNextExecution() < nextRunningCronjob.getTimeToNextExecution()
|
2020-07-12 00:25:55 +00:00
|
|
|
) {
|
2020-09-04 06:39:22 +00:00
|
|
|
nextRunningCronjob = cronJob;
|
2020-07-12 00:25:55 +00:00
|
|
|
}
|
2020-05-27 16:59:26 +00:00
|
|
|
}
|
2020-09-04 06:39:22 +00:00
|
|
|
this.executionTimeout = new plugins.smartdelay.Timeout(nextRunningCronjob.getTimeToNextExecution());
|
2020-09-03 20:06:02 +00:00
|
|
|
console.log(
|
|
|
|
`Next CronJob scheduled in ${this.executionTimeout.getTimeLeft()} milliseconds`
|
|
|
|
);
|
|
|
|
await this.executionTimeout.promise;
|
|
|
|
} while (this.status === 'started');
|
2020-07-12 00:25:55 +00:00
|
|
|
};
|
2020-09-03 20:06:02 +00:00
|
|
|
runCronCycle();
|
2020-07-12 00:25:55 +00:00
|
|
|
}
|
2019-04-10 12:06:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* stops all cronjobs
|
|
|
|
*/
|
|
|
|
public stop() {
|
2020-09-02 14:09:21 +00:00
|
|
|
if (this.status === 'started') {
|
|
|
|
this.status = 'stopped';
|
|
|
|
this.executionTimeout.cancel();
|
|
|
|
} else {
|
|
|
|
console.log(`You tried to stop a CronManager that was not actually started.`);
|
|
|
|
}
|
2020-07-11 23:36:24 +00:00
|
|
|
for (const cron of this.cronjobs.getArray()) {
|
2019-04-10 12:06:20 +00:00
|
|
|
cron.stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|