smarttime/ts/smarttime.classes.cronmanager.ts

87 lines
2.4 KiB
TypeScript
Raw Permalink Normal View History

2022-11-21 08:14:32 +00:00
import * as plugins from './smarttime.plugins.js';
2023-07-21 01:39:32 +00:00
import { CronJob, type TJobFunction } from './smarttime.classes.cronjob.js';
2022-11-21 08:14:32 +00:00
import { getMilliSecondsAsHumanReadableString } from './smarttime.units.js';
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
2023-01-06 19:05:02 +00:00
public addCronjob(cronIdentifierArg: string, cronFunctionArg: TJobFunction) {
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();
}
2023-10-20 10:50:47 +00:00
this.runCronCycle();
2020-07-12 00:25:55 +00:00
}
2019-04-10 12:06:20 +00:00
}
2023-10-20 10:50:47 +00:00
private async runCronCycle() {
this.executionTimeout = new plugins.smartdelay.Timeout(0);
do {
let nextRunningCronjob: CronJob;
for (const cronJob of this.cronjobs.getArray()) {
cronJob.checkExecution();
if (
!nextRunningCronjob ||
cronJob.getTimeToNextExecution() < nextRunningCronjob.getTimeToNextExecution()
) {
nextRunningCronjob = cronJob;
}
}
if (nextRunningCronjob) {
this.executionTimeout = new plugins.smartdelay.Timeout(
nextRunningCronjob.getTimeToNextExecution()
);
console.log(
`Next CronJob scheduled in ${getMilliSecondsAsHumanReadableString(
this.executionTimeout.getTimeLeft()
)}`
);
} else {
this.executionTimeout = new plugins.smartdelay.Timeout(1000);
console.log('no cronjobs specified! Checking again in 1 second');
}
await this.executionTimeout.promise;
} while (this.status === 'started');
};
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();
}
}
}