Fix redis connection (#7956)
## Context bull-mq connection was not working as intended, the connection parameter was ignored and was falling back to localhost. This PR should fix the issue by instantiating a IORedis client following bullmq documentation https://docs.bullmq.io/guide/connections I also changed cache-storage module to use IORedis client as well to be more consistent even though it was not necessary there. We could move that instantiation to a factory class in the future. ## Test start server + worker with correct port and wrong port with cache-storage-type memory/redis and queue-type sync/bull-mq
This commit is contained in:
@ -0,0 +1,12 @@
|
||||
import { Global, Module } from '@nestjs/common';
|
||||
|
||||
import { EnvironmentModule } from 'src/engine/core-modules/environment/environment.module';
|
||||
import { RedisClientService } from 'src/engine/core-modules/redis-client/redis-client.service';
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
imports: [EnvironmentModule],
|
||||
providers: [RedisClientService],
|
||||
exports: [RedisClientService],
|
||||
})
|
||||
export class RedisClientModule {}
|
||||
@ -0,0 +1,33 @@
|
||||
import { Injectable, OnModuleDestroy } from '@nestjs/common';
|
||||
|
||||
import IORedis from 'ioredis';
|
||||
|
||||
import { EnvironmentService } from 'src/engine/core-modules/environment/environment.service';
|
||||
|
||||
@Injectable()
|
||||
export class RedisClientService implements OnModuleDestroy {
|
||||
private redisClient: IORedis | null = null;
|
||||
|
||||
constructor(private readonly environmentService: EnvironmentService) {}
|
||||
|
||||
getClient() {
|
||||
if (!this.redisClient) {
|
||||
const redisUrl = this.environmentService.get('REDIS_URL');
|
||||
|
||||
if (!redisUrl) {
|
||||
throw new Error('REDIS_URL must be defined');
|
||||
}
|
||||
|
||||
this.redisClient = new IORedis(redisUrl);
|
||||
}
|
||||
|
||||
return this.redisClient;
|
||||
}
|
||||
|
||||
async onModuleDestroy() {
|
||||
if (this.redisClient) {
|
||||
await this.redisClient.quit();
|
||||
this.redisClient = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user