feat: Enable running task runners externally (no-changelog) (#11319)

This commit is contained in:
Tomi Turtiainen
2024-10-22 16:23:59 +03:00
committed by GitHub
parent 216b119350
commit 8404282046
12 changed files with 99 additions and 123 deletions

View File

@@ -1,4 +1,4 @@
import { GlobalConfig } from '@n8n/config';
import { TaskRunnersConfig } from '@n8n/config';
import * as a from 'node:assert/strict';
import { spawn } from 'node:child_process';
import * as process from 'node:process';
@@ -28,6 +28,10 @@ export class TaskRunnerProcess {
return this._runPromise;
}
private get useLauncher() {
return this.runnerConfig.mode === 'internal_launcher';
}
private process: ChildProcess | null = null;
private _runPromise: Promise<void> | null = null;
@@ -35,17 +39,22 @@ export class TaskRunnerProcess {
private isShuttingDown = false;
constructor(
private readonly globalConfig: GlobalConfig,
private readonly runnerConfig: TaskRunnersConfig,
private readonly authService: TaskRunnerAuthService,
) {}
) {
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.globalConfig.taskRunners.port}`;
this.process = this.globalConfig.taskRunners.useLauncher
const n8nUri = `127.0.0.1:${this.runnerConfig.port}`;
this.process = this.useLauncher
? this.startLauncher(grantToken, n8nUri)
: this.startNode(grantToken, n8nUri);
@@ -70,21 +79,17 @@ export class TaskRunnerProcess {
}
startLauncher(grantToken: string, n8nUri: string) {
return spawn(
this.globalConfig.taskRunners.launcherPath,
['launch', this.globalConfig.taskRunners.launcherRunner],
{
env: {
PATH: process.env.PATH,
N8N_RUNNERS_GRANT_TOKEN: grantToken,
N8N_RUNNERS_N8N_URI: n8nUri,
NODE_FUNCTION_ALLOW_BUILTIN: process.env.NODE_FUNCTION_ALLOW_BUILTIN,
NODE_FUNCTION_ALLOW_EXTERNAL: process.env.NODE_FUNCTION_ALLOW_EXTERNAL,
// For debug logging if enabled
RUST_LOG: process.env.RUST_LOG,
},
return spawn(this.runnerConfig.launcherPath, ['launch', this.runnerConfig.launcherRunner], {
env: {
PATH: process.env.PATH,
N8N_RUNNERS_GRANT_TOKEN: grantToken,
N8N_RUNNERS_N8N_URI: n8nUri,
NODE_FUNCTION_ALLOW_BUILTIN: process.env.NODE_FUNCTION_ALLOW_BUILTIN,
NODE_FUNCTION_ALLOW_EXTERNAL: process.env.NODE_FUNCTION_ALLOW_EXTERNAL,
// For debug logging if enabled
RUST_LOG: process.env.RUST_LOG,
},
);
});
}
@OnShutdown()
@@ -96,7 +101,7 @@ export class TaskRunnerProcess {
this.isShuttingDown = true;
// TODO: Timeout & force kill
if (this.globalConfig.taskRunners.useLauncher) {
if (this.useLauncher) {
await this.killLauncher();
} else {
this.killNode();
@@ -118,9 +123,9 @@ export class TaskRunnerProcess {
return;
}
const killProcess = spawn(this.globalConfig.taskRunners.launcherPath, [
const killProcess = spawn(this.runnerConfig.launcherPath, [
'kill',
this.globalConfig.taskRunners.launcherRunner,
this.runnerConfig.launcherRunner,
this.process.pid.toString(),
]);