mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-18 02:21:13 +00:00
refactor(core): Set up worker server (#10814)
This commit is contained in:
127
packages/cli/src/scaling/__tests__/worker-server.test.ts
Normal file
127
packages/cli/src/scaling/__tests__/worker-server.test.ts
Normal file
@@ -0,0 +1,127 @@
|
||||
import type { GlobalConfig } from '@n8n/config';
|
||||
import type express from 'express';
|
||||
import { mock } from 'jest-mock-extended';
|
||||
import { AssertionError } from 'node:assert';
|
||||
import * as http from 'node:http';
|
||||
|
||||
import config from '@/config';
|
||||
import { PortTakenError } from '@/errors/port-taken.error';
|
||||
import type { ExternalHooks } from '@/external-hooks';
|
||||
import { bodyParser, rawBodyReader } from '@/middlewares';
|
||||
|
||||
import { WorkerServer } from '../worker-server';
|
||||
|
||||
const app = mock<express.Application>();
|
||||
|
||||
jest.mock('node:http');
|
||||
jest.mock('express', () => ({ __esModule: true, default: () => app }));
|
||||
|
||||
const addressInUseError = () => {
|
||||
const error: NodeJS.ErrnoException = new Error('Port already in use');
|
||||
error.code = 'EADDRINUSE';
|
||||
|
||||
return error;
|
||||
};
|
||||
|
||||
describe('WorkerServer', () => {
|
||||
let globalConfig: GlobalConfig;
|
||||
|
||||
const externalHooks = mock<ExternalHooks>();
|
||||
|
||||
beforeEach(() => {
|
||||
config.set('generic.instanceType', 'worker');
|
||||
globalConfig = mock<GlobalConfig>({
|
||||
queue: {
|
||||
health: { active: true, port: 5678 },
|
||||
},
|
||||
credentials: {
|
||||
overwrite: { endpoint: '' },
|
||||
},
|
||||
});
|
||||
jest.restoreAllMocks();
|
||||
});
|
||||
|
||||
describe('constructor', () => {
|
||||
it('should throw if non-worker instance type', () => {
|
||||
config.set('generic.instanceType', 'webhook');
|
||||
|
||||
expect(
|
||||
() => new WorkerServer(globalConfig, mock(), mock(), mock(), externalHooks),
|
||||
).toThrowError(AssertionError);
|
||||
});
|
||||
|
||||
it('should throw if port taken', async () => {
|
||||
const server = mock<http.Server>();
|
||||
|
||||
jest.spyOn(http, 'createServer').mockReturnValue(server);
|
||||
|
||||
server.on.mockImplementation((event: string, callback: (arg?: unknown) => void) => {
|
||||
if (event === 'error') callback(addressInUseError());
|
||||
return server;
|
||||
});
|
||||
|
||||
expect(
|
||||
() => new WorkerServer(globalConfig, mock(), mock(), mock(), externalHooks),
|
||||
).toThrowError(PortTakenError);
|
||||
});
|
||||
|
||||
it('should set up `/healthz` if health check is enabled', async () => {
|
||||
jest.spyOn(http, 'createServer').mockReturnValue(mock<http.Server>());
|
||||
|
||||
new WorkerServer(globalConfig, mock(), mock(), mock(), externalHooks);
|
||||
|
||||
expect(app.get).toHaveBeenCalledWith('/healthz', expect.any(Function));
|
||||
});
|
||||
|
||||
it('should not set up `/healthz` if health check is disabled', async () => {
|
||||
globalConfig.queue.health.active = false;
|
||||
|
||||
jest.spyOn(http, 'createServer').mockReturnValue(mock<http.Server>());
|
||||
|
||||
new WorkerServer(globalConfig, mock(), mock(), mock(), externalHooks);
|
||||
|
||||
expect(app.get).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should set up `/:endpoint` if overwrites endpoint is set', async () => {
|
||||
jest.spyOn(http, 'createServer').mockReturnValue(mock<http.Server>());
|
||||
|
||||
const CREDENTIALS_OVERWRITE_ENDPOINT = 'credentials/overwrites';
|
||||
globalConfig.credentials.overwrite.endpoint = CREDENTIALS_OVERWRITE_ENDPOINT;
|
||||
|
||||
new WorkerServer(globalConfig, mock(), mock(), mock(), externalHooks);
|
||||
|
||||
expect(app.post).toHaveBeenCalledWith(
|
||||
`/${CREDENTIALS_OVERWRITE_ENDPOINT}`,
|
||||
rawBodyReader,
|
||||
bodyParser,
|
||||
expect.any(Function),
|
||||
);
|
||||
});
|
||||
|
||||
it('should not set up `/:endpoint` if overwrites endpoint is not set', async () => {
|
||||
jest.spyOn(http, 'createServer').mockReturnValue(mock<http.Server>());
|
||||
|
||||
new WorkerServer(globalConfig, mock(), mock(), mock(), externalHooks);
|
||||
|
||||
expect(app.post).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('init', () => {
|
||||
it('should call `worker.ready` external hook', async () => {
|
||||
const server = mock<http.Server>();
|
||||
jest.spyOn(http, 'createServer').mockReturnValue(server);
|
||||
|
||||
server.listen.mockImplementation((_port, callback: () => void) => {
|
||||
callback();
|
||||
return server;
|
||||
});
|
||||
|
||||
const workerServer = new WorkerServer(globalConfig, mock(), mock(), mock(), externalHooks);
|
||||
await workerServer.init();
|
||||
|
||||
expect(externalHooks.run).toHaveBeenCalledWith('worker.ready');
|
||||
});
|
||||
});
|
||||
});
|
||||
129
packages/cli/src/scaling/worker-server.ts
Normal file
129
packages/cli/src/scaling/worker-server.ts
Normal file
@@ -0,0 +1,129 @@
|
||||
import { GlobalConfig } from '@n8n/config';
|
||||
import express from 'express';
|
||||
import { ensureError } from 'n8n-workflow';
|
||||
import { strict as assert } from 'node:assert';
|
||||
import http from 'node:http';
|
||||
import type { Server } from 'node:http';
|
||||
import { Service } from 'typedi';
|
||||
|
||||
import config from '@/config';
|
||||
import { CredentialsOverwrites } from '@/credentials-overwrites';
|
||||
import * as Db from '@/db';
|
||||
import { CredentialsOverwritesAlreadySetError } from '@/errors/credentials-overwrites-already-set.error';
|
||||
import { NonJsonBodyError } from '@/errors/non-json-body.error';
|
||||
import { PortTakenError } from '@/errors/port-taken.error';
|
||||
import { ServiceUnavailableError } from '@/errors/response-errors/service-unavailable.error';
|
||||
import { ExternalHooks } from '@/external-hooks';
|
||||
import type { ICredentialsOverwrite } from '@/interfaces';
|
||||
import { Logger } from '@/logger';
|
||||
import { rawBodyReader, bodyParser } from '@/middlewares';
|
||||
import * as ResponseHelper from '@/response-helper';
|
||||
import { ScalingService } from '@/scaling/scaling.service';
|
||||
|
||||
/**
|
||||
* Responsible for handling HTTP requests sent to a worker.
|
||||
*/
|
||||
@Service()
|
||||
export class WorkerServer {
|
||||
private readonly port: number;
|
||||
|
||||
private readonly server: Server;
|
||||
|
||||
/**
|
||||
* @doc https://docs.n8n.io/embed/configuration/#credential-overwrites
|
||||
*/
|
||||
private overwritesLoaded = false;
|
||||
|
||||
constructor(
|
||||
private readonly globalConfig: GlobalConfig,
|
||||
private readonly logger: Logger,
|
||||
private readonly scalingService: ScalingService,
|
||||
private readonly credentialsOverwrites: CredentialsOverwrites,
|
||||
private readonly externalHooks: ExternalHooks,
|
||||
) {
|
||||
assert(config.getEnv('generic.instanceType') === 'worker');
|
||||
|
||||
const app = express();
|
||||
|
||||
app.disable('x-powered-by');
|
||||
|
||||
this.server = http.createServer(app);
|
||||
|
||||
this.port = this.globalConfig.queue.health.port;
|
||||
|
||||
const overwritesEndpoint = this.globalConfig.credentials.overwrite.endpoint;
|
||||
|
||||
this.server.on('error', (error: NodeJS.ErrnoException) => {
|
||||
if (error.code === 'EADDRINUSE') throw new PortTakenError(this.port);
|
||||
});
|
||||
|
||||
if (this.globalConfig.queue.health.active) {
|
||||
app.get('/healthz', async (req, res) => await this.healthcheck(req, res));
|
||||
}
|
||||
|
||||
if (overwritesEndpoint !== '') {
|
||||
app.post(`/${overwritesEndpoint}`, rawBodyReader, bodyParser, (req, res) =>
|
||||
this.handleOverwrites(req, res),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async init() {
|
||||
await new Promise<void>((resolve) => this.server.listen(this.port, resolve));
|
||||
|
||||
await this.externalHooks.run('worker.ready');
|
||||
|
||||
this.logger.info(`\nn8n worker server listening on port ${this.port}`);
|
||||
}
|
||||
|
||||
private async healthcheck(_req: express.Request, res: express.Response) {
|
||||
this.logger.debug('[WorkerServer] Health check started');
|
||||
|
||||
try {
|
||||
await Db.getConnection().query('SELECT 1');
|
||||
} catch (value) {
|
||||
this.logger.error('[WorkerServer] No database connection', ensureError(value));
|
||||
|
||||
return ResponseHelper.sendErrorResponse(
|
||||
res,
|
||||
new ServiceUnavailableError('No database connection'),
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
await this.scalingService.pingQueue();
|
||||
} catch (value) {
|
||||
this.logger.error('[WorkerServer] No Redis connection', ensureError(value));
|
||||
|
||||
return ResponseHelper.sendErrorResponse(
|
||||
res,
|
||||
new ServiceUnavailableError('No Redis connection'),
|
||||
);
|
||||
}
|
||||
|
||||
this.logger.debug('[WorkerServer] Health check succeeded');
|
||||
|
||||
ResponseHelper.sendSuccessResponse(res, { status: 'ok' }, true, 200);
|
||||
}
|
||||
|
||||
private handleOverwrites(
|
||||
req: express.Request<{}, {}, ICredentialsOverwrite>,
|
||||
res: express.Response,
|
||||
) {
|
||||
if (this.overwritesLoaded) {
|
||||
ResponseHelper.sendErrorResponse(res, new CredentialsOverwritesAlreadySetError());
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.contentType !== 'application/json') {
|
||||
ResponseHelper.sendErrorResponse(res, new NonJsonBodyError());
|
||||
return;
|
||||
}
|
||||
|
||||
this.credentialsOverwrites.setData(req.body);
|
||||
|
||||
this.overwritesLoaded = true;
|
||||
|
||||
ResponseHelper.sendSuccessResponse(res, { success: true }, true, 200);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user