Skip to content

Commit

Permalink
fix: correct the use of the redis health indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
juandav committed Dec 21, 2023
1 parent 242923b commit 8f61004
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 3 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,49 @@ export class AppController {
}
```

#### How to use the Redis indicator for the Terminus library?"

```js
//health.module.ts
import { Module } from '@nestjs/common';
import { TerminusModule } from '@nestjs/terminus';
import { RedisHealthModule, RedisHealthIndicator, } from '@nestjs-modules/ioredis';

@Module({
imports: [TerminusModule, RedisHealthModule],
controllers: [HealthController],
providers: [RedisHealthIndicator]
})
export class HealthModule {}
```

```js
//health.indicator.ts
import { Controller, Get } from '@nestjs/common';
import {
HealthCheckService,
HealthCheck,
HealthCheckResult
} from '@nestjs/terminus';
import { RedisHealthIndicator } from './redis.health';

@Controller('health')
export class HealthController {
constructor(
private health: HealthCheckService,
private redis: RedisHealthIndicator,
) {}

@Get()
@HealthCheck()
check(): Promise<HealthCheckResult> {
return this.health.check([
async () => this.redis.isHealthy('redis'),
]);
}
}
```

## License

MIT
3 changes: 2 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export * from './redis.module';
export * from './redis.decorators';
export * from './redis.interfaces';
export * from './redis.utils';
export * from './redis.health';
export * from './indicator/redis-health.indicator';
export * from './indicator/redis-health.module';
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Injectable } from '@nestjs/common';
import { Injectable, Inject } from '@nestjs/common';
import { HealthIndicator, HealthIndicatorResult, HealthCheckError } from '@nestjs/terminus';
import Redis from 'ioredis';

import { REDIS_HEALTH_INDICATOR } from '../redis.constants';

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

Expand Down
15 changes: 15 additions & 0 deletions lib/indicator/redis-health.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Module } from '@nestjs/common';
import { TerminusModule } from '@nestjs/terminus';
//import { HttpModule } from '@nestjs/axios';
import { RedisHealthIndicator } from './redis-health.indicator';
import { redisHealthIndicatorProvider } from './redis-health.provider';

@Module({
imports: [TerminusModule],
providers: [
RedisHealthIndicator,
redisHealthIndicatorProvider,
],
exports: [RedisHealthIndicator]
})
export class RedisHealthModule {}
7 changes: 7 additions & 0 deletions lib/indicator/redis-health.provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Redis from 'ioredis';
import { REDIS_HEALTH_INDICATOR } from '../redis.constants';

export const redisHealthIndicatorProvider = {
provide: REDIS_HEALTH_INDICATOR,
useFactory: () => new Redis(),
};
2 changes: 2 additions & 0 deletions lib/redis.constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const REDIS_MODULE_CONNECTION = 'default';
export const REDIS_MODULE_CONNECTION_TOKEN = 'IORedisModuleConnectionToken';
export const REDIS_MODULE_OPTIONS_TOKEN = 'IORedisModuleOptionsToken';

export const REDIS_HEALTH_INDICATOR = 'redisHealthIndicator';

0 comments on commit 8f61004

Please sign in to comment.