Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement cache client event listeners #6

Open
1 task done
aaroncraigongithub opened this issue Feb 16, 2023 · 3 comments
Open
1 task done

Implement cache client event listeners #6

aaroncraigongithub opened this issue Feb 16, 2023 · 3 comments

Comments

@aaroncraigongithub
Copy link

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe it

I often use console commands for long running jobs such as data migrations. I also have some workers that run in a headless Nest application.

In both instances, I occasionally have issues where the Redis cache client throws an ETIMEDOUT error. This has the effect of crashing the entire program, and no amount of try/catching seems to catch the error.

I believe the correct way to deal with this is to set up an event listener on the redis client itself, however I have not been able to find a way to get the client once it's been instantiated.

Unless I'm missing something, there is no opportunity to set up an error listener in the following example code:

    CacheModule.registerAsync<RedisClientOptions>({
      useFactory: async () => {
        const config = await getRedisConfig();

        return {
          ...config,
          store: redisStore,
        };
      },
    }),

Describe the solution you'd like

Being able to grab the client and set up listeners somewhere before it is used seems to be important. Ideally one could set up listeners at various touchpoints:

  • in the configuration itself
  • by exposing the client (or an .on() method) at runtime

Event listeners may be specific to the Redis client, in which case this would be a better request for that library.

Teachability, documentation, adoption, migration strategy

A possible API

Setting up listeners at config time:

    CacheModule.registerAsync<RedisClientOptions>({
      useFactory: async () => {
        const config = await getRedisConfig();

        return {
          ...config,
          store: redisStore,
          on: {
            error: globalCacheErrorHandler,
            // other event listeners
          }
        };
      },
    }),

Setting up listeners at run time

 @Injectable()
export class CacheService {
  constructor(@Inject(CACHE_MANAGER) private cacheManager: Cache) {
    this.cacheManager.on('error', this.handleCacheError);
  }

  private handleCacheError(err: Error) {
    // ....
  }
}

What is the motivation / use case for changing the behavior?

As it stands, it's impossible to react to events emitted by the underlying client.

@nishkarsh-beamery
Copy link

For listening to the events from RedisClient, once the module has been initialized, I could write a listener for it as follows (I am using cache-manager-redis-store):

export class RedisClientEventListener implements OnModuleInit, OnModuleDestroy {
  private readonly logger = new Logger(RedisClientEventListener.name);
  private readonly redisStore: RedisStore;

  constructor(@Inject(CACHE_MANAGER) readonly cacheManager: Cache) {
    this.redisStore = cacheManager.store as unknown as RedisStore;
  }

  onModuleInit(): void {
    const client = this.redisStore.getClient();

    if (!client) {
      this.logger.error('no redis client initialised');
    }

    if (client.isReady) {
      this.logger.log('redis client is connected and ready');
    }

    client.on('connect', () => {
      this.logger.log('redis client is connecting to server...');
    });

    client.on('ready', () => {
      this.logger.log('redis client is ready');
    });

    client.on('end', () => {
      this.logger.log('redis client connection closed');
    });

    client.on('error', (error: Error) => {
      this.logger.error(error, 'redis client received an error');
    });
  }

  async onModuleDestroy(): Promise<void> {
    await this.redisStore.getClient().quit();
  }
}

and then the module that imports and initializes CacheModule extends the above:

@Module({
  imports: [
    CacheModule.registerAsync(cacheModuleOptions),
  ],
  controllers: [SomeController],
})
export class SomeModule extends RedisClientEventListener {}

This works well to listen to the errors and handle those and the app doesn't crash, however, if the app is unable to connect to the client during initialisation itself, these listeners don't get a change to get registered and the app crashes. So far even I haven't found a way to avoid the app from crashing when app is unable to connect while initialising CacheModule.

@kamilmysliwiec kamilmysliwiec transferred this issue from nestjs/nest Apr 5, 2023
@Mut1aq
Copy link

Mut1aq commented Jun 17, 2023

anything new on this issue?

@diskra
Copy link

diskra commented Feb 28, 2024

For listening to the events from RedisClient, once the module has been initialized, I could write a listener for it as follows (I am using cache-manager-redis-store):

export class RedisClientEventListener implements OnModuleInit, OnModuleDestroy {
  private readonly logger = new Logger(RedisClientEventListener.name);
  private readonly redisStore: RedisStore;

  constructor(@Inject(CACHE_MANAGER) readonly cacheManager: Cache) {
    this.redisStore = cacheManager.store as unknown as RedisStore;
  }

  onModuleInit(): void {
    const client = this.redisStore.getClient();

    if (!client) {
      this.logger.error('no redis client initialised');
    }

    if (client.isReady) {
      this.logger.log('redis client is connected and ready');
    }

    client.on('connect', () => {
      this.logger.log('redis client is connecting to server...');
    });

    client.on('ready', () => {
      this.logger.log('redis client is ready');
    });

    client.on('end', () => {
      this.logger.log('redis client connection closed');
    });

    client.on('error', (error: Error) => {
      this.logger.error(error, 'redis client received an error');
    });
  }

  async onModuleDestroy(): Promise<void> {
    await this.redisStore.getClient().quit();
  }
}

and then the module that imports and initializes CacheModule extends the above:

@Module({
  imports: [
    CacheModule.registerAsync(cacheModuleOptions),
  ],
  controllers: [SomeController],
})
export class SomeModule extends RedisClientEventListener {}

This works well to listen to the errors and handle those and the app doesn't crash, however, if the app is unable to connect to the client during initialisation itself, these listeners don't get a change to get registered and the app crashes. So far even I haven't found a way to avoid the app from crashing when app is unable to connect while initialising CacheModule.

I am stuck on the exactly the same thing :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants