Notes/src/services/scheduler.ts

55 lines
1.9 KiB
TypeScript
Raw Normal View History

import scriptService from "./script.js";
import cls from "./cls.js";
import sqlInit from "./sql_init.js";
import config from "./config.js";
import log from "./log.js";
import attributeService from "../services/attributes.js";
import protectedSessionService from "../services/protected_session.js";
import hiddenSubtreeService from "./hidden_subtree.js";
import BNote from "../becca/entities/bnote.js";
2021-04-08 21:22:47 +02:00
2024-04-04 23:08:32 +03:00
function getRunAtHours(note: BNote): number[] {
2021-04-08 21:22:47 +02:00
try {
return note.getLabelValues('runAtHour').map(hour => parseInt(hour));
2024-04-04 23:08:32 +03:00
} catch (e: any) {
2021-04-08 21:22:47 +02:00
log.error(`Could not parse runAtHour for note ${note.noteId}: ${e.message}`);
return [];
}
}
2024-04-04 23:08:32 +03:00
function runNotesWithLabel(runAttrValue: string) {
2021-04-08 21:22:47 +02:00
const instanceName = config.General ? config.General.instanceName : null;
const currentHours = new Date().getHours();
const notes = attributeService.getNotesWithLabel('run', runAttrValue);
2021-04-08 21:22:47 +02:00
for (const note of notes) {
2021-04-08 21:22:47 +02:00
const runOnInstances = note.getLabelValues('runOnInstance');
const runAtHours = getRunAtHours(note);
if ((runOnInstances.length === 0 || runOnInstances.includes(instanceName))
&& (runAtHours.length === 0 || runAtHours.includes(currentHours))
) {
2024-04-04 22:47:58 +03:00
scriptService.executeNoteNoException(note, { originEntity: note });
2021-04-08 21:22:47 +02:00
}
}
}
sqlInit.dbReady.then(() => {
2022-12-29 14:25:25 +01:00
cls.init(() => {
hiddenSubtreeService.checkHiddenSubtree();
});
2022-08-05 16:44:26 +02:00
if (!process.env.TRILIUM_SAFE_MODE) {
setTimeout(cls.wrap(() => runNotesWithLabel('backendStartup')), 10 * 1000);
setInterval(cls.wrap(() => runNotesWithLabel('hourly')), 3600 * 1000);
setInterval(cls.wrap(() => runNotesWithLabel('daily')), 24 * 3600 * 1000);
setInterval(cls.wrap(() => hiddenSubtreeService.checkHiddenSubtree()), 7 * 3600 * 1000);
}
setInterval(() => protectedSessionService.checkProtectedSessionExpiration(), 30000);
2020-06-20 21:42:41 +02:00
});