trilium/src/services/scheduler.js

30 lines
1 KiB
JavaScript
Raw Normal View History

const scriptService = require('./script');
2018-03-31 21:07:58 +08:00
const repository = require('./repository');
const cls = require('./cls');
2018-04-06 11:17:19 +08:00
const sqlInit = require('./sql_init');
async function runNotesWithLabel(runAttrValue) {
2018-03-31 21:07:58 +08:00
const notes = await repository.getEntities(`
SELECT notes.*
FROM notes
2018-08-10 02:55:16 +08:00
JOIN attributes ON attributes.noteId = notes.noteId
AND attributes.isDeleted = 0
AND attributes.type = 'label'
AND attributes.name = 'run'
AND attributes.value = ?
WHERE
notes.type = 'code'
AND notes.isDeleted = 0`, [runAttrValue]);
for (const note of notes) {
scriptService.executeNote(note, { originEntity: note });
}
}
2018-04-06 11:17:19 +08:00
sqlInit.dbReady.then(() => {
setTimeout(cls.wrap(() => runNotesWithLabel('backendStartup')), 10 * 1000);
2018-04-06 11:17:19 +08:00
setInterval(cls.wrap(() => runNotesWithLabel('hourly')), 3600 * 1000);
2018-04-06 11:17:19 +08:00
setInterval(cls.wrap(() => runNotesWithLabel('daily')), 24 * 3600 * 1000);
});