mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
basic scheduling of backend scripts using attributes
This commit is contained in:
parent
31d5ac05ff
commit
982b723647
@ -73,6 +73,8 @@ require('./services/backup');
|
|||||||
// trigger consistency checks timer
|
// trigger consistency checks timer
|
||||||
require('./services/consistency_checks');
|
require('./services/consistency_checks');
|
||||||
|
|
||||||
|
require('./services/scheduler');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
app,
|
app,
|
||||||
sessionParser
|
sessionParser
|
||||||
|
@ -10,7 +10,8 @@ const BUILTIN_ATTRIBUTES = [
|
|||||||
'disable_versioning',
|
'disable_versioning',
|
||||||
'calendar_root',
|
'calendar_root',
|
||||||
'hide_in_autocomplete',
|
'hide_in_autocomplete',
|
||||||
'exclude_from_export'
|
'exclude_from_export',
|
||||||
|
'run'
|
||||||
];
|
];
|
||||||
|
|
||||||
async function getNoteAttributeMap(noteId) {
|
async function getNoteAttributeMap(noteId) {
|
||||||
|
26
src/services/scheduler.js
Normal file
26
src/services/scheduler.js
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
const script = require('./script');
|
||||||
|
const Repository = require('./repository');
|
||||||
|
|
||||||
|
const repo = new Repository();
|
||||||
|
|
||||||
|
async function runNotesWithAttribute(runAttrValue) {
|
||||||
|
const notes = await repo.getEntities(`
|
||||||
|
SELECT notes.*
|
||||||
|
FROM notes
|
||||||
|
JOIN attributes ON attributes.noteId = notes.noteId
|
||||||
|
AND attributes.name = 'run'
|
||||||
|
AND attributes.value = ?
|
||||||
|
WHERE
|
||||||
|
notes.type = 'code'
|
||||||
|
AND notes.isDeleted = 0`, [runAttrValue]);
|
||||||
|
|
||||||
|
for (const note of notes) {
|
||||||
|
script.executeNote(note);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setTimeout(() => runNotesWithAttribute('on_startup'), 10 * 1000);
|
||||||
|
|
||||||
|
setInterval(() => runNotesWithAttribute('hourly'), 3600 * 1000);
|
||||||
|
|
||||||
|
setInterval(() => runNotesWithAttribute('daily'), 24 * 3600 * 1000);
|
@ -1,6 +1,18 @@
|
|||||||
const sql = require('./sql');
|
const sql = require('./sql');
|
||||||
const ScriptContext = require('./script_context');
|
const ScriptContext = require('./script_context');
|
||||||
|
|
||||||
|
async function executeNote(note) {
|
||||||
|
if (note.isProtected || !note.isJavaScript()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const ctx = new ScriptContext();
|
||||||
|
|
||||||
|
return await sql.doInTransaction(async () => {
|
||||||
|
return await (function() { return eval(`const api = this; (async function() {${note.content}\n\r})()`); }.call(ctx));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
async function executeScript(dataKey, script, params) {
|
async function executeScript(dataKey, script, params) {
|
||||||
const ctx = new ScriptContext(dataKey);
|
const ctx = new ScriptContext(dataKey);
|
||||||
const paramsStr = getParams(params);
|
const paramsStr = getParams(params);
|
||||||
@ -73,6 +85,7 @@ function getParams(params) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
executeNote,
|
||||||
executeScript,
|
executeScript,
|
||||||
setJob
|
setJob
|
||||||
};
|
};
|
Loading…
x
Reference in New Issue
Block a user