refactor(core): Move execution progress saving to standalone utility (no-changelog) (#7770)

This PR continues the effort of moving logic inside execution lifecycle
hooks into standalone testable functions, as a stepping stone to
refactoring the hooks themselves.
This commit is contained in:
Iván Ovejero
2023-11-27 13:10:43 +01:00
committed by GitHub
parent 2807ddcd0d
commit 7b8532d3a3
3 changed files with 220 additions and 83 deletions

View File

@@ -65,6 +65,7 @@ import {
import { restoreBinaryDataId } from './executionLifecycleHooks/restoreBinaryDataId';
import { toSaveSettings } from './executionLifecycleHooks/toSaveSettings';
import { Logger } from './Logger';
import { saveExecutionProgress } from './executionLifecycleHooks/saveExecutionProgress';
const ERROR_TRIGGER_TYPE = config.getEnv('nodes.errorTriggerType');
@@ -358,89 +359,14 @@ export function hookFunctionsPreExecute(parentProcessMode?: string): IWorkflowEx
data: ITaskData,
executionData: IRunExecutionData,
): Promise<void> {
const saveSettings = toSaveSettings(this.workflowData.settings);
if (!saveSettings.progress) return;
try {
logger.debug(
`Save execution progress to database for execution ID ${this.executionId} `,
{ executionId: this.executionId, nodeName },
);
const fullExecutionData = await Container.get(ExecutionRepository).findSingleExecution(
this.executionId,
{
includeData: true,
unflattenData: true,
},
);
if (!fullExecutionData) {
// Something went badly wrong if this happens.
// This check is here mostly to make typescript happy.
return;
}
if (fullExecutionData.finished) {
// We already received ´workflowExecuteAfter´ webhook, so this is just an async call
// that was left behind. We skip saving because the other call should have saved everything
// so this one is safe to ignore
return;
}
if (fullExecutionData.data === undefined) {
fullExecutionData.data = {
startData: {},
resultData: {
runData: {},
},
executionData: {
contextData: {},
metadata: {},
nodeExecutionStack: [],
waitingExecution: {},
waitingExecutionSource: {},
},
};
}
if (Array.isArray(fullExecutionData.data.resultData.runData[nodeName])) {
// Append data if array exists
fullExecutionData.data.resultData.runData[nodeName].push(data);
} else {
// Initialize array and save data
fullExecutionData.data.resultData.runData[nodeName] = [data];
}
fullExecutionData.data.executionData = executionData.executionData;
// Set last executed node so that it may resume on failure
fullExecutionData.data.resultData.lastNodeExecuted = nodeName;
fullExecutionData.status = 'running';
await Container.get(ExecutionRepository).updateExistingExecution(
this.executionId,
fullExecutionData,
);
} catch (err) {
ErrorReporter.error(err);
// TODO: Improve in the future!
// Errors here might happen because of database access
// For busy machines, we may get "Database is locked" errors.
// We do this to prevent crashes and executions ending in `unknown` state.
logger.error(
`Failed saving execution progress to database for execution ID ${this.executionId} (hookFunctionsPreExecute, nodeExecuteAfter)`,
{
...err,
executionId: this.executionId,
sessionId: this.sessionId,
workflowId: this.workflowData.id,
},
);
}
await saveExecutionProgress(
this.workflowData,
this.executionId,
nodeName,
data,
executionData,
this.sessionId,
);
},
],
};