UNPKG

n8n

Version:

n8n Workflow Automation Tool

184 lines 8.08 kB
"use strict"; 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 __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.ActiveExecutions = void 0; const typedi_1 = require("typedi"); const n8n_workflow_1 = require("n8n-workflow"); const node_assert_1 = require("node:assert"); const utils_1 = require("./utils"); const execution_repository_1 = require("./databases/repositories/execution.repository"); const Logger_1 = require("./Logger"); const concurrency_control_service_1 = require("./concurrency/concurrency-control.service"); const config_1 = __importDefault(require("./config")); let ActiveExecutions = class ActiveExecutions { constructor(logger, executionRepository, concurrencyControl) { this.logger = logger; this.executionRepository = executionRepository; this.concurrencyControl = concurrencyControl; this.activeExecutions = {}; } has(executionId) { return this.activeExecutions[executionId] !== undefined; } async add(executionData, executionId) { let executionStatus = executionId ? 'running' : 'new'; const mode = executionData.executionMode; if (executionId === undefined) { const fullExecutionData = { data: executionData.executionData, mode, finished: false, startedAt: new Date(), workflowData: executionData.workflowData, status: executionStatus, workflowId: executionData.workflowData.id, }; if (executionData.retryOf !== undefined) { fullExecutionData.retryOf = executionData.retryOf.toString(); } const workflowId = executionData.workflowData.id; if (workflowId !== undefined && (0, utils_1.isWorkflowIdValid)(workflowId)) { fullExecutionData.workflowId = workflowId; } executionId = await this.executionRepository.createNewExecution(fullExecutionData); (0, node_assert_1.strict)(executionId); await this.concurrencyControl.throttle({ mode, executionId }); executionStatus = 'running'; } else { await this.concurrencyControl.throttle({ mode, executionId }); const execution = { id: executionId, data: executionData.executionData, waitTill: null, status: executionStatus, }; await this.executionRepository.updateExistingExecution(executionId, execution); } this.activeExecutions[executionId] = { executionData, startedAt: new Date(), postExecutePromises: [], status: executionStatus, }; return executionId; } attachWorkflowExecution(executionId, workflowExecution) { this.getExecution(executionId).workflowExecution = workflowExecution; } attachResponsePromise(executionId, responsePromise) { this.getExecution(executionId).responsePromise = responsePromise; } resolveResponsePromise(executionId, response) { var _a; const execution = this.activeExecutions[executionId]; (_a = execution === null || execution === void 0 ? void 0 : execution.responsePromise) === null || _a === void 0 ? void 0 : _a.resolve(response); } getPostExecutePromiseCount(executionId) { var _a, _b; return (_b = (_a = this.activeExecutions[executionId]) === null || _a === void 0 ? void 0 : _a.postExecutePromises.length) !== null && _b !== void 0 ? _b : 0; } remove(executionId, fullRunData) { const execution = this.activeExecutions[executionId]; if (execution === undefined) { return; } for (const promise of execution.postExecutePromises) { promise.resolve(fullRunData); } this.postExecuteCleanup(executionId); } stopExecution(executionId) { const execution = this.activeExecutions[executionId]; if (execution === undefined) { return; } execution.workflowExecution.cancel(); const reason = new n8n_workflow_1.ExecutionCancelledError(executionId); for (const promise of execution.postExecutePromises) { promise.reject(reason); } this.postExecuteCleanup(executionId); } postExecuteCleanup(executionId) { const execution = this.activeExecutions[executionId]; if (execution === undefined) { return; } delete this.activeExecutions[executionId]; this.concurrencyControl.release({ mode: execution.executionData.executionMode }); } async getPostExecutePromise(executionId) { const waitPromise = await (0, n8n_workflow_1.createDeferredPromise)(); this.getExecution(executionId).postExecutePromises.push(waitPromise); return await waitPromise.promise(); } getActiveExecutions() { const returnData = []; let data; for (const id of Object.keys(this.activeExecutions)) { data = this.activeExecutions[id]; returnData.push({ id, retryOf: data.executionData.retryOf, startedAt: data.startedAt, mode: data.executionData.executionMode, workflowId: data.executionData.workflowData.id, status: data.status, }); } return returnData; } setStatus(executionId, status) { this.getExecution(executionId).status = status; } getStatus(executionId) { return this.getExecution(executionId).status; } async shutdown(cancelAll = false) { let executionIds = Object.keys(this.activeExecutions); if (config_1.default.getEnv('executions.mode') === 'regular') { this.concurrencyControl.disable(); } if (cancelAll) { if (config_1.default.getEnv('executions.mode') === 'regular') { await this.concurrencyControl.removeAll(this.activeExecutions); } executionIds.forEach((executionId) => this.stopExecution(executionId)); } let count = 0; while (executionIds.length !== 0) { if (count++ % 4 === 0) { this.logger.info(`Waiting for ${executionIds.length} active executions to finish...`); } await (0, n8n_workflow_1.sleep)(500); executionIds = Object.keys(this.activeExecutions); } } getExecution(executionId) { const execution = this.activeExecutions[executionId]; if (!execution) { throw new n8n_workflow_1.ApplicationError('No active execution found', { extra: { executionId } }); } return execution; } }; exports.ActiveExecutions = ActiveExecutions; exports.ActiveExecutions = ActiveExecutions = __decorate([ (0, typedi_1.Service)(), __metadata("design:paramtypes", [Logger_1.Logger, execution_repository_1.ExecutionRepository, concurrency_control_service_1.ConcurrencyControlService]) ], ActiveExecutions); //# sourceMappingURL=ActiveExecutions.js.map