Disable background jobs on mobile
This commit is contained in:
+44
-35
@@ -2,57 +2,66 @@ import { Hook, Manifest } from "../types.ts";
|
||||
import { Cron } from "https://cdn.jsdelivr.net/gh/hexagon/croner@4/src/croner.js";
|
||||
import { safeRun } from "../util.ts";
|
||||
import { System } from "../system.ts";
|
||||
import { timingSafeEqual } from "https://deno.land/std@0.152.0/crypto/timing_safe_equal";
|
||||
|
||||
export type CronHookT = {
|
||||
cron?: string | string[];
|
||||
};
|
||||
|
||||
export class CronHook implements Hook<CronHookT> {
|
||||
tasks: Cron[] = [];
|
||||
constructor(private system: System<CronHookT>) {
|
||||
}
|
||||
|
||||
apply(system: System<CronHookT>): void {
|
||||
let tasks: Cron[] = [];
|
||||
this.system = system;
|
||||
system.on({
|
||||
plugLoaded: () => {
|
||||
reloadCrons();
|
||||
this.reloadCrons();
|
||||
},
|
||||
plugUnloaded() {
|
||||
reloadCrons();
|
||||
plugUnloaded: () => {
|
||||
this.reloadCrons();
|
||||
},
|
||||
});
|
||||
|
||||
reloadCrons();
|
||||
this.reloadCrons();
|
||||
}
|
||||
|
||||
function reloadCrons() {
|
||||
tasks.forEach((task) => task.stop());
|
||||
tasks = [];
|
||||
for (const plug of system.loadedPlugs.values()) {
|
||||
if (!plug.manifest) {
|
||||
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;
|
||||
}
|
||||
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) {
|
||||
tasks.push(
|
||||
new Cron(cronDef, () => {
|
||||
// console.log("Now acting on cron", cronDef);
|
||||
safeRun(async () => {
|
||||
try {
|
||||
await plug.invoke(name, [cronDef]);
|
||||
} catch (e: any) {
|
||||
console.error("Execution of cron function failed", e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
}
|
||||
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);
|
||||
safeRun(async () => {
|
||||
try {
|
||||
await plug.invoke(name, [cronDef]);
|
||||
} catch (e: any) {
|
||||
console.error("Execution of cron function failed", e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user