#Handling disconnections and errors from redis client

4 messages · Page 1 of 1 (latest)

lofty glade
#

Hi! I have searched around on how to use Redis as a db, since the current code in my app looks like hell. I have found this answer(#1007602665716469800 message) from jmcdo29 . However, it got me thinking on how can I handle the client errors and disconnections when working with this kind of provider configuration.
I noticed that exists a nestjs-redis module that use DynamicModule to handle all of this, but I didn't like that I need to register the module at the root of my app, making it global.
Will I need to create a DynamicModule or would it be possible to at least handle the disconnection from the provider config?

Here is the current code that I wrote based on the previous link:

import Redis, { RedisOptions } from 'ioredis';

import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';

const REDIS_DB_FOR_CASE_TREE = 0;

@Module({
  imports: [ConfigModule],
  providers: [
    {
      provide: 'REDIS_OPTIONS',
      inject: [ConfigService],
      useFactory: configService =>
        ({
          db: REDIS_DB_FOR_CASE_TREE,
          host: configService.get('REDIS_HOST'),
          port: configService.get('REDIS_PORT', 6379),
        } as RedisOptions),
    },
    {
      provide: 'REDIS',
      inject: ['REDIS_OPTIONS'],
      useFactory: (options: RedisOptions) => new Redis(options),
    },
  ],
  exports: ['REDIS'],
})
export class RedisModule {}

#

Checking the Lifecycle docs I could implement the OnModuleDestroy in my RedisModule, but then I would need to inject the client in itself to handle the disconnection? 🤔

lofty glade
#

Apparently configuring in this way I can at least avoid the crash of my application using the on('error') (and I can deal with the errors):

import Redis, { RedisOptions } from 'ioredis';

import { Logger, Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';

const REDIS_DB_FOR_CASE_TREE = 0;

@Module({
  imports: [ConfigModule],
  providers: [
    {
      provide: 'REDIS_OPTIONS',
      inject: [ConfigService],
      useFactory: configService =>
        ({
          db: REDIS_DB_FOR_CASE_TREE,
          host: configService.get('REDIS_HOST'),
          port: configService.get('REDIS_PORT', 6379),
          reconnectOnError: () => true,
        } as RedisOptions),
    },
    {
      provide: 'REDIS',
      inject: ['REDIS_OPTIONS'],
      useFactory: (options: RedisOptions) => {
        const logger = new Logger('RedisForCaseTree');
        let client;
        try {
          client = new Redis(options);
          client.on('error', err => {
            logger.error('Redis Client Error: ', err);
          });
        } catch (error) {
          logger.error('Failed to connect to redis!', error);
          throw Error('Failed to connect to redis!');
        }

        return client;
      },
    },
  ],
  exports: ['REDIS'],
})
export class RedisModule {}
#

Right now, I have a problem that another module using cache manager and redis store that crashes the app when it lost the connection to the redis