mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 18:36:30 +01:00 
			
		
		
		
	manual transaction handling for jobs
This commit is contained in:
		| @@ -716,6 +716,9 @@ const noteTree = (function() { | |||||||
|             titlePath = ''; |             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 = []; |         const autocompleteItems = []; | ||||||
|  |  | ||||||
|         for (const childNoteId of parentToChildren[parentNoteId]) { |         for (const childNoteId of parentToChildren[parentNoteId]) { | ||||||
|   | |||||||
| @@ -1,21 +1,15 @@ | |||||||
| const log = require('./log'); |  | ||||||
| const sql = require('./sql'); | const sql = require('./sql'); | ||||||
| const ScriptContext = require('./script_context'); | const ScriptContext = require('./script_context'); | ||||||
|  |  | ||||||
| async function executeScript(dataKey, script, params) { | async function executeScript(dataKey, script, params) { | ||||||
|     log.info('Executing script: ' + script); |  | ||||||
|  |  | ||||||
|     const ctx = new ScriptContext(dataKey); |     const ctx = new ScriptContext(dataKey); | ||||||
|  |  | ||||||
|     const paramsStr = getParams(params); |     const paramsStr = getParams(params); | ||||||
|  |  | ||||||
|     let ret; |     return await sql.doInTransaction(async () => execute(ctx, script, paramsStr)); | ||||||
|  | } | ||||||
|  |  | ||||||
|     await sql.doInTransaction(async () => { | async function execute(ctx, script, paramsStr) { | ||||||
|         ret = await (function() { return eval(`const api = this; (${script})(${paramsStr})`); }.call(ctx)); |     return await (function() { return eval(`const api = this; (${script})(${paramsStr})`); }.call(ctx)); | ||||||
|     }); |  | ||||||
|  |  | ||||||
|     return ret; |  | ||||||
| } | } | ||||||
|  |  | ||||||
| const timeouts = {}; | const timeouts = {}; | ||||||
| @@ -35,15 +29,31 @@ function clearExistingJob(name) { | |||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| async function setJob(opts) { | async function executeJob(script, params, manualTransactionHandling) { | ||||||
|     clearExistingJob(opts.name); |     const ctx = new ScriptContext(); | ||||||
|  |     const paramsStr = getParams(params); | ||||||
|  |  | ||||||
|     if (opts.runEveryMs && opts.runEveryMs > 0) { |     if (manualTransactionHandling) { | ||||||
|         intervals[opts.name] = setInterval(() => executeScript(null, opts.job, opts.params), opts.runEveryMs); |         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) { |     if (initialRunAfterMs && initialRunAfterMs > 0) { | ||||||
|         timeouts[opts.name] = setTimeout(() => executeScript(null, opts.job, opts.params), opts.initialRunAfterMs); |         timeouts[name] = setTimeout(jobFunc, initialRunAfterMs); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,12 +1,13 @@ | |||||||
| const log = require('./log'); | const log = require('./log'); | ||||||
| const protected_session = require('./protected_session'); | const protected_session = require('./protected_session'); | ||||||
| const notes = require('./notes'); | const notes = require('./notes'); | ||||||
|  | const sql = require('./sql'); | ||||||
| const attributes = require('./attributes'); | const attributes = require('./attributes'); | ||||||
| const date_notes = require('./date_notes'); | const date_notes = require('./date_notes'); | ||||||
| const config = require('./config'); | const config = require('./config'); | ||||||
| const Repository = require('./repository'); | const Repository = require('./repository'); | ||||||
|  |  | ||||||
| function ScriptContext(noteId, dataKey) { | function ScriptContext(dataKey) { | ||||||
|     dataKey = protected_session.getDataKey(dataKey); |     dataKey = protected_session.getDataKey(dataKey); | ||||||
|     const repository = new Repository(dataKey); |     const repository = new Repository(dataKey); | ||||||
|  |  | ||||||
| @@ -65,6 +66,8 @@ function ScriptContext(noteId, dataKey) { | |||||||
|  |  | ||||||
|     this.getRootCalendarNoteId = date_notes.getRootCalendarNoteId; |     this.getRootCalendarNoteId = date_notes.getRootCalendarNoteId; | ||||||
|     this.getDateNoteId = date_notes.getDateNoteId; |     this.getDateNoteId = date_notes.getDateNoteId; | ||||||
|  |  | ||||||
|  |     this.transaction = sql.doInTransaction; | ||||||
| } | } | ||||||
|  |  | ||||||
| module.exports = ScriptContext; | module.exports = ScriptContext; | ||||||
| @@ -195,6 +195,7 @@ async function doInTransaction(func) { | |||||||
|         await transactionPromise; |         await transactionPromise; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     let ret = null; | ||||||
|     const error = new Error(); // to capture correct stack trace in case of exception |     const error = new Error(); // to capture correct stack trace in case of exception | ||||||
|  |  | ||||||
|     transactionActive = true; |     transactionActive = true; | ||||||
| @@ -202,7 +203,7 @@ async function doInTransaction(func) { | |||||||
|         try { |         try { | ||||||
|             await beginTransaction(); |             await beginTransaction(); | ||||||
|  |  | ||||||
|             await func(); |             ret = await func(); | ||||||
|  |  | ||||||
|             await commit(); |             await commit(); | ||||||
|  |  | ||||||
| @@ -223,6 +224,8 @@ async function doInTransaction(func) { | |||||||
|     if (transactionActive) { |     if (transactionActive) { | ||||||
|         await transactionPromise; |         await transactionPromise; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     return ret; | ||||||
| } | } | ||||||
|  |  | ||||||
| async function isDbUpToDate() { | async function isDbUpToDate() { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user