import type { Hook, Manifest } from "../types.ts"; import { Cron } from "https://deno.land/x/croner@4.4.1/src/croner.js"; import type { System } from "../system.ts"; import type { CronHookT } from "$lib/manifest.ts"; export class CronHook implements Hook { tasks: Cron[] = []; constructor(private system: System) { } apply(system: System): void { this.system = system; system.on({ plugLoaded: () => { this.reloadCrons(); }, plugUnloaded: () => { this.reloadCrons(); }, }); this.reloadCrons(); } 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) { continue; } 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); (async () => { try { if (await plug.canInvoke(name)) { await plug.invoke(name, [cronDef]); } } catch (e: any) { console.error("Execution of cron function failed", e); } })().catch(console.error); }), ); } } } } validateManifest(manifest: Manifest): string[] { const errors: string[] = []; for (const functionDef of Object.values(manifest.functions)) { if (!functionDef.cron) { continue; } const crons = Array.isArray(functionDef.cron) ? functionDef.cron : [functionDef.cron]; for (const _cronDef of crons) { // if (!cron.validate(cronDef)) { // errors.push(`Invalid cron expression ${cronDef}`); // } } } return errors; } }