feat(core): Make Redis available for backend communication (#6719)

* support redis cluster

* cleanup, fix config schema

* set default prefix to bull

* initial commit

* improve logging

* improve types and refactor

* list support and refactor

* fix redis service and tests

* add comment

* add redis and cache prefix

* use injection

* lint fix

* clean schema comments

* improve naming, tests, cluster client

* merge master

* cache returns unknown instead of T

* update cache service, tests and doc

* remove console.log

* do not cache null or undefined values

* fix merge

* lint fix
This commit is contained in:
Michael Auerswald
2023-08-02 12:51:25 +02:00
committed by GitHub
parent 4ac4b850dd
commit 3cad60e918
20 changed files with 1377 additions and 322 deletions

View File

@@ -0,0 +1,46 @@
import { Service } from 'typedi';
import { LoggerProxy as Logger } from 'n8n-workflow';
import {
COMMAND_REDIS_CHANNEL,
EVENT_BUS_REDIS_CHANNEL,
WORKER_RESPONSE_REDIS_CHANNEL,
} from './RedisServiceHelper';
import { RedisServiceBaseReceiver } from './RedisServiceBaseClasses';
@Service()
export class RedisServicePubSubSubscriber extends RedisServiceBaseReceiver {
async init(): Promise<void> {
await super.init('subscriber');
this.redisClient?.on('message', (channel: string, message: string) => {
this.messageHandlers.forEach((handler: (channel: string, message: string) => void) =>
handler(channel, message),
);
});
}
async subscribe(channel: string): Promise<void> {
if (!this.redisClient) {
await this.init();
}
await this.redisClient?.subscribe(channel, (error, count: number) => {
if (error) {
Logger.error(`Error subscribing to channel ${channel}`);
} else {
Logger.debug(`Subscribed ${count.toString()} to eventlog channel`);
}
});
}
async subscribeToEventLog(): Promise<void> {
await this.subscribe(EVENT_BUS_REDIS_CHANNEL);
}
async subscribeToCommandChannel(): Promise<void> {
await this.subscribe(COMMAND_REDIS_CHANNEL);
}
async subscribeToWorkerResponseChannel(): Promise<void> {
await this.subscribe(WORKER_RESPONSE_REDIS_CHANNEL);
}
}