backend scripts should not be async because of transaction management

This commit is contained in:
zadam
2021-05-15 22:57:23 +02:00
parent a65eda5e4a
commit 04f249e800
6 changed files with 52 additions and 47 deletions

View File

@@ -1,10 +1,9 @@
const ScriptContext = require('./script_context');
const repository = require('./repository');
const cls = require('./cls');
const log = require('./log');
const becca = require("./becca/becca");
async function executeNote(note, apiParams) {
function executeNote(note, apiParams) {
if (!note.isJavaScript() || note.getScriptEnv() !== 'backend' || !note.isContentAvailable) {
log.info(`Cannot execute note ${note.noteId} "${note.title}", note must be of type "Code: JS frontend"`);
@@ -16,16 +15,16 @@ async function executeNote(note, apiParams) {
return executeBundle(bundle, apiParams);
}
async function executeNoteNoException(note, apiParams) {
function executeNoteNoException(note, apiParams) {
try {
await executeNote(note, apiParams);
executeNote(note, apiParams);
}
catch (e) {
// just swallow, exception is logged already in executeNote
}
}
async function executeBundle(bundle, apiParams = {}) {
function executeBundle(bundle, apiParams = {}) {
if (!apiParams.startNote) {
// this is the default case, the only exception is when we want to preserve frontend startNote
apiParams.startNote = bundle.note;
@@ -34,12 +33,12 @@ async function executeBundle(bundle, apiParams = {}) {
cls.set('sourceId', 'script');
// last \r\n is necessary if script contains line comment on its last line
const script = "async function() {\r\n" + bundle.script + "\r\n}";
const script = "function() {\r\n" + bundle.script + "\r\n}";
const ctx = new ScriptContext(bundle.allNotes, apiParams);
try {
return await execute(ctx, script);
return execute(ctx, script);
}
catch (e) {
log.error(`Execution of script "${bundle.note.title}" (${bundle.note.noteId}) failed with error: ${e.message}`);
@@ -49,10 +48,13 @@ async function executeBundle(bundle, apiParams = {}) {
}
/**
* THIS METHOD CANT BE ASYNC, OTHERWISE TRANSACTION WRAPPER WON'T BE EFFECTIVE AND WE WILL BE LOSING THE
* ENTITY CHANGES IN CLS.
*
* This method preserves frontend startNode - that's why we start execution from currentNote and override
* bundle's startNote.
*/
async function executeScript(script, params, startNoteId, currentNoteId, originEntityName, originEntityId) {
function executeScript(script, params, startNoteId, currentNoteId, originEntityName, originEntityId) {
const startNote = becca.getNote(startNoteId);
const currentNote = becca.getNote(currentNoteId);
const originEntity = becca.getEntityFromName(originEntityName, originEntityId);
@@ -63,11 +65,11 @@ async function executeScript(script, params, startNoteId, currentNoteId, originE
const bundle = getScriptBundle(currentNote, true, null, [], backendOverrideContent);
return await executeBundle(bundle, { startNote, originEntity });
return executeBundle(bundle, { startNote, originEntity });
}
async function execute(ctx, script) {
return await (function() { return eval(`const apiContext = this;\r\n(${script}\r\n)()`); }.call(ctx));
function execute(ctx, script) {
return function() { return eval(`const apiContext = this;\r\n(${script}\r\n)()`); }.call(ctx);
}
function getParams(params) {
@@ -153,10 +155,13 @@ function getScriptBundle(note, root = true, scriptEnv = null, includedNoteIds =
const moduleNoteIds = modules.map(mod => mod.noteId);
// only frontend scripts are async. Backend cannot be async because of transaction management.
const isFrontend = scriptEnv === 'frontend';
if (note.isJavaScript()) {
bundle.script += `
apiContext.modules['${note.noteId}'] = {};
${root ? 'return ' : ''}await ((async function(exports, module, require, api` + (modules.length > 0 ? ', ' : '') +
${root ? 'return ' : ''}${isFrontend ? 'await' : ''} ((${isFrontend ? 'async' : ''} function(exports, module, require, api` + (modules.length > 0 ? ', ' : '') +
modules.map(child => sanitizeVariableName(child.title)).join(', ') + `) {
try {
${backendOverrideContent || note.getContent()};