2024-10-22 06:23:59 -07:00
|
|
|
import { TaskRunnersConfig } from '@n8n/config';
|
2024-10-02 05:16:02 -07:00
|
|
|
import Container from 'typedi';
|
|
|
|
|
2024-11-04 06:12:29 -08:00
|
|
|
import { TaskRunnerWsServer } from '@/runners/runner-ws-server';
|
2024-10-02 05:16:02 -07:00
|
|
|
import { TaskBroker } from '@/runners/task-broker.service';
|
|
|
|
import { TaskRunnerProcess } from '@/runners/task-runner-process';
|
2024-10-02 06:38:42 -07:00
|
|
|
import { TaskRunnerServer } from '@/runners/task-runner-server';
|
2024-10-02 05:16:02 -07:00
|
|
|
import { retryUntil } from '@test-integration/retry-until';
|
|
|
|
|
|
|
|
describe('TaskRunnerProcess', () => {
|
|
|
|
const authToken = 'token';
|
2024-10-22 06:23:59 -07:00
|
|
|
const runnerConfig = Container.get(TaskRunnersConfig);
|
|
|
|
runnerConfig.disabled = false;
|
|
|
|
runnerConfig.mode = 'internal_childprocess';
|
|
|
|
runnerConfig.authToken = authToken;
|
|
|
|
runnerConfig.port = 0; // Use any port
|
2024-10-02 06:38:42 -07:00
|
|
|
const taskRunnerServer = Container.get(TaskRunnerServer);
|
2024-10-02 05:16:02 -07:00
|
|
|
|
|
|
|
const runnerProcess = Container.get(TaskRunnerProcess);
|
|
|
|
const taskBroker = Container.get(TaskBroker);
|
2024-11-04 06:12:29 -08:00
|
|
|
const taskRunnerService = Container.get(TaskRunnerWsServer);
|
2024-10-02 05:16:02 -07:00
|
|
|
|
2024-10-14 06:19:17 -07:00
|
|
|
const startLauncherSpy = jest.spyOn(runnerProcess, 'startLauncher');
|
|
|
|
const startNodeSpy = jest.spyOn(runnerProcess, 'startNode');
|
|
|
|
const killLauncherSpy = jest.spyOn(runnerProcess, 'killLauncher');
|
|
|
|
const killNodeSpy = jest.spyOn(runnerProcess, 'killNode');
|
|
|
|
|
2024-10-02 06:38:42 -07:00
|
|
|
beforeAll(async () => {
|
|
|
|
await taskRunnerServer.start();
|
|
|
|
// Set the port to the actually used port
|
2024-10-22 06:23:59 -07:00
|
|
|
runnerConfig.port = taskRunnerServer.port;
|
2024-10-02 06:38:42 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
await taskRunnerServer.stop();
|
|
|
|
});
|
|
|
|
|
2024-10-02 05:16:02 -07:00
|
|
|
afterEach(async () => {
|
|
|
|
await runnerProcess.stop();
|
2024-10-14 06:19:17 -07:00
|
|
|
|
|
|
|
startLauncherSpy.mockClear();
|
|
|
|
startNodeSpy.mockClear();
|
|
|
|
killLauncherSpy.mockClear();
|
|
|
|
killNodeSpy.mockClear();
|
2024-10-02 05:16:02 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
const getNumConnectedRunners = () => taskRunnerService.runnerConnections.size;
|
|
|
|
const getNumRegisteredRunners = () => taskBroker.getKnownRunners().size;
|
|
|
|
|
|
|
|
it('should start and connect the task runner', async () => {
|
|
|
|
// Act
|
|
|
|
await runnerProcess.start();
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
expect(runnerProcess.isRunning).toBeTruthy();
|
|
|
|
|
|
|
|
// Wait until the runner has connected
|
|
|
|
await retryUntil(() => expect(getNumConnectedRunners()).toBe(1));
|
|
|
|
expect(getNumRegisteredRunners()).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should stop an disconnect the task runner', async () => {
|
|
|
|
// Arrange
|
|
|
|
await runnerProcess.start();
|
|
|
|
|
|
|
|
// Wait until the runner has connected
|
|
|
|
await retryUntil(() => expect(getNumConnectedRunners()).toBe(1));
|
|
|
|
expect(getNumRegisteredRunners()).toBe(1);
|
|
|
|
|
|
|
|
// Act
|
|
|
|
await runnerProcess.stop();
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
// Wait until the runner has disconnected
|
|
|
|
await retryUntil(() => expect(getNumConnectedRunners()).toBe(0));
|
|
|
|
|
|
|
|
expect(runnerProcess.isRunning).toBeFalsy();
|
|
|
|
expect(getNumRegisteredRunners()).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should restart the task runner if it exits', async () => {
|
|
|
|
// Arrange
|
|
|
|
await runnerProcess.start();
|
|
|
|
|
|
|
|
// Wait until the runner has connected
|
|
|
|
await retryUntil(() => expect(getNumConnectedRunners()).toBe(1));
|
|
|
|
const processId = runnerProcess.pid;
|
|
|
|
|
|
|
|
// Act
|
|
|
|
// @ts-expect-error private property
|
|
|
|
runnerProcess.process?.kill('SIGKILL');
|
|
|
|
|
2024-10-18 01:33:08 -07:00
|
|
|
// Wait until the runner has exited
|
|
|
|
await runnerProcess.runPromise;
|
2024-10-02 05:16:02 -07:00
|
|
|
|
2024-10-18 01:33:08 -07:00
|
|
|
// Assert
|
2024-10-02 05:16:02 -07:00
|
|
|
// Wait until the runner has connected again
|
|
|
|
await retryUntil(() => expect(getNumConnectedRunners()).toBe(1));
|
|
|
|
expect(getNumConnectedRunners()).toBe(1);
|
|
|
|
expect(getNumRegisteredRunners()).toBe(1);
|
2024-10-18 01:33:08 -07:00
|
|
|
expect(runnerProcess.pid).not.toBe(processId);
|
2024-10-02 05:16:02 -07:00
|
|
|
});
|
2024-10-14 06:19:17 -07:00
|
|
|
|
|
|
|
it('should launch runner directly if not using a launcher', async () => {
|
2024-10-22 06:23:59 -07:00
|
|
|
runnerConfig.mode = 'internal_childprocess';
|
2024-10-14 06:19:17 -07:00
|
|
|
|
|
|
|
await runnerProcess.start();
|
|
|
|
|
|
|
|
expect(startLauncherSpy).toBeCalledTimes(0);
|
|
|
|
expect(startNodeSpy).toBeCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should use a launcher if configured', async () => {
|
2024-10-22 06:23:59 -07:00
|
|
|
runnerConfig.mode = 'internal_launcher';
|
|
|
|
runnerConfig.launcherPath = 'node';
|
2024-10-14 06:19:17 -07:00
|
|
|
|
|
|
|
await runnerProcess.start();
|
|
|
|
|
|
|
|
expect(startLauncherSpy).toBeCalledTimes(1);
|
|
|
|
expect(startNodeSpy).toBeCalledTimes(0);
|
2024-10-22 06:23:59 -07:00
|
|
|
runnerConfig.mode = 'internal_childprocess';
|
2024-10-14 06:19:17 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should kill the process directly if not using a launcher', async () => {
|
2024-10-22 06:23:59 -07:00
|
|
|
runnerConfig.mode = 'internal_childprocess';
|
2024-10-14 06:19:17 -07:00
|
|
|
|
|
|
|
await runnerProcess.start();
|
|
|
|
await runnerProcess.stop();
|
|
|
|
|
|
|
|
expect(killLauncherSpy).toBeCalledTimes(0);
|
|
|
|
expect(killNodeSpy).toBeCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should kill the process using a launcher if configured', async () => {
|
2024-10-22 06:23:59 -07:00
|
|
|
runnerConfig.mode = 'internal_launcher';
|
|
|
|
runnerConfig.launcherPath = 'node';
|
2024-10-14 06:19:17 -07:00
|
|
|
|
|
|
|
await runnerProcess.start();
|
|
|
|
await runnerProcess.stop();
|
|
|
|
|
|
|
|
expect(killLauncherSpy).toBeCalledTimes(1);
|
|
|
|
expect(killNodeSpy).toBeCalledTimes(0);
|
2024-10-22 06:23:59 -07:00
|
|
|
runnerConfig.mode = 'internal_childprocess';
|
2024-10-14 06:19:17 -07:00
|
|
|
});
|
2024-10-02 05:16:02 -07:00
|
|
|
});
|