2018-04-01 21:27:46 -04:00
|
|
|
const scriptService = require('./script');
|
2018-03-28 23:41:22 -04:00
|
|
|
const cls = require('./cls');
|
2022-11-23 23:49:49 +01:00
|
|
|
const sqlInit = require('./sql_init');
|
2021-04-08 21:22:47 +02:00
|
|
|
const config = require('./config');
|
|
|
|
const log = require('./log');
|
2023-03-06 23:00:29 +01:00
|
|
|
const attributeService = require("../services/attributes");
|
2022-05-17 20:39:21 +02:00
|
|
|
const protectedSessionService = require("../services/protected_session");
|
2022-12-07 12:46:41 +01:00
|
|
|
const hiddenSubtreeService = require("./hidden_subtree");
|
2021-04-08 21:22:47 +02:00
|
|
|
|
|
|
|
function getRunAtHours(note) {
|
|
|
|
try {
|
|
|
|
return note.getLabelValues('runAtHour').map(hour => parseInt(hour));
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
log.error(`Could not parse runAtHour for note ${note.noteId}: ${e.message}`);
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
2018-03-02 20:56:58 -05:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function runNotesWithLabel(runAttrValue) {
|
2021-04-08 21:22:47 +02:00
|
|
|
const instanceName = config.General ? config.General.instanceName : null;
|
|
|
|
const currentHours = new Date().getHours();
|
2023-03-06 23:00:29 +01: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) {
|
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))
|
|
|
|
) {
|
|
|
|
scriptService.executeNoteNoException(note, {originEntity: note});
|
|
|
|
}
|
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) {
|
2021-01-15 20:12:14 +01:00
|
|
|
setTimeout(cls.wrap(() => runNotesWithLabel('backendStartup')), 10 * 1000);
|
2018-03-02 20:56:58 -05:00
|
|
|
|
2021-01-15 20:12:14 +01:00
|
|
|
setInterval(cls.wrap(() => runNotesWithLabel('hourly')), 3600 * 1000);
|
2018-03-02 20:56:58 -05:00
|
|
|
|
2021-01-15 20:12:14 +01:00
|
|
|
setInterval(cls.wrap(() => runNotesWithLabel('daily')), 24 * 3600 * 1000);
|
2023-05-07 10:54:06 +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
|
|
|
});
|