Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
basic scheduling of backend scripts using attributes
azivner982b72364742
diff --git a/src/app.js b/src/app.jsindex e36391bfb1..38936ff5eb 100644--- a/src/app.js+++ b/src/app.js@@ -73,6 +73,8 @@ require('./services/backup');// trigger consistency checks timerrequire('./services/consistency_checks');+require('./services/scheduler');+module.exports = {app,sessionParserdiff --git a/src/services/attributes.js b/src/services/attributes.jsindex c597c766ee..8ba17cd81d 100644--- a/src/services/attributes.js+++ b/src/services/attributes.js@@ -10,7 +10,8 @@ const BUILTIN_ATTRIBUTES = ['disable_versioning','calendar_root','hide_in_autocomplete',- 'exclude_from_export'+ 'exclude_from_export',+ 'run'];async function getNoteAttributeMap(noteId) {diff --git a/src/services/scheduler.js b/src/services/scheduler.jsnew file mode 100644index 0000000000..8d296e833b--- /dev/null+++ b/src/services/scheduler.js@@ -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);\ No newline at end of filediff --git a/src/services/script.js b/src/services/script.jsindex 7021393d11..0de7c47c07 100644--- a/src/services/script.js+++ b/src/services/script.js@@ -1,6 +1,18 @@const sql = require('./sql');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) {const ctx = new ScriptContext(dataKey);const paramsStr = getParams(params);@@ -73,6 +85,7 @@ function getParams(params) {}module.exports = {+ executeNote,executeScript,setJob};\ No newline at end of file