n8n
Version:
n8n Workflow Automation Tool
170 lines • 8.27 kB
JavaScript
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.JobProcessor = void 0;
const typedi_1 = require("typedi");
const n8n_workflow_1 = require("n8n-workflow");
const n8n_core_1 = require("n8n-core");
const Logger_1 = require("../Logger");
const config_1 = __importDefault(require("../config"));
const execution_repository_1 = require("../databases/repositories/execution.repository");
const workflow_repository_1 = require("../databases/repositories/workflow.repository");
const WorkflowExecuteAdditionalData = __importStar(require("../WorkflowExecuteAdditionalData"));
const NodeTypes_1 = require("../NodeTypes");
let JobProcessor = class JobProcessor {
constructor(logger, executionRepository, workflowRepository, nodeTypes) {
this.logger = logger;
this.executionRepository = executionRepository;
this.workflowRepository = workflowRepository;
this.nodeTypes = nodeTypes;
this.runningJobs = {};
}
async processJob(job) {
var _a, _b, _c;
const { executionId, loadStaticData } = job.data;
const execution = await this.executionRepository.findSingleExecution(executionId, {
includeData: true,
unflattenData: true,
});
if (!execution) {
this.logger.error('[JobProcessor] Failed to find execution data', { executionId });
throw new n8n_workflow_1.ApplicationError('Failed to find execution data. Aborting execution.', {
extra: { executionId },
});
}
const workflowId = execution.workflowData.id;
this.logger.info(`[JobProcessor] Starting job ${job.id} (execution ${executionId})`);
await this.executionRepository.updateStatus(executionId, 'running');
let { staticData } = execution.workflowData;
if (loadStaticData) {
const workflowData = await this.workflowRepository.findOne({
select: ['id', 'staticData'],
where: { id: workflowId },
});
if (workflowData === null) {
this.logger.error('[JobProcessor] Failed to find workflow', { workflowId, executionId });
throw new n8n_workflow_1.ApplicationError('Failed to find workflow', { extra: { workflowId } });
}
staticData = workflowData.staticData;
}
const workflowSettings = (_a = execution.workflowData.settings) !== null && _a !== void 0 ? _a : {};
let workflowTimeout = (_b = workflowSettings.executionTimeout) !== null && _b !== void 0 ? _b : config_1.default.getEnv('executions.timeout');
let executionTimeoutTimestamp;
if (workflowTimeout > 0) {
workflowTimeout = Math.min(workflowTimeout, config_1.default.getEnv('executions.maxTimeout'));
executionTimeoutTimestamp = Date.now() + workflowTimeout * 1000;
}
const workflow = new n8n_workflow_1.Workflow({
id: workflowId,
name: execution.workflowData.name,
nodes: execution.workflowData.nodes,
connections: execution.workflowData.connections,
active: execution.workflowData.active,
nodeTypes: this.nodeTypes,
staticData,
settings: execution.workflowData.settings,
});
const additionalData = await WorkflowExecuteAdditionalData.getBase(undefined, undefined, executionTimeoutTimestamp);
additionalData.hooks = WorkflowExecuteAdditionalData.getWorkflowHooksWorkerExecuter(execution.mode, job.data.executionId, execution.workflowData, { retryOf: execution.retryOf });
additionalData.hooks.hookFunctions.sendResponse = [
async (response) => {
await job.progress({
kind: 'respond-to-webhook',
executionId,
response: this.encodeWebhookResponse(response),
});
},
];
additionalData.executionId = executionId;
additionalData.setExecutionStatus = (status) => {
this.logger.debug(`[JobProcessor] Queued worker execution status for ${executionId} is "${status}"`);
};
let workflowExecute;
let workflowRun;
if (execution.data !== undefined) {
workflowExecute = new n8n_core_1.WorkflowExecute(additionalData, execution.mode, execution.data);
workflowRun = workflowExecute.processRunExecutionData(workflow);
}
else {
workflowExecute = new n8n_core_1.WorkflowExecute(additionalData, execution.mode);
workflowRun = workflowExecute.run(workflow);
}
const runningJob = {
run: workflowRun,
executionId,
workflowId: execution.workflowId,
workflowName: execution.workflowData.name,
mode: execution.mode,
startedAt: execution.startedAt,
retryOf: (_c = execution.retryOf) !== null && _c !== void 0 ? _c : '',
status: execution.status,
};
this.runningJobs[job.id] = runningJob;
await workflowRun;
delete this.runningJobs[job.id];
this.logger.debug('[JobProcessor] Job finished running', { jobId: job.id, executionId });
return { success: true };
}
stopJob(jobId) {
var _a;
(_a = this.runningJobs[jobId]) === null || _a === void 0 ? void 0 : _a.run.cancel();
delete this.runningJobs[jobId];
}
getRunningJobIds() {
return Object.keys(this.runningJobs);
}
getRunningJobsSummary() {
return Object.values(this.runningJobs).map(({ run, ...summary }) => summary);
}
encodeWebhookResponse(response) {
if (typeof response === 'object' && Buffer.isBuffer(response.body)) {
response.body = {
'__@N8nEncodedBuffer@__': response.body.toString(n8n_workflow_1.BINARY_ENCODING),
};
}
return response;
}
};
exports.JobProcessor = JobProcessor;
exports.JobProcessor = JobProcessor = __decorate([
(0, typedi_1.Service)(),
__metadata("design:paramtypes", [Logger_1.Logger,
execution_repository_1.ExecutionRepository,
workflow_repository_1.WorkflowRepository,
NodeTypes_1.NodeTypes])
], JobProcessor);
//# sourceMappingURL=job-processor.js.map
;