Notes/src/services/scheduler.ts

65 lines
2.0 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 type 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 {
2025-01-09 18:07:02 +02:00
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) {
2025-01-24 00:02:45 +01:00
const instanceName = config.General.instanceName;
2021-04-08 21:22:47 +02:00
const currentHours = new Date().getHours();
2025-01-09 18:07:02 +02:00
const notes = attributeService.getNotesWithLabel("run", runAttrValue);
2021-04-08 21:22:47 +02:00
for (const note of notes) {
2025-01-09 18:07:02 +02:00
const runOnInstances = note.getLabelValues("runOnInstance");
2021-04-08 21:22:47 +02:00
const runAtHours = getRunAtHours(note);
2025-01-09 18:07:02 +02:00
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) {
2025-01-09 18:07:02 +02:00
setTimeout(
cls.wrap(() => runNotesWithLabel("backendStartup")),
10 * 1000
);
2025-01-09 18:07:02 +02:00
setInterval(
cls.wrap(() => runNotesWithLabel("hourly")),
3600 * 1000
);
2025-01-09 18:07:02 +02:00
setInterval(
cls.wrap(() => runNotesWithLabel("daily")),
24 * 3600 * 1000
);
2025-01-09 18:07:02 +02:00
setInterval(
cls.wrap(() => hiddenSubtreeService.checkHiddenSubtree()),
7 * 3600 * 1000
);
}
setInterval(() => protectedSessionService.checkProtectedSessionExpiration(), 30000);
2020-06-20 21:42:41 +02:00
});