fixed bugs
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import { Injectable, OnModuleInit } from '@nestjs/common';
|
||||
import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
|
||||
import { createClient, RedisClientType } from 'redis';
|
||||
|
||||
@Injectable()
|
||||
export class RedisService implements OnModuleInit {
|
||||
private readonly logger = new Logger(RedisService.name);
|
||||
private client: RedisClientType;
|
||||
|
||||
async onModuleInit() {
|
||||
@@ -10,10 +11,22 @@ export class RedisService implements OnModuleInit {
|
||||
url: `redis://${process.env.REDIS_HOST}:${process.env.REDIS_PORT}`,
|
||||
});
|
||||
|
||||
this.client.on('error', (err) => console.error('Redis Error:', err));
|
||||
this.client.on('ready', () => this.logger.log('Redis is ready!'));
|
||||
this.client.on('error', (err) =>
|
||||
this.logger.error('Redis Error:', err.message || err),
|
||||
);
|
||||
await this.client.connect();
|
||||
}
|
||||
|
||||
async waitForReady(retries = 10, interval = 200): Promise<boolean> {
|
||||
for (let i = 0; i < retries; i++) {
|
||||
if (this.client?.isOpen) return true;
|
||||
await new Promise((res) => setTimeout(res, interval));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
getClient() {
|
||||
return this.client;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user