Skip to content

Commit ffc2ad7

Browse files
committedDec 21, 2023
feat: add redis health indicator
1 parent 62e2971 commit ffc2ad7

File tree

5 files changed

+137
-38
lines changed

5 files changed

+137
-38
lines changed
 

‎lib/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from './redis.module';
22
export * from './redis.decorators';
33
export * from './redis.interfaces';
44
export * from './redis.utils';
5+
export * from './redis.health';

‎lib/redis.health.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Injectable } from '@nestjs/common';
2+
import { HealthIndicator, HealthIndicatorResult, HealthCheckError } from '@nestjs/terminus';
3+
import Redis from 'ioredis';
4+
5+
@Injectable()
6+
export class RedisHealthIndicator extends HealthIndicator {
7+
constructor(private readonly redis: Redis) {
8+
super();
9+
}
10+
11+
async isHealthy(key: string): Promise<HealthIndicatorResult> {
12+
try {
13+
await this.redis.ping();
14+
return this.getStatus(key, true);
15+
} catch (error) {
16+
throw new HealthCheckError('Redis check failed', this.getStatus(key, false, { message: error.message }));
17+
}
18+
}
19+
}

‎lib/redis.module.spec.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ describe('RedisModule', () => {
99
it('Instance Redis', async () => {
1010
const module: TestingModule = await Test.createTestingModule({
1111
imports: [RedisModule.forRoot({
12-
config: {
13-
host: '127.0.0.1',
14-
port: 6379,
15-
password: '123456',
16-
}
12+
config: {
13+
host: '127.0.0.1',
14+
port: 6379,
15+
password: '123456',
16+
}
1717
})],
1818
}).compile();
1919

@@ -32,7 +32,7 @@ describe('RedisModule', () => {
3232
name: '1',
3333
host: '127.0.0.1',
3434
port: 6379,
35-
password: '123456',
35+
password: '123456',
3636
}
3737
})],
3838
}).compile();
@@ -78,4 +78,4 @@ describe('RedisModule', () => {
7878

7979
await app.close();
8080
});
81-
});
81+
});

‎package.json

+4
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@
2929
"@nestjs/core": ">=6.7.0",
3030
"ioredis": ">=5.0.0"
3131
},
32+
"optionalDependencies": {
33+
"@nestjs/terminus": "10.2.0"
34+
},
3235
"devDependencies": {
3336
"@nestjs/common": "10.3.0",
3437
"@nestjs/core": "10.3.0",
38+
"@nestjs/terminus": "10.2.0",
3539
"@nestjs/testing": "10.3.0",
3640
"@types/jest": "29.5.11",
3741
"@typescript-eslint/eslint-plugin": "6.15.0",

‎pnpm-lock.yaml

+106-31
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.