From 1c207206dd358c5697250188a59e7c3f1167bcc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Ovejero?= Date: Mon, 5 Feb 2024 12:54:56 +0100 Subject: [PATCH 1/3] refactor(core): Lock `webhook` process out of multi-main setup --- packages/cli/src/commands/webhook.ts | 8 +- .../webhook/handleCommandMessageWebhook.ts | 74 ++++++++++++++++++- 2 files changed, 78 insertions(+), 4 deletions(-) diff --git a/packages/cli/src/commands/webhook.ts b/packages/cli/src/commands/webhook.ts index e7184bdfe0a70..2be3031ad4e28 100644 --- a/packages/cli/src/commands/webhook.ts +++ b/packages/cli/src/commands/webhook.ts @@ -1,6 +1,6 @@ import { Container } from 'typedi'; import { Flags, type Config } from '@oclif/core'; -import { sleep } from 'n8n-workflow'; +import { ApplicationError, sleep } from 'n8n-workflow'; import config from '@/config'; import { ActiveExecutions } from '@/ActiveExecutions'; @@ -102,6 +102,12 @@ export class Webhook extends BaseCommand { } async run() { + if (config.getEnv('multiMainSetup.enabled')) { + throw new ApplicationError( + 'Webhook process cannot be started when multi-main setup is enabled.', + ); + } + await Container.get(Queue).init(); await this.server.start(); this.logger.debug(`Webhook listener ID: ${this.server.uniqueInstanceId}`); diff --git a/packages/cli/src/services/orchestration/webhook/handleCommandMessageWebhook.ts b/packages/cli/src/services/orchestration/webhook/handleCommandMessageWebhook.ts index 3be43ae835837..0c0a5c8758684 100644 --- a/packages/cli/src/services/orchestration/webhook/handleCommandMessageWebhook.ts +++ b/packages/cli/src/services/orchestration/webhook/handleCommandMessageWebhook.ts @@ -1,6 +1,74 @@ -import { handleCommandMessageMain } from '../main/handleCommandMessageMain'; +import { ExternalSecretsManager } from '@/ExternalSecrets/ExternalSecretsManager.ee'; +import { License } from '@/License'; +import { MessageEventBus } from '@/eventbus'; +import Container from 'typedi'; +import { Logger } from 'winston'; +import { messageToRedisServiceCommandObject, debounceMessageReceiver } from '../helpers'; +import config from '@/config'; export async function handleCommandMessageWebhook(messageString: string) { - // currently webhooks handle commands the same way as the main instance - return await handleCommandMessageMain(messageString); + const queueModeId = config.getEnv('redis.queueModeId'); + const isMainInstance = config.getEnv('generic.instanceType') === 'main'; + const message = messageToRedisServiceCommandObject(messageString); + const logger = Container.get(Logger); + + if (message) { + logger.debug( + `RedisCommandHandler(main): Received command message ${message.command} from ${message.senderId}`, + ); + + const selfSendingAllowed = [ + 'add-webhooks-triggers-and-pollers', + 'remove-triggers-and-pollers', + ].includes(message.command); + + if ( + !selfSendingAllowed && + (message.senderId === queueModeId || + (message.targets && !message.targets.includes(queueModeId))) + ) { + // Skipping command message because it's not for this instance + logger.debug( + `Skipping command message ${message.command} because it's not for this instance.`, + ); + return message; + } + + switch (message.command) { + case 'reloadLicense': + if (!debounceMessageReceiver(message, 500)) { + message.payload = { + result: 'debounced', + }; + return message; + } + + if (isMainInstance && !config.getEnv('multiMainSetup.enabled')) { + // at this point in time, only a single main instance is supported, thus this command _should_ never be caught currently + logger.error( + 'Received command to reload license via Redis, but this should not have happened and is not supported on the main instance yet.', + ); + return message; + } + await Container.get(License).reload(); + break; + case 'restartEventBus': + if (!debounceMessageReceiver(message, 200)) { + message.payload = { + result: 'debounced', + }; + return message; + } + await Container.get(MessageEventBus).restart(); + case 'reloadExternalSecretsProviders': + if (!debounceMessageReceiver(message, 200)) { + message.payload = { + result: 'debounced', + }; + return message; + } + await Container.get(ExternalSecretsManager).reloadAllProviders(); + break; + } + } } From 74cb07e815c583ae224102d4a4d74d70be00cbfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Ovejero?= Date: Mon, 5 Feb 2024 13:41:07 +0100 Subject: [PATCH 2/3] Fix lint --- .../orchestration/webhook/handleCommandMessageWebhook.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/cli/src/services/orchestration/webhook/handleCommandMessageWebhook.ts b/packages/cli/src/services/orchestration/webhook/handleCommandMessageWebhook.ts index 0c0a5c8758684..e10849df3441f 100644 --- a/packages/cli/src/services/orchestration/webhook/handleCommandMessageWebhook.ts +++ b/packages/cli/src/services/orchestration/webhook/handleCommandMessageWebhook.ts @@ -69,6 +69,13 @@ export async function handleCommandMessageWebhook(messageString: string) { } await Container.get(ExternalSecretsManager).reloadAllProviders(); break; + + default: + break; } + + return message; } + + return; } From 3c81d24d8acae39652039484223d6c249e0d6c1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Ovejero?= Date: Mon, 5 Feb 2024 17:48:25 +0100 Subject: [PATCH 3/3] Remove unneeded check --- .../webhook/handleCommandMessageWebhook.ts | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/packages/cli/src/services/orchestration/webhook/handleCommandMessageWebhook.ts b/packages/cli/src/services/orchestration/webhook/handleCommandMessageWebhook.ts index e10849df3441f..5456d1d63d4a9 100644 --- a/packages/cli/src/services/orchestration/webhook/handleCommandMessageWebhook.ts +++ b/packages/cli/src/services/orchestration/webhook/handleCommandMessageWebhook.ts @@ -17,15 +17,9 @@ export async function handleCommandMessageWebhook(messageString: string) { `RedisCommandHandler(main): Received command message ${message.command} from ${message.senderId}`, ); - const selfSendingAllowed = [ - 'add-webhooks-triggers-and-pollers', - 'remove-triggers-and-pollers', - ].includes(message.command); - if ( - !selfSendingAllowed && - (message.senderId === queueModeId || - (message.targets && !message.targets.includes(queueModeId))) + message.senderId === queueModeId || + (message.targets && !message.targets.includes(queueModeId)) ) { // Skipping command message because it's not for this instance logger.debug(