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

refactor(core): Lock webhook process out of multi-main setup (no-changelog) #8549

Merged
merged 3 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 7 additions & 1 deletion packages/cli/src/commands/webhook.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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}`);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,81 @@
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);

ivov marked this conversation as resolved.
Show resolved Hide resolved
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;

default:
break;
}

return message;
}

return;
}
Loading