manual transaction handling for jobs

This commit is contained in:
azivner 2018-02-24 22:44:45 -05:00
parent f0bea9cf71
commit 12c06ae97e
4 changed files with 37 additions and 18 deletions

View File

@ -716,6 +716,9 @@ const noteTree = (function() {
titlePath = '';
}
// https://github.com/zadam/trilium/issues/46
// unfortunately not easy to implement because we don't have an easy access to note's isProtected property
const autocompleteItems = [];
for (const childNoteId of parentToChildren[parentNoteId]) {

View File

@ -1,21 +1,15 @@
const log = require('./log');
const sql = require('./sql');
const ScriptContext = require('./script_context');
async function executeScript(dataKey, script, params) {
log.info('Executing script: ' + script);
const ctx = new ScriptContext(dataKey);
const paramsStr = getParams(params);
let ret;
return await sql.doInTransaction(async () => execute(ctx, script, paramsStr));
}
await sql.doInTransaction(async () => {
ret = await (function() { return eval(`const api = this; (${script})(${paramsStr})`); }.call(ctx));
});
return ret;
async function execute(ctx, script, paramsStr) {
return await (function() { return eval(`const api = this; (${script})(${paramsStr})`); }.call(ctx));
}
const timeouts = {};
@ -35,15 +29,31 @@ function clearExistingJob(name) {
}
}
async function setJob(opts) {
clearExistingJob(opts.name);
async function executeJob(script, params, manualTransactionHandling) {
const ctx = new ScriptContext();
const paramsStr = getParams(params);
if (opts.runEveryMs && opts.runEveryMs > 0) {
intervals[opts.name] = setInterval(() => executeScript(null, opts.job, opts.params), opts.runEveryMs);
if (manualTransactionHandling) {
return await execute(ctx, script, paramsStr);
}
else {
return await sql.doInTransaction(async () => execute(ctx, script, paramsStr));
}
}
async function setJob(opts) {
const { name, runEveryMs, initialRunAfterMs } = opts;
clearExistingJob(name);
const jobFunc = () => executeJob(opts.job, opts.params, opts.manualTransactionHandling);
if (runEveryMs && runEveryMs > 0) {
intervals[name] = setInterval(jobFunc, runEveryMs);
}
if (opts.initialRunAfterMs && opts.initialRunAfterMs > 0) {
timeouts[opts.name] = setTimeout(() => executeScript(null, opts.job, opts.params), opts.initialRunAfterMs);
if (initialRunAfterMs && initialRunAfterMs > 0) {
timeouts[name] = setTimeout(jobFunc, initialRunAfterMs);
}
}

View File

@ -1,12 +1,13 @@
const log = require('./log');
const protected_session = require('./protected_session');
const notes = require('./notes');
const sql = require('./sql');
const attributes = require('./attributes');
const date_notes = require('./date_notes');
const config = require('./config');
const Repository = require('./repository');
function ScriptContext(noteId, dataKey) {
function ScriptContext(dataKey) {
dataKey = protected_session.getDataKey(dataKey);
const repository = new Repository(dataKey);
@ -65,6 +66,8 @@ function ScriptContext(noteId, dataKey) {
this.getRootCalendarNoteId = date_notes.getRootCalendarNoteId;
this.getDateNoteId = date_notes.getDateNoteId;
this.transaction = sql.doInTransaction;
}
module.exports = ScriptContext;

View File

@ -195,6 +195,7 @@ async function doInTransaction(func) {
await transactionPromise;
}
let ret = null;
const error = new Error(); // to capture correct stack trace in case of exception
transactionActive = true;
@ -202,7 +203,7 @@ async function doInTransaction(func) {
try {
await beginTransaction();
await func();
ret = await func();
await commit();
@ -223,6 +224,8 @@ async function doInTransaction(func) {
if (transactionActive) {
await transactionPromise;
}
return ret;
}
async function isDbUpToDate() {