2024-07-18 21:35:17 +03:00
|
|
|
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";
|
2025-01-13 23:18:10 +02:00
|
|
|
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 [];
|
|
|
|
}
|
|
|
|
}
|
2018-03-02 20:56:58 -05:00
|
|
|
|
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
|
|
|
|
2018-03-02 20:56:58 -05: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
|
|
|
}
|
2018-03-02 20:56:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-23 23:49:49 +01:00
|
|
|
sqlInit.dbReady.then(() => {
|
2022-12-29 14:25:25 +01:00
|
|
|
cls.init(() => {
|
|
|
|
hiddenSubtreeService.checkHiddenSubtree();
|
|
|
|
});
|
2022-08-05 16:44:26 +02:00
|
|
|
|
2022-12-27 14:44:28 +01:00
|
|
|
if (!process.env.TRILIUM_SAFE_MODE) {
|
2025-01-09 18:07:02 +02:00
|
|
|
setTimeout(
|
|
|
|
cls.wrap(() => runNotesWithLabel("backendStartup")),
|
|
|
|
10 * 1000
|
|
|
|
);
|
2018-03-02 20:56:58 -05:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
setInterval(
|
|
|
|
cls.wrap(() => runNotesWithLabel("hourly")),
|
|
|
|
3600 * 1000
|
|
|
|
);
|
2018-03-02 20:56:58 -05:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
setInterval(
|
|
|
|
cls.wrap(() => runNotesWithLabel("daily")),
|
|
|
|
24 * 3600 * 1000
|
|
|
|
);
|
2023-05-07 10:54:06 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
setInterval(
|
|
|
|
cls.wrap(() => hiddenSubtreeService.checkHiddenSubtree()),
|
|
|
|
7 * 3600 * 1000
|
|
|
|
);
|
2021-01-15 20:12:14 +01:00
|
|
|
}
|
2022-05-17 20:39:21 +02:00
|
|
|
|
|
|
|
setInterval(() => protectedSessionService.checkProtectedSessionExpiration(), 30000);
|
2020-06-20 21:42:41 +02:00
|
|
|
});
|