refactor(core): Simplify main pubsub message handler (#11156)

This commit is contained in:
Iván Ovejero
2024-10-11 10:31:33 +02:00
committed by GitHub
parent 4aaebfd435
commit 0820cb5ab9
21 changed files with 813 additions and 531 deletions

View File

@@ -17,8 +17,6 @@ import type { PubSub } from './pubsub.types';
export class Subscriber {
private readonly client: SingleNodeClient | MultiNodeClient;
private readonly handlers: Map<PubSub.Channel, PubSub.HandlerFn> = new Map();
// #region Lifecycle
constructor(
@@ -31,8 +29,18 @@ export class Subscriber {
this.client = this.redisClientService.createClient({ type: 'subscriber(n8n)' });
this.client.on('message', (channel: PubSub.Channel, message) => {
this.handlers.get(channel)?.(message);
const handlerFn = (msg: PubSub.Command | PubSub.WorkerResponse) => {
const eventName = 'command' in msg ? msg.command : msg.response;
this.eventService.emit(eventName, msg.payload);
};
const debouncedHandlerFn = debounce(handlerFn, 300);
this.client.on('message', (_channel: PubSub.Channel, str) => {
const msg = this.parseMessage(str);
if (!msg) return;
if (msg.debounce) debouncedHandlerFn(msg);
else handlerFn(msg);
});
}
@@ -60,49 +68,31 @@ export class Subscriber {
});
}
/** Set the message handler function for a channel. */
setMessageHandler(channel: PubSub.Channel, handlerFn: PubSub.HandlerFn) {
this.handlers.set(channel, handlerFn);
}
// #endregion
// #region Commands
setCommandMessageHandler() {
const handlerFn = (msg: PubSub.Command) => this.eventService.emit(msg.command, msg.payload);
const debouncedHandlerFn = debounce(handlerFn, 300);
this.setMessageHandler('n8n.commands', (str: string) => {
const msg = this.parseCommandMessage(str);
if (!msg) return;
if (msg.debounce) debouncedHandlerFn(msg);
else handlerFn(msg);
private parseMessage(str: string) {
const msg = jsonParse<PubSub.Command | PubSub.WorkerResponse | null>(str, {
fallbackValue: null,
});
}
private parseCommandMessage(str: string) {
const msg = jsonParse<PubSub.Command | null>(str, { fallbackValue: null });
if (!msg) {
this.logger.debug('Received invalid string via command channel', { message: str });
this.logger.debug('Received invalid string via pubsub channel', { message: str });
return null;
}
this.logger.debug('Received message via command channel', msg);
const queueModeId = config.getEnv('redis.queueModeId');
if (
'command' in msg &&
!msg.selfSend &&
(msg.senderId === queueModeId || (msg.targets && !msg.targets.includes(queueModeId)))
) {
this.logger.debug('Disregarding message - not for this instance', msg);
return null;
}
this.logger.debug('Received message via pubsub channel', msg);
return msg;
}