mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-20 11:22:15 +00:00
refactor(core): Simplify main pubsub message handler (#11156)
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user