n8n
Version:
n8n Workflow Automation Tool
376 lines • 19.6 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.WorkflowRunner = void 0;
const typedi_1 = require("typedi");
const n8n_core_1 = require("n8n-core");
const n8n_workflow_1 = require("n8n-workflow");
const p_cancelable_1 = __importDefault(require("p-cancelable"));
const ActiveExecutions_1 = require("./ActiveExecutions");
const config_1 = __importDefault(require("./config"));
const execution_repository_1 = require("./databases/repositories/execution.repository");
const ExternalHooks_1 = require("./ExternalHooks");
const NodeTypes_1 = require("./NodeTypes");
const WorkflowHelpers = __importStar(require("./WorkflowHelpers"));
const WorkflowExecuteAdditionalData = __importStar(require("./WorkflowExecuteAdditionalData"));
const WorkflowHelpers_1 = require("./WorkflowHelpers");
const PermissionChecker_1 = require("./UserManagement/PermissionChecker");
const Logger_1 = require("./Logger");
const workflowStaticData_service_1 = require("./workflows/workflowStaticData.service");
const event_service_1 = require("./events/event.service");
const config_2 = require("@n8n/config");
let WorkflowRunner = class WorkflowRunner {
constructor(logger, activeExecutions, executionRepository, externalHooks, workflowStaticDataService, nodeTypes, permissionChecker, eventService) {
this.logger = logger;
this.activeExecutions = activeExecutions;
this.executionRepository = executionRepository;
this.externalHooks = externalHooks;
this.workflowStaticDataService = workflowStaticDataService;
this.nodeTypes = nodeTypes;
this.permissionChecker = permissionChecker;
this.eventService = eventService;
this.executionsMode = config_1.default.getEnv('executions.mode');
}
async processError(error, startedAt, executionMode, executionId, hooks) {
n8n_workflow_1.ErrorReporterProxy.error(error);
const isQueueMode = config_1.default.getEnv('executions.mode') === 'queue';
if (isQueueMode && executionMode !== 'manual') {
const executionWithoutData = await this.executionRepository.findSingleExecution(executionId, {
includeData: false,
});
if ((executionWithoutData === null || executionWithoutData === void 0 ? void 0 : executionWithoutData.finished) === true && (executionWithoutData === null || executionWithoutData === void 0 ? void 0 : executionWithoutData.status) === 'success') {
return;
}
}
const fullRunData = {
data: {
resultData: {
error: {
...error,
message: error.message,
stack: error.stack,
},
runData: {},
},
},
finished: false,
mode: executionMode,
startedAt,
stoppedAt: new Date(),
status: 'error',
};
this.activeExecutions.remove(executionId, fullRunData);
if (hooks) {
await hooks.executeHookFunctions('workflowExecuteAfter', [fullRunData]);
}
}
async run(data, loadStaticData, realtime, restartExecutionId, responsePromise) {
const executionId = await this.activeExecutions.add(data, restartExecutionId);
const { id: workflowId, nodes } = data.workflowData;
try {
await this.permissionChecker.check(workflowId, nodes);
}
catch (error) {
const runData = (0, WorkflowHelpers_1.generateFailedExecutionFromError)(data.executionMode, error, error.node);
const workflowHooks = WorkflowExecuteAdditionalData.getWorkflowHooksMain(data, executionId);
await workflowHooks.executeHookFunctions('workflowExecuteBefore', []);
await workflowHooks.executeHookFunctions('workflowExecuteAfter', [runData]);
responsePromise === null || responsePromise === void 0 ? void 0 : responsePromise.reject(error);
this.activeExecutions.remove(executionId);
return executionId;
}
if (responsePromise) {
this.activeExecutions.attachResponsePromise(executionId, responsePromise);
}
if (this.executionsMode === 'queue' && data.executionMode !== 'manual') {
await this.enqueueExecution(executionId, data, loadStaticData, realtime);
}
else {
await this.runMainProcess(executionId, data, loadStaticData, restartExecutionId);
this.eventService.emit('workflow-pre-execute', { executionId, data });
}
if (this.executionsMode !== 'queue' ||
config_1.default.getEnv('generic.instanceType') === 'worker' ||
data.executionMode === 'manual') {
const postExecutePromise = this.activeExecutions.getPostExecutePromise(executionId);
postExecutePromise
.then(async (executionData) => {
this.eventService.emit('workflow-post-execute', {
workflow: data.workflowData,
executionId,
userId: data.userId,
runData: executionData,
});
if (this.externalHooks.exists('workflow.postExecute')) {
try {
await this.externalHooks.run('workflow.postExecute', [
executionData,
data.workflowData,
executionId,
]);
}
catch (error) {
n8n_workflow_1.ErrorReporterProxy.error(error);
this.logger.error('There was a problem running hook "workflow.postExecute"', error);
}
}
})
.catch((error) => {
if (error instanceof n8n_workflow_1.ExecutionCancelledError)
return;
n8n_workflow_1.ErrorReporterProxy.error(error);
this.logger.error('There was a problem running internal hook "onWorkflowPostExecute"', error);
});
}
return executionId;
}
async runMainProcess(executionId, data, loadStaticData, restartExecutionId) {
var _a, _b, _c;
const workflowId = data.workflowData.id;
if (loadStaticData === true && workflowId) {
data.workflowData.staticData =
await this.workflowStaticDataService.getStaticDataById(workflowId);
}
let executionTimeout;
const workflowSettings = (_a = data.workflowData.settings) !== null && _a !== void 0 ? _a : {};
let workflowTimeout = (_b = workflowSettings.executionTimeout) !== null && _b !== void 0 ? _b : config_1.default.getEnv('executions.timeout');
if (workflowTimeout > 0) {
workflowTimeout = Math.min(workflowTimeout, config_1.default.getEnv('executions.maxTimeout'));
}
let pinData;
if (data.executionMode === 'manual') {
pinData = (_c = data.pinData) !== null && _c !== void 0 ? _c : data.workflowData.pinData;
}
const workflow = new n8n_workflow_1.Workflow({
id: workflowId,
name: data.workflowData.name,
nodes: data.workflowData.nodes,
connections: data.workflowData.connections,
active: data.workflowData.active,
nodeTypes: this.nodeTypes,
staticData: data.workflowData.staticData,
settings: workflowSettings,
pinData,
});
const additionalData = await WorkflowExecuteAdditionalData.getBase(data.userId, undefined, workflowTimeout <= 0 ? undefined : Date.now() + workflowTimeout * 1000);
additionalData.restartExecutionId = restartExecutionId;
additionalData.executionId = executionId;
this.logger.verbose(`Execution for workflow ${data.workflowData.name} was assigned id ${executionId}`, { executionId });
let workflowExecution;
await this.executionRepository.updateStatus(executionId, 'running');
try {
additionalData.hooks = WorkflowExecuteAdditionalData.getWorkflowHooksMain(data, executionId);
additionalData.hooks.hookFunctions.sendResponse = [
async (response) => {
this.activeExecutions.resolveResponsePromise(executionId, response);
},
];
additionalData.setExecutionStatus = WorkflowExecuteAdditionalData.setExecutionStatus.bind({
executionId,
});
additionalData.sendDataToUI = WorkflowExecuteAdditionalData.sendDataToUI.bind({
pushRef: data.pushRef,
});
if (data.executionData !== undefined) {
this.logger.debug(`Execution ID ${executionId} had Execution data. Running with payload.`, {
executionId,
});
const workflowExecute = new n8n_core_1.WorkflowExecute(additionalData, data.executionMode, data.executionData);
workflowExecution = workflowExecute.processRunExecutionData(workflow);
}
else if (data.runData === undefined ||
data.startNodes === undefined ||
data.startNodes.length === 0) {
this.logger.debug(`Execution ID ${executionId} will run executing all nodes.`, {
executionId,
});
const startNode = WorkflowHelpers.getExecutionStartNode(data, workflow);
const workflowExecute = new n8n_core_1.WorkflowExecute(additionalData, data.executionMode);
workflowExecution = workflowExecute.run(workflow, startNode, data.destinationNode, data.pinData);
}
else {
this.logger.debug(`Execution ID ${executionId} is a partial execution.`, { executionId });
const workflowExecute = new n8n_core_1.WorkflowExecute(additionalData, data.executionMode);
workflowExecution = workflowExecute.runPartialWorkflow(workflow, data.runData, data.startNodes, data.destinationNode, data.pinData);
}
this.activeExecutions.attachWorkflowExecution(executionId, workflowExecution);
if (workflowTimeout > 0) {
const timeout = Math.min(workflowTimeout, config_1.default.getEnv('executions.maxTimeout')) * 1000;
executionTimeout = setTimeout(() => {
void this.activeExecutions.stopExecution(executionId);
}, timeout);
}
workflowExecution
.then((fullRunData) => {
clearTimeout(executionTimeout);
if (workflowExecution.isCanceled) {
fullRunData.finished = false;
}
fullRunData.status = this.activeExecutions.getStatus(executionId);
this.activeExecutions.remove(executionId, fullRunData);
})
.catch(async (error) => await this.processError(error, new Date(), data.executionMode, executionId, additionalData.hooks));
}
catch (error) {
await this.processError(error, new Date(), data.executionMode, executionId, additionalData.hooks);
throw error;
}
}
async enqueueExecution(executionId, data, loadStaticData, realtime) {
const jobData = {
executionId,
loadStaticData: !!loadStaticData,
};
if (!this.scalingService) {
const { ScalingService } = await Promise.resolve().then(() => __importStar(require('./scaling/scaling.service')));
this.scalingService = typedi_1.Container.get(ScalingService);
}
let priority = 100;
if (realtime === true) {
priority = 50;
}
const jobOptions = {
priority,
removeOnComplete: true,
removeOnFail: true,
};
let job;
let hooks;
try {
job = await this.scalingService.addJob(jobData, jobOptions);
hooks = WorkflowExecuteAdditionalData.getWorkflowHooksWorkerMain(data.executionMode, executionId, data.workflowData, { retryOf: data.retryOf ? data.retryOf.toString() : undefined });
await hooks.executeHookFunctions('workflowExecuteBefore', []);
}
catch (error) {
const hooks = WorkflowExecuteAdditionalData.getWorkflowHooksWorkerExecuter(data.executionMode, executionId, data.workflowData, { retryOf: data.retryOf ? data.retryOf.toString() : undefined });
await this.processError(error, new Date(), data.executionMode, executionId, hooks);
throw error;
}
const workflowExecution = new p_cancelable_1.default(async (resolve, reject, onCancel) => {
onCancel.shouldReject = false;
onCancel(async () => {
await this.scalingService.stopJob(job);
const hooksWorker = WorkflowExecuteAdditionalData.getWorkflowHooksWorkerExecuter(data.executionMode, executionId, data.workflowData, { retryOf: data.retryOf ? data.retryOf.toString() : undefined });
const error = new n8n_workflow_1.ExecutionCancelledError(executionId);
await this.processError(error, new Date(), data.executionMode, executionId, hooksWorker);
reject(error);
});
const jobData = job.finished();
const { queueRecoveryInterval } = typedi_1.Container.get(config_2.GlobalConfig).queue.bull;
const racingPromises = [jobData];
let clearWatchdogInterval;
if (queueRecoveryInterval > 0) {
let watchDogInterval;
const watchDog = new Promise((res) => {
watchDogInterval = setInterval(async () => {
const currentJob = await this.scalingService.getJob(job.id);
if (currentJob === null) {
res({ success: true });
}
}, queueRecoveryInterval * 1000);
});
racingPromises.push(watchDog);
clearWatchdogInterval = () => {
if (watchDogInterval) {
clearInterval(watchDogInterval);
watchDogInterval = undefined;
}
};
}
try {
await Promise.race(racingPromises);
if (clearWatchdogInterval !== undefined) {
clearWatchdogInterval();
}
}
catch (error) {
n8n_workflow_1.ErrorReporterProxy.error(error);
const hooks = WorkflowExecuteAdditionalData.getWorkflowHooksWorkerExecuter(data.executionMode, executionId, data.workflowData, { retryOf: data.retryOf ? data.retryOf.toString() : undefined });
this.logger.error(`Problem with execution ${executionId}: ${error.message}. Aborting.`);
if (clearWatchdogInterval !== undefined) {
clearWatchdogInterval();
}
await this.processError(error, new Date(), data.executionMode, executionId, hooks);
reject(error);
}
const executionHasPostExecutionPromises = this.activeExecutions.getPostExecutePromiseCount(executionId) > 0;
if (executionHasPostExecutionPromises) {
this.logger.debug(`Reading execution data for execution ${executionId} from db for PostExecutionPromise.`);
}
else {
this.logger.debug(`Skipping execution data for execution ${executionId} since there are no PostExecutionPromise.`);
}
const fullExecutionData = await this.executionRepository.findSingleExecution(executionId, {
includeData: executionHasPostExecutionPromises,
unflattenData: executionHasPostExecutionPromises,
});
if (!fullExecutionData) {
return reject(new Error(`Could not find execution with id "${executionId}"`));
}
const runData = {
data: {},
finished: fullExecutionData.finished,
mode: fullExecutionData.mode,
startedAt: fullExecutionData.startedAt,
stoppedAt: fullExecutionData.stoppedAt,
status: fullExecutionData.status,
};
if (executionHasPostExecutionPromises) {
runData.data = fullExecutionData.data;
}
this.activeExecutions.remove(executionId, runData);
await hooks.executeHookFunctions('workflowExecuteAfter', [runData]);
resolve(runData);
});
workflowExecution.catch(() => {
});
this.activeExecutions.attachWorkflowExecution(executionId, workflowExecution);
}
};
exports.WorkflowRunner = WorkflowRunner;
exports.WorkflowRunner = WorkflowRunner = __decorate([
(0, typedi_1.Service)(),
__metadata("design:paramtypes", [Logger_1.Logger,
ActiveExecutions_1.ActiveExecutions,
execution_repository_1.ExecutionRepository,
ExternalHooks_1.ExternalHooks,
workflowStaticData_service_1.WorkflowStaticDataService,
NodeTypes_1.NodeTypes,
PermissionChecker_1.PermissionChecker,
event_service_1.EventService])
], WorkflowRunner);
//# sourceMappingURL=WorkflowRunner.js.map