nest-modules / ioredis

:see_no_evil: :hear_no_evil: :speak_no_evil: A ioredis module for Nest framework (node.js)
MIT License
145 stars 31 forks source link

is RedisHealthModule only working with localhost? #280

Open PunRabbit opened 10 months ago

PunRabbit commented 10 months ago

When I use RedisModule with forRootAsync method, I can choose connection url.

but, when I add RedisHealthModule, I can't choose connection url.

it just keep trying to connect with 127.0.0.1 address.

is there any way to choose connection url for Redis health check?

Here is the code below that I use.

import { Module } from '@nestjs/common';
import { RedisHealthModule, RedisModule } from '@nestjs-modules/ioredis';
import { MongooseModule } from '@nestjs/mongoose';
import { TerminusModule } from '@nestjs/terminus';
import { ConfigModule, ConfigService } from '@nestjs/config';

import { KeyService } from '../../application/key/key.service';
import { KeyController } from '../../controller/key/key.controller';
import { KeyRepo } from '../../infra/key/key.repo';

@Module({
    imports: [
        TerminusModule,
        ConfigModule.forRoot({envFilePath: "src/core/key/.key.env"}),
        RedisModule.forRootAsync({
            useFactory: async (configService: ConfigService) => ({
                type: 'single',
                url: configService.get<string>('REDIS_URL')
            }),
            inject: [ConfigService],
            imports: [ConfigModule]
        }),
        MongooseModule.forRootAsync({
            imports: [ConfigModule],
            useFactory: async (configService: ConfigService) => ({
                uri: configService.get('MONGO_URL'),
                useNewUrlParser: true,
                useUnifiedTopology: true,
            }),
            inject: [ConfigService],
        }),
        RedisHealthModule
    ],
    controllers: [KeyController],
    providers: [
        {
            provide: 'IKeyService',
            useClass: KeyService
        },
        {
            provide: 'IKeyRepo',
            useClass: KeyRepo
        }
    ],
})
export class KeyModule {}

and, this is what I got when I run application.

스크린샷 2024-01-04 오전 11 33 51

Thanks.

Dryymoon commented 10 months ago

+1

silas-joekel commented 9 months ago

I've also run into this issue and decided to implement my own redis health indicator.

@Injectable()
export class RedisHealthIndicator extends HealthIndicator {
    constructor(@InjectRedis() private readonly redis: Redis) {
        super();
    }

    async isHealthy(key: string): Promise<HealthIndicatorResult> {
        try {
            await this.redis.ping();
            return this.getStatus(key, true);
        } catch (error) {
            throw new HealthCheckError('Redis check failed', this.getStatus(key, false, { message: error.message }));
        }
    }
}

The health indicator provided by @nest-modules/ioredis uses a custom provider for a redis health check instance which doesn't consider the configuration (see https://github.com/nest-modules/ioredis/blob/main/lib/indicator/redis-health.provider.ts#L6).

Theoretically this could be changed similar to my implementation. But I don't know what plans the lib maintainers have for the health checking feature 🤷🏻

juandav commented 8 months ago

@silas-joekel I invite you to add the solution to this repo, I could dedicate the weekend to this, but if you can add it, I would appreciate it.

juandav commented 8 months ago

I'm open to any improvement just leave your PRs

juandav commented 8 months ago

I am still working on an implementation for this, I hope I can solve this.