Fix subscription cross tenant issue (#16670)

As title
This commit is contained in:
martmull
2025-12-18 15:22:17 +01:00
committed by GitHub
parent c36bb8f3a1
commit 52cf3775b3
13 changed files with 178 additions and 105 deletions
@@ -2,6 +2,7 @@ import { Injectable, type OnModuleDestroy } from '@nestjs/common';
import IORedis from 'ioredis';
import { isDefined } from 'twenty-shared/utils';
import { RedisPubSub } from 'graphql-redis-subscriptions';
import { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service';
@@ -9,6 +10,7 @@ import { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twent
export class RedisClientService implements OnModuleDestroy {
private redisClient: IORedis | null = null;
private redisQueueClient: IORedis | null = null;
private redisPubSubClient: RedisPubSub | null = null;
constructor(private readonly twentyConfigService: TwentyConfigService) {}
@@ -46,6 +48,19 @@ export class RedisClientService implements OnModuleDestroy {
return this.redisClient;
}
getPubSubClient() {
if (!this.redisPubSubClient) {
const redisClient = this.getClient();
this.redisPubSubClient = new RedisPubSub({
publisher: redisClient.duplicate(),
subscriber: redisClient.duplicate(),
});
}
return this.redisPubSubClient;
}
async onModuleDestroy() {
if (isDefined(this.redisQueueClient)) {
await this.redisQueueClient.quit();
@@ -55,5 +70,9 @@ export class RedisClientService implements OnModuleDestroy {
await this.redisClient.quit();
this.redisClient = null;
}
if (isDefined(this.redisPubSubClient)) {
await this.redisPubSubClient.close();
this.redisPubSubClient = null;
}
}
}