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

is RedisHealthModule only working with localhost? #280

Open
PunRabbit opened this issue Jan 4, 2024 · 5 comments
Open

is RedisHealthModule only working with localhost? #280

PunRabbit opened this issue Jan 4, 2024 · 5 comments

Comments

@PunRabbit
Copy link

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
Copy link

+1

@silas-joekel
Copy link

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
Copy link
Member

juandav commented Feb 13, 2024

@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
Copy link
Member

juandav commented Feb 13, 2024

I'm open to any improvement just leave your PRs

@juandav
Copy link
Member

juandav commented Mar 1, 2024

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

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