2018-04-02 09:27:46 +08:00
|
|
|
const scriptService = require('./script');
|
2018-03-31 21:07:58 +08:00
|
|
|
const repository = require('./repository');
|
2018-03-29 11:41:22 +08:00
|
|
|
const cls = require('./cls');
|
2020-06-21 03:42:41 +08:00
|
|
|
const sqlInit = require('./sql_init');
|
2021-04-09 03:22:47 +08:00
|
|
|
const config = require('./config');
|
|
|
|
const log = require('./log');
|
|
|
|
|
|
|
|
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-03 09:56:58 +08:00
|
|
|
|
2020-06-20 18:31:38 +08:00
|
|
|
function runNotesWithLabel(runAttrValue) {
|
|
|
|
const notes = repository.getEntities(`
|
2018-03-03 09:56:58 +08:00
|
|
|
SELECT notes.*
|
|
|
|
FROM notes
|
2021-04-09 03:22:47 +08: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-03 09:56:58 +08:00
|
|
|
WHERE
|
|
|
|
notes.type = 'code'
|
|
|
|
AND notes.isDeleted = 0`, [runAttrValue]);
|
|
|
|
|
2021-04-09 03:22:47 +08:00
|
|
|
const instanceName = config.General ? config.General.instanceName : null;
|
|
|
|
const currentHours = new Date().getHours();
|
|
|
|
|
2018-03-03 09:56:58 +08:00
|
|
|
for (const note of notes) {
|
2021-04-09 03:22:47 +08: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-03 09:56:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 03:42:41 +08:00
|
|
|
sqlInit.dbReady.then(() => {
|
2021-01-16 03:12:14 +08:00
|
|
|
if (!process.env.TRILIUM_SAFE_MODE) {
|
|
|
|
setTimeout(cls.wrap(() => runNotesWithLabel('backendStartup')), 10 * 1000);
|
2018-03-03 09:56:58 +08:00
|
|
|
|
2021-01-16 03:12:14 +08:00
|
|
|
setInterval(cls.wrap(() => runNotesWithLabel('hourly')), 3600 * 1000);
|
2018-03-03 09:56:58 +08:00
|
|
|
|
2021-01-16 03:12:14 +08:00
|
|
|
setInterval(cls.wrap(() => runNotesWithLabel('daily')), 24 * 3600 * 1000);
|
|
|
|
}
|
2020-06-21 03:42:41 +08:00
|
|
|
});
|