Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Lazy-load queue-mode and analytics dependencies #5061

Merged
merged 4 commits into from
Jan 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
"@oclif/dev-cli": "^1.22.2",
"@types/basic-auth": "^1.1.2",
"@types/bcryptjs": "^2.4.2",
"@types/bull": "^3.3.10",
"@types/compression": "1.0.1",
"@types/connect-history-api-fallback": "^1.3.1",
"@types/convict": "^4.2.1",
Expand Down Expand Up @@ -108,14 +107,14 @@
"@oclif/core": "^1.16.4",
"@oclif/errors": "^1.3.6",
"@rudderstack/rudder-sdk-node": "1.0.6",
"@sentry/integrations": "^7.17.3",
"@sentry/node": "^7.17.3",
"@sentry/integrations": "^7.28.1",
"@sentry/node": "^7.28.1",
"axios": "^0.21.1",
"basic-auth": "^2.0.1",
"bcryptjs": "^2.4.3",
"body-parser": "^1.18.3",
"body-parser-xml": "^2.0.3",
"bull": "^3.19.0",
"bull": "^4.10.2",
"callsites": "^3.1.0",
"change-case": "^4.1.1",
"class-validator": "^0.13.1",
Expand All @@ -135,7 +134,7 @@
"google-timezones-json": "^1.0.2",
"handlebars": "4.7.7",
"inquirer": "^7.0.1",
"ioredis": "^4.28.5",
"ioredis": "^5.2.4",
"json-diff": "^0.5.4",
"jsonschema": "^1.4.1",
"jsonwebtoken": "^8.5.1",
Expand Down
18 changes: 11 additions & 7 deletions packages/cli/src/ErrorReporting.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import * as Sentry from '@sentry/node';
import { RewriteFrames } from '@sentry/integrations';
import type { Application } from 'express';
import config from '@/config';
import { ErrorReporterProxy } from 'n8n-workflow';

let initialized = false;

export const initErrorHandling = () => {
export const initErrorHandling = async () => {
if (initialized) return;

if (!config.getEnv('diagnostics.enabled')) {
Expand All @@ -20,7 +18,11 @@ export const initErrorHandling = () => {
const dsn = config.getEnv('diagnostics.config.sentry.dsn');
const { N8N_VERSION: release, ENVIRONMENT: environment } = process.env;

Sentry.init({
const { init, captureException } = await import('@sentry/node');
// eslint-disable-next-line @typescript-eslint/naming-convention
const { RewriteFrames } = await import('@sentry/integrations');

init({
dsn,
release,
environment,
Expand All @@ -37,14 +39,16 @@ export const initErrorHandling = () => {
});

ErrorReporterProxy.init({
report: (error, options) => Sentry.captureException(error, options),
report: (error, options) => captureException(error, options),
});

initialized = true;
};

export const setupErrorMiddleware = (app: Application) => {
const { requestHandler, errorHandler } = Sentry.Handlers;
export const setupErrorMiddleware = async (app: Application) => {
const {
Handlers: { requestHandler, errorHandler },
} = await import('@sentry/node');
app.use(requestHandler());
app.use(errorHandler());
};
10 changes: 8 additions & 2 deletions packages/cli/src/InternalHooksManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ export class InternalHooksManager {
throw new Error('InternalHooks not initialized');
}

static init(instanceId: string, versionCli: string, nodeTypes: INodeTypes): InternalHooksClass {
static async init(
instanceId: string,
versionCli: string,
nodeTypes: INodeTypes,
): Promise<InternalHooksClass> {
if (!this.internalHooksInstance) {
const telemetry = new Telemetry(instanceId, versionCli);
await telemetry.init();
this.internalHooksInstance = new InternalHooksClass(
new Telemetry(instanceId, versionCli),
telemetry,
instanceId,
versionCli,
nodeTypes,
Expand Down
20 changes: 12 additions & 8 deletions packages/cli/src/Queue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Bull from 'bull';
import type Bull from 'bull';
import type { RedisOptions } from 'ioredis';
import { IExecuteResponsePromiseData } from 'n8n-workflow';
import config from '@/config';
import * as ActiveExecutions from '@/ActiveExecutions';
Expand All @@ -22,15 +23,17 @@ export interface WebhookResponse {
}

export class Queue {
private activeExecutions: ActiveExecutions.ActiveExecutions;

private jobQueue: JobQueue;

constructor() {
this.activeExecutions = ActiveExecutions.getInstance();
constructor(private activeExecutions: ActiveExecutions.ActiveExecutions) {}

async init() {
const prefix = config.getEnv('queue.bull.prefix');
const redisOptions = config.getEnv('queue.bull.redis');
const redisOptions: RedisOptions = config.getEnv('queue.bull.redis');

// eslint-disable-next-line @typescript-eslint/naming-convention
const { default: Bull } = await import('bull');

// Disabling ready check is necessary as it allows worker to
// quickly reconnect to Redis if Redis crashes or is unreachable
// for some time. With it enabled, worker might take minutes to realize
Expand Down Expand Up @@ -89,9 +92,10 @@ export class Queue {

let activeQueueInstance: Queue | undefined;

export function getInstance(): Queue {
export async function getInstance(): Promise<Queue> {
if (activeQueueInstance === undefined) {
activeQueueInstance = new Queue();
activeQueueInstance = new Queue(ActiveExecutions.getInstance());
await activeQueueInstance.init();
}

return activeQueueInstance;
Expand Down
10 changes: 6 additions & 4 deletions packages/cli/src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ class App {
this.presetCredentialsLoaded = false;
this.endpointPresetCredentials = config.getEnv('credentials.overwrite.endpoint');

setupErrorMiddleware(this.app);
void setupErrorMiddleware(this.app);

if (process.env.E2E_TESTS === 'true') {
this.app.use('/e2e', require('./api/e2e.api').e2eController);
Expand Down Expand Up @@ -1299,7 +1299,8 @@ class App {
ResponseHelper.send(
async (req: ExecutionRequest.GetAllCurrent): Promise<IExecutionsSummary[]> => {
if (config.getEnv('executions.mode') === 'queue') {
const currentJobs = await Queue.getInstance().getJobs(['active', 'waiting']);
const queue = await Queue.getInstance();
const currentJobs = await queue.getJobs(['active', 'waiting']);

const currentlyRunningQueueIds = currentJobs.map((job) => job.data.executionId);

Expand Down Expand Up @@ -1428,14 +1429,15 @@ class App {
} as IExecutionsStopData;
}

const currentJobs = await Queue.getInstance().getJobs(['active', 'waiting']);
const queue = await Queue.getInstance();
const currentJobs = await queue.getJobs(['active', 'waiting']);

const job = currentJobs.find((job) => job.data.executionId.toString() === req.params.id);

if (!job) {
throw new Error(`Could not stop "${req.params.id}" as it is no longer in queue.`);
} else {
await Queue.getInstance().stopJob(job);
await queue.stopJob(job);
}

const executionDb = (await Db.collections.Execution.findOne(
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/WebhookServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ class App {
this.presetCredentialsLoaded = false;
this.endpointPresetCredentials = config.getEnv('credentials.overwrite.endpoint');

setupErrorMiddleware(this.app);
void setupErrorMiddleware(this.app);
}

/**
Expand Down
26 changes: 12 additions & 14 deletions packages/cli/src/WorkflowRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,10 @@ export class WorkflowRunner {
constructor() {
this.push = Push.getInstance();
this.activeExecutions = ActiveExecutions.getInstance();

const executionsMode = config.getEnv('executions.mode');

if (executionsMode === 'queue') {
this.jobQueue = Queue.getInstance().getBullObjectInstance();
}

initErrorHandling();
}

/**
* The process did send a hook message so execute the appropriate hook
*
*/
processHookMessage(workflowHooks: WorkflowHooks, hookData: IProcessMessageDataHook) {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
Expand All @@ -83,7 +74,6 @@ export class WorkflowRunner {

/**
* The process did error
*
*/
async processError(
error: ExecutionError,
Expand Down Expand Up @@ -133,13 +123,20 @@ export class WorkflowRunner {
executionId?: string,
responsePromise?: IDeferredPromise<IExecuteResponsePromiseData>,
): Promise<string> {
const executionsProcess = config.getEnv('executions.process');
const executionsMode = config.getEnv('executions.mode');
const executionsProcess = config.getEnv('executions.process');

await initErrorHandling();

if (executionsMode === 'queue') {
const queue = await Queue.getInstance();
this.jobQueue = queue.getBullObjectInstance();
}

if (executionsMode === 'queue' && data.executionMode !== 'manual') {
// Do not run "manual" executions in bull because sending events to the
// frontend would not be possible
executionId = await this.runBull(
executionId = await this.enqueueExecution(
data,
loadStaticData,
realtime,
Expand Down Expand Up @@ -378,7 +375,7 @@ export class WorkflowRunner {
return executionId;
}

async runBull(
async enqueueExecution(
data: IWorkflowExecutionDataProcess,
loadStaticData?: boolean,
realtime?: boolean,
Expand Down Expand Up @@ -444,7 +441,8 @@ export class WorkflowRunner {
async (resolve, reject, onCancel) => {
onCancel.shouldReject = false;
onCancel(async () => {
await Queue.getInstance().stopJob(job);
const queue = await Queue.getInstance();
await queue.stopJob(job);

// We use "getWorkflowHooksWorkerExecuter" as "getWorkflowHooksWorkerMain" does not contain the
// "workflowExecuteAfter" which we require.
Expand Down
8 changes: 3 additions & 5 deletions packages/cli/src/WorkflowRunnerProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,12 @@ class WorkflowRunnerProcess {
}, 30000);
}

constructor() {
initErrorHandling();
}

async runWorkflow(inputData: IWorkflowExecutionDataProcessWithExecution): Promise<IRun> {
process.once('SIGTERM', WorkflowRunnerProcess.stopProcess);
process.once('SIGINT', WorkflowRunnerProcess.stopProcess);

await initErrorHandling();

// eslint-disable-next-line no-multi-assign
const logger = (this.logger = getLogger());
LoggerProxy.init(logger);
Expand Down Expand Up @@ -114,7 +112,7 @@ class WorkflowRunnerProcess {

const instanceId = (await UserSettings.prepareUserSettings()).instanceId ?? '';
const { cli } = await GenericHelpers.getVersions();
InternalHooksManager.init(instanceId, cli, nodeTypes);
await InternalHooksManager.init(instanceId, cli, nodeTypes);

const binaryDataConfig = config.getEnv('binaryDataManager');
await BinaryDataManager.init(binaryDataConfig);
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export class Execute extends Command {

const instanceId = await UserSettings.getInstanceId();
const { cli } = await GenericHelpers.getVersions();
InternalHooksManager.init(instanceId, cli, nodeTypes);
await InternalHooksManager.init(instanceId, cli, nodeTypes);

if (!WorkflowHelpers.isWorkflowIdValid(workflowId)) {
workflowId = undefined;
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/executeBatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ export class ExecuteBatch extends Command {

const instanceId = await UserSettings.getInstanceId();
const { cli } = await GenericHelpers.getVersions();
InternalHooksManager.init(instanceId, cli, nodeTypes);
await InternalHooksManager.init(instanceId, cli, nodeTypes);

// Send a shallow copy of allWorkflows so we still have all workflow data.
const results = await this.runTests([...allWorkflows]);
Expand Down
8 changes: 5 additions & 3 deletions packages/cli/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { createReadStream, createWriteStream, existsSync } from 'fs';
import localtunnel from 'localtunnel';
import { BinaryDataManager, TUNNEL_SUBDOMAIN_ENV, UserSettings } from 'n8n-core';
import { Command, flags } from '@oclif/command';
import Redis from 'ioredis';
import stream from 'stream';
import replaceStream from 'replacestream';
import { promisify } from 'util';
Expand Down Expand Up @@ -225,7 +224,7 @@ export class Start extends Command {
LoggerProxy.init(logger);
logger.info('Initializing n8n process');

initErrorHandling();
await initErrorHandling();
await CrashJournal.init();

// eslint-disable-next-line @typescript-eslint/no-shadow
Expand Down Expand Up @@ -394,6 +393,9 @@ export class Start extends Command {
settings.db = redisDB;
}

// eslint-disable-next-line @typescript-eslint/naming-convention
const { default: Redis } = await import('ioredis');

// This connection is going to be our heartbeat
// IORedis automatically pings redis and tries to reconnect
// We will be using the retryStrategy above
Expand Down Expand Up @@ -466,7 +468,7 @@ export class Start extends Command {

const instanceId = await UserSettings.getInstanceId();
const { cli } = await GenericHelpers.getVersions();
InternalHooksManager.init(instanceId, cli, nodeTypes);
await InternalHooksManager.init(instanceId, cli, nodeTypes);

const binaryDataConfig = config.getEnv('binaryDataManager');
await BinaryDataManager.init(binaryDataConfig, true);
Expand Down
8 changes: 5 additions & 3 deletions packages/cli/src/commands/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
/* eslint-disable @typescript-eslint/unbound-method */
import { BinaryDataManager, UserSettings } from 'n8n-core';
import { Command, flags } from '@oclif/command';
import Redis from 'ioredis';

import { IDataObject, LoggerProxy, sleep } from 'n8n-workflow';
import config from '@/config';
Expand Down Expand Up @@ -93,7 +92,7 @@ export class Webhook extends Command {
process.once('SIGTERM', Webhook.stopProcess);
process.once('SIGINT', Webhook.stopProcess);

initErrorHandling();
await initErrorHandling();
await CrashJournal.init();

// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-shadow
Expand Down Expand Up @@ -153,7 +152,7 @@ export class Webhook extends Command {

const instanceId = await UserSettings.getInstanceId();
const { cli } = await GenericHelpers.getVersions();
InternalHooksManager.init(instanceId, cli, nodeTypes);
await InternalHooksManager.init(instanceId, cli, nodeTypes);

const binaryDataConfig = config.getEnv('binaryDataManager');
await BinaryDataManager.init(binaryDataConfig);
Expand Down Expand Up @@ -203,6 +202,9 @@ export class Webhook extends Command {
settings.db = redisDB;
}

// eslint-disable-next-line @typescript-eslint/naming-convention
const { default: Redis } = await import('ioredis');

// This connection is going to be our heartbeat
// IORedis automatically pings redis and tries to reconnect
// We will be using the retryStrategy above
Expand Down
5 changes: 3 additions & 2 deletions packages/cli/src/commands/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,14 +299,15 @@ export class Worker extends Command {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const redisConnectionTimeoutLimit = config.getEnv('queue.bull.redis.timeoutThreshold');

Worker.jobQueue = Queue.getInstance().getBullObjectInstance();
const queue = await Queue.getInstance();
Worker.jobQueue = queue.getBullObjectInstance();
// eslint-disable-next-line @typescript-eslint/no-floating-promises
Worker.jobQueue.process(flags.concurrency, async (job) => this.runJob(job, nodeTypes));

const versions = await GenericHelpers.getVersions();
const instanceId = await UserSettings.getInstanceId();

InternalHooksManager.init(instanceId, versions.cli, nodeTypes);
await InternalHooksManager.init(instanceId, versions.cli, nodeTypes);

const binaryDataConfig = config.getEnv('binaryDataManager');
await BinaryDataManager.init(binaryDataConfig);
Expand Down
Loading