2022-10-10 12:50:21 +00:00
|
|
|
import { Hook, Manifest } from "../types.ts";
|
|
|
|
import { Cron } from "https://cdn.jsdelivr.net/gh/hexagon/croner@4/src/croner.js";
|
|
|
|
import { System } from "../system.ts";
|
2022-03-29 09:21:32 +00:00
|
|
|
export type CronHookT = {
|
2022-03-27 09:26:13 +00:00
|
|
|
cron?: string | string[];
|
2022-03-23 14:41:12 +00:00
|
|
|
};
|
|
|
|
|
2023-01-20 15:08:01 +00:00
|
|
|
export class CronHook implements Hook<CronHookT> {
|
2023-01-25 17:29:47 +00:00
|
|
|
tasks: Cron[] = [];
|
|
|
|
constructor(private system: System<CronHookT>) {
|
|
|
|
}
|
|
|
|
|
2022-03-29 09:21:32 +00:00
|
|
|
apply(system: System<CronHookT>): void {
|
2023-01-25 17:29:47 +00:00
|
|
|
this.system = system;
|
2022-03-23 14:41:12 +00:00
|
|
|
system.on({
|
2022-04-26 17:04:36 +00:00
|
|
|
plugLoaded: () => {
|
2023-01-25 17:29:47 +00:00
|
|
|
this.reloadCrons();
|
2022-03-23 14:41:12 +00:00
|
|
|
},
|
2023-01-25 17:29:47 +00:00
|
|
|
plugUnloaded: () => {
|
|
|
|
this.reloadCrons();
|
2022-03-23 14:41:12 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-01-25 17:29:47 +00:00
|
|
|
this.reloadCrons();
|
|
|
|
}
|
2022-03-23 14:41:12 +00:00
|
|
|
|
2023-01-25 17:29:47 +00:00
|
|
|
stop() {
|
|
|
|
this.tasks.forEach((task) => task.stop());
|
|
|
|
this.tasks = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
reloadCrons() {
|
|
|
|
this.stop();
|
|
|
|
for (const plug of this.system.loadedPlugs.values()) {
|
|
|
|
if (!plug.manifest) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (
|
|
|
|
const [name, functionDef] of Object.entries(
|
|
|
|
plug.manifest.functions,
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
if (!functionDef.cron) {
|
2022-03-27 09:26:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-01-25 17:29:47 +00:00
|
|
|
const crons = Array.isArray(functionDef.cron)
|
|
|
|
? functionDef.cron
|
|
|
|
: [functionDef.cron];
|
|
|
|
for (const cronDef of crons) {
|
|
|
|
this.tasks.push(
|
|
|
|
new Cron(cronDef, () => {
|
|
|
|
// console.log("Now acting on cron", cronDef);
|
2023-05-23 18:53:53 +00:00
|
|
|
(async () => {
|
2023-01-25 17:29:47 +00:00
|
|
|
try {
|
|
|
|
await plug.invoke(name, [cronDef]);
|
|
|
|
} catch (e: any) {
|
|
|
|
console.error("Execution of cron function failed", e);
|
|
|
|
}
|
2023-05-23 18:53:53 +00:00
|
|
|
})().catch(console.error);
|
2023-01-25 17:29:47 +00:00
|
|
|
}),
|
|
|
|
);
|
2022-03-23 14:41:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-29 09:21:32 +00:00
|
|
|
validateManifest(manifest: Manifest<CronHookT>): string[] {
|
2022-10-15 17:02:56 +00:00
|
|
|
const errors: string[] = [];
|
|
|
|
for (const functionDef of Object.values(manifest.functions)) {
|
2022-03-27 09:26:13 +00:00
|
|
|
if (!functionDef.cron) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const crons = Array.isArray(functionDef.cron)
|
|
|
|
? functionDef.cron
|
|
|
|
: [functionDef.cron];
|
2022-10-15 17:02:56 +00:00
|
|
|
for (const _cronDef of crons) {
|
2022-10-10 12:50:21 +00:00
|
|
|
// if (!cron.validate(cronDef)) {
|
|
|
|
// errors.push(`Invalid cron expression ${cronDef}`);
|
|
|
|
// }
|
2022-03-23 14:41:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors;
|
|
|
|
}
|
|
|
|
}
|