mirror of
https://github.com/n8n-io/n8n.git
synced 2025-01-11 04:47:29 -08:00
refactor(core): Have WorkerServer
use InstanceSettings
(#10840)
This commit is contained in:
parent
94aa680c9b
commit
7f4ef31507
|
@ -1,10 +1,10 @@
|
||||||
import type { GlobalConfig } from '@n8n/config';
|
import type { GlobalConfig } from '@n8n/config';
|
||||||
import type express from 'express';
|
import type express from 'express';
|
||||||
import { mock } from 'jest-mock-extended';
|
import { mock } from 'jest-mock-extended';
|
||||||
|
import type { InstanceSettings } from 'n8n-core';
|
||||||
import { AssertionError } from 'node:assert';
|
import { AssertionError } from 'node:assert';
|
||||||
import * as http from 'node:http';
|
import * as http from 'node:http';
|
||||||
|
|
||||||
import config from '@/config';
|
|
||||||
import { PortTakenError } from '@/errors/port-taken.error';
|
import { PortTakenError } from '@/errors/port-taken.error';
|
||||||
import type { ExternalHooks } from '@/external-hooks';
|
import type { ExternalHooks } from '@/external-hooks';
|
||||||
import { bodyParser, rawBodyReader } from '@/middlewares';
|
import { bodyParser, rawBodyReader } from '@/middlewares';
|
||||||
|
@ -27,9 +27,9 @@ describe('WorkerServer', () => {
|
||||||
let globalConfig: GlobalConfig;
|
let globalConfig: GlobalConfig;
|
||||||
|
|
||||||
const externalHooks = mock<ExternalHooks>();
|
const externalHooks = mock<ExternalHooks>();
|
||||||
|
const instanceSettings = mock<InstanceSettings>({ instanceType: 'worker' });
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
config.set('generic.instanceType', 'worker');
|
|
||||||
globalConfig = mock<GlobalConfig>({
|
globalConfig = mock<GlobalConfig>({
|
||||||
queue: {
|
queue: {
|
||||||
health: { active: true, port: 5678 },
|
health: { active: true, port: 5678 },
|
||||||
|
@ -43,10 +43,16 @@ describe('WorkerServer', () => {
|
||||||
|
|
||||||
describe('constructor', () => {
|
describe('constructor', () => {
|
||||||
it('should throw if non-worker instance type', () => {
|
it('should throw if non-worker instance type', () => {
|
||||||
config.set('generic.instanceType', 'webhook');
|
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
() => new WorkerServer(globalConfig, mock(), mock(), mock(), externalHooks),
|
() =>
|
||||||
|
new WorkerServer(
|
||||||
|
globalConfig,
|
||||||
|
mock(),
|
||||||
|
mock(),
|
||||||
|
mock(),
|
||||||
|
externalHooks,
|
||||||
|
mock<InstanceSettings>({ instanceType: 'webhook' }),
|
||||||
|
),
|
||||||
).toThrowError(AssertionError);
|
).toThrowError(AssertionError);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -61,14 +67,15 @@ describe('WorkerServer', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
() => new WorkerServer(globalConfig, mock(), mock(), mock(), externalHooks),
|
() =>
|
||||||
|
new WorkerServer(globalConfig, mock(), mock(), mock(), externalHooks, instanceSettings),
|
||||||
).toThrowError(PortTakenError);
|
).toThrowError(PortTakenError);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should set up `/healthz` if health check is enabled', async () => {
|
it('should set up `/healthz` if health check is enabled', async () => {
|
||||||
jest.spyOn(http, 'createServer').mockReturnValue(mock<http.Server>());
|
jest.spyOn(http, 'createServer').mockReturnValue(mock<http.Server>());
|
||||||
|
|
||||||
new WorkerServer(globalConfig, mock(), mock(), mock(), externalHooks);
|
new WorkerServer(globalConfig, mock(), mock(), mock(), externalHooks, instanceSettings);
|
||||||
|
|
||||||
expect(app.get).toHaveBeenCalledWith('/healthz', expect.any(Function));
|
expect(app.get).toHaveBeenCalledWith('/healthz', expect.any(Function));
|
||||||
});
|
});
|
||||||
|
@ -78,7 +85,7 @@ describe('WorkerServer', () => {
|
||||||
|
|
||||||
jest.spyOn(http, 'createServer').mockReturnValue(mock<http.Server>());
|
jest.spyOn(http, 'createServer').mockReturnValue(mock<http.Server>());
|
||||||
|
|
||||||
new WorkerServer(globalConfig, mock(), mock(), mock(), externalHooks);
|
new WorkerServer(globalConfig, mock(), mock(), mock(), externalHooks, instanceSettings);
|
||||||
|
|
||||||
expect(app.get).not.toHaveBeenCalled();
|
expect(app.get).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
@ -89,7 +96,7 @@ describe('WorkerServer', () => {
|
||||||
const CREDENTIALS_OVERWRITE_ENDPOINT = 'credentials/overwrites';
|
const CREDENTIALS_OVERWRITE_ENDPOINT = 'credentials/overwrites';
|
||||||
globalConfig.credentials.overwrite.endpoint = CREDENTIALS_OVERWRITE_ENDPOINT;
|
globalConfig.credentials.overwrite.endpoint = CREDENTIALS_OVERWRITE_ENDPOINT;
|
||||||
|
|
||||||
new WorkerServer(globalConfig, mock(), mock(), mock(), externalHooks);
|
new WorkerServer(globalConfig, mock(), mock(), mock(), externalHooks, instanceSettings);
|
||||||
|
|
||||||
expect(app.post).toHaveBeenCalledWith(
|
expect(app.post).toHaveBeenCalledWith(
|
||||||
`/${CREDENTIALS_OVERWRITE_ENDPOINT}`,
|
`/${CREDENTIALS_OVERWRITE_ENDPOINT}`,
|
||||||
|
@ -102,7 +109,7 @@ describe('WorkerServer', () => {
|
||||||
it('should not set up `/:endpoint` if overwrites endpoint is not set', async () => {
|
it('should not set up `/:endpoint` if overwrites endpoint is not set', async () => {
|
||||||
jest.spyOn(http, 'createServer').mockReturnValue(mock<http.Server>());
|
jest.spyOn(http, 'createServer').mockReturnValue(mock<http.Server>());
|
||||||
|
|
||||||
new WorkerServer(globalConfig, mock(), mock(), mock(), externalHooks);
|
new WorkerServer(globalConfig, mock(), mock(), mock(), externalHooks, instanceSettings);
|
||||||
|
|
||||||
expect(app.post).not.toHaveBeenCalled();
|
expect(app.post).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
@ -118,7 +125,14 @@ describe('WorkerServer', () => {
|
||||||
return server;
|
return server;
|
||||||
});
|
});
|
||||||
|
|
||||||
const workerServer = new WorkerServer(globalConfig, mock(), mock(), mock(), externalHooks);
|
const workerServer = new WorkerServer(
|
||||||
|
globalConfig,
|
||||||
|
mock(),
|
||||||
|
mock(),
|
||||||
|
mock(),
|
||||||
|
externalHooks,
|
||||||
|
instanceSettings,
|
||||||
|
);
|
||||||
await workerServer.init();
|
await workerServer.init();
|
||||||
|
|
||||||
expect(externalHooks.run).toHaveBeenCalledWith('worker.ready');
|
expect(externalHooks.run).toHaveBeenCalledWith('worker.ready');
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
import { GlobalConfig } from '@n8n/config';
|
import { GlobalConfig } from '@n8n/config';
|
||||||
import express from 'express';
|
import express from 'express';
|
||||||
|
import { InstanceSettings } from 'n8n-core';
|
||||||
import { ensureError } from 'n8n-workflow';
|
import { ensureError } from 'n8n-workflow';
|
||||||
import { strict as assert } from 'node:assert';
|
import { strict as assert } from 'node:assert';
|
||||||
import http from 'node:http';
|
import http from 'node:http';
|
||||||
import type { Server } from 'node:http';
|
import type { Server } from 'node:http';
|
||||||
import { Service } from 'typedi';
|
import { Service } from 'typedi';
|
||||||
|
|
||||||
import config from '@/config';
|
|
||||||
import { CredentialsOverwrites } from '@/credentials-overwrites';
|
import { CredentialsOverwrites } from '@/credentials-overwrites';
|
||||||
import * as Db from '@/db';
|
import * as Db from '@/db';
|
||||||
import { CredentialsOverwritesAlreadySetError } from '@/errors/credentials-overwrites-already-set.error';
|
import { CredentialsOverwritesAlreadySetError } from '@/errors/credentials-overwrites-already-set.error';
|
||||||
|
@ -40,8 +40,9 @@ export class WorkerServer {
|
||||||
private readonly scalingService: ScalingService,
|
private readonly scalingService: ScalingService,
|
||||||
private readonly credentialsOverwrites: CredentialsOverwrites,
|
private readonly credentialsOverwrites: CredentialsOverwrites,
|
||||||
private readonly externalHooks: ExternalHooks,
|
private readonly externalHooks: ExternalHooks,
|
||||||
|
private readonly instanceSettings: InstanceSettings,
|
||||||
) {
|
) {
|
||||||
assert(config.getEnv('generic.instanceType') === 'worker');
|
assert(this.instanceSettings.instanceType === 'worker');
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue