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');
|
2021-05-02 19:59:16 +02:00
|
|
|
const sql = require("./sql");
|
2021-06-29 22:15:57 +02:00
|
|
|
const becca = require("../becca/becca");
|
2021-09-16 22:20:59 +02:00
|
|
|
const specialNotesService = require("../services/special_notes");
|
2022-05-17 20:39:21 +02:00
|
|
|
const protectedSessionService = require("../services/protected_session");
|
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-05-02 19:59:16 +02:00
|
|
|
// TODO: should be refactored into becca search
|
|
|
|
const noteIds = sql.getColumn(`
|
|
|
|
SELECT notes.noteId
|
2018-03-02 20:56:58 -05:00
|
|
|
FROM notes
|
2021-04-08 21:22:47 +02:00
|
|
|
JOIN attributes ON attributes.noteId = notes.noteId
|
|
|
|
AND attributes.isDeleted = 0
|
|
|
|
AND attributes.type = 'label'
|
|
|
|
AND attributes.name = 'run'
|
|
|
|
AND attributes.value = ?
|
2018-03-02 20:56:58 -05:00
|
|
|
WHERE
|
|
|
|
notes.type = 'code'
|
|
|
|
AND notes.isDeleted = 0`, [runAttrValue]);
|
|
|
|
|
2021-05-02 19:59:16 +02:00
|
|
|
const notes = becca.getNotes(noteIds);
|
|
|
|
|
2021-04-08 21:22:47 +02:00
|
|
|
const instanceName = config.General ? config.General.instanceName : null;
|
|
|
|
const currentHours = new Date().getHours();
|
|
|
|
|
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(() => {
|
2021-01-15 20:12:14 +01:00
|
|
|
if (!process.env.TRILIUM_SAFE_MODE) {
|
2022-08-05 16:44:26 +02:00
|
|
|
cls.init(() => specialNotesService.createMissingSpecialNotes());
|
|
|
|
|
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);
|
|
|
|
}
|
2022-05-17 20:39:21 +02:00
|
|
|
|
|
|
|
setInterval(() => protectedSessionService.checkProtectedSessionExpiration(), 30000);
|
2020-06-20 21:42:41 +02:00
|
|
|
});
|