UNPKG

n8n

Version:

n8n Workflow Automation Tool

164 lines 6.63 kB
"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); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TaskRunnerProcess = void 0; const config_1 = require("@n8n/config"); const a = __importStar(require("node:assert/strict")); const node_child_process_1 = require("node:child_process"); const process = __importStar(require("node:process")); const typedi_1 = require("typedi"); const task_runner_auth_service_1 = require("./auth/task-runner-auth.service"); const on_shutdown_1 = require("../decorators/on-shutdown"); let TaskRunnerProcess = class TaskRunnerProcess { get isRunning() { return this.process !== null; } get pid() { return this.process?.pid; } get runPromise() { return this._runPromise; } get useLauncher() { return this.runnerConfig.mode === 'internal_launcher'; } constructor(runnerConfig, authService) { this.runnerConfig = runnerConfig; this.authService = authService; this.process = null; this._runPromise = null; this.isShuttingDown = false; a.ok(this.runnerConfig.mode === 'internal_childprocess' || this.runnerConfig.mode === 'internal_launcher'); } async start() { a.ok(!this.process, 'Task Runner Process already running'); const grantToken = await this.authService.createGrantToken(); const n8nUri = `127.0.0.1:${this.runnerConfig.port}`; this.process = this.useLauncher ? this.startLauncher(grantToken, n8nUri) : this.startNode(grantToken, n8nUri); this.process.stdout?.pipe(process.stdout); this.process.stderr?.pipe(process.stderr); this.monitorProcess(this.process); } startNode(grantToken, n8nUri) { const startScript = require.resolve('@n8n/task-runner'); return (0, node_child_process_1.spawn)('node', [startScript], { env: { PATH: process.env.PATH, N8N_RUNNERS_GRANT_TOKEN: grantToken, N8N_RUNNERS_N8N_URI: n8nUri, N8N_RUNNERS_MAX_PAYLOAD: this.runnerConfig.maxPayload.toString(), NODE_FUNCTION_ALLOW_BUILTIN: process.env.NODE_FUNCTION_ALLOW_BUILTIN, NODE_FUNCTION_ALLOW_EXTERNAL: process.env.NODE_FUNCTION_ALLOW_EXTERNAL, }, }); } startLauncher(grantToken, n8nUri) { return (0, node_child_process_1.spawn)(this.runnerConfig.launcherPath, ['launch', this.runnerConfig.launcherRunner], { env: { PATH: process.env.PATH, N8N_RUNNERS_GRANT_TOKEN: grantToken, N8N_RUNNERS_N8N_URI: n8nUri, N8N_RUNNERS_MAX_PAYLOAD: this.runnerConfig.maxPayload.toString(), NODE_FUNCTION_ALLOW_BUILTIN: process.env.NODE_FUNCTION_ALLOW_BUILTIN, NODE_FUNCTION_ALLOW_EXTERNAL: process.env.NODE_FUNCTION_ALLOW_EXTERNAL, RUST_LOG: process.env.RUST_LOG, }, }); } async stop() { if (!this.process) { return; } this.isShuttingDown = true; if (this.useLauncher) { await this.killLauncher(); } else { this.killNode(); } await this._runPromise; this.isShuttingDown = false; } killNode() { if (!this.process) { return; } this.process.kill(); } async killLauncher() { if (!this.process?.pid) { return; } const killProcess = (0, node_child_process_1.spawn)(this.runnerConfig.launcherPath, [ 'kill', this.runnerConfig.launcherRunner, this.process.pid.toString(), ]); await new Promise((resolve) => { killProcess.on('exit', () => { resolve(); }); }); } monitorProcess(taskRunnerProcess) { this._runPromise = new Promise((resolve) => { taskRunnerProcess.on('exit', (code) => { this.onProcessExit(code, resolve); }); }); } onProcessExit(_code, resolveFn) { this.process = null; resolveFn(); if (!this.isShuttingDown) { setImmediate(async () => await this.start()); } } }; exports.TaskRunnerProcess = TaskRunnerProcess; __decorate([ (0, on_shutdown_1.OnShutdown)(), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], TaskRunnerProcess.prototype, "stop", null); exports.TaskRunnerProcess = TaskRunnerProcess = __decorate([ (0, typedi_1.Service)(), __metadata("design:paramtypes", [config_1.TaskRunnersConfig, task_runner_auth_service_1.TaskRunnerAuthService]) ], TaskRunnerProcess); //# sourceMappingURL=task-runner-process.js.map