refactor health module
This commit is contained in:
@@ -1,37 +1,22 @@
|
||||
import { KyselyDB } from '@docmost/db/types/kysely.types';
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
HttpCode,
|
||||
HttpStatus,
|
||||
InternalServerErrorException,
|
||||
Logger,
|
||||
} from '@nestjs/common';
|
||||
import { sql } from 'kysely';
|
||||
import { InjectKysely } from 'nestjs-kysely';
|
||||
import { Redis } from 'ioredis';
|
||||
import { EnvironmentService } from '../environment/environment.service';
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { HealthCheck, HealthCheckService } from '@nestjs/terminus';
|
||||
import { PostgresHealthIndicator } from './postgres.health';
|
||||
import { RedisHealthIndicator } from './redis.health';
|
||||
|
||||
@Controller()
|
||||
@Controller('health')
|
||||
export class HealthController {
|
||||
constructor(
|
||||
@InjectKysely() private readonly db: KyselyDB,
|
||||
private environmentService: EnvironmentService,
|
||||
private health: HealthCheckService,
|
||||
private postgres: PostgresHealthIndicator,
|
||||
private redis: RedisHealthIndicator,
|
||||
) {}
|
||||
|
||||
private readonly logger = new Logger(HealthController.name);
|
||||
|
||||
@Get('health')
|
||||
@HttpCode(HttpStatus.OK)
|
||||
async health() {
|
||||
try {
|
||||
const redis = new Redis(this.environmentService.getRedisUrl());
|
||||
|
||||
await sql`SELECT 1=1`.execute(this.db);
|
||||
await redis.ping();
|
||||
} catch (error) {
|
||||
this.logger.error('Health check failed');
|
||||
throw new InternalServerErrorException();
|
||||
}
|
||||
@Get()
|
||||
@HealthCheck()
|
||||
async check() {
|
||||
return this.health.check([
|
||||
() => this.postgres.pingCheck('database'),
|
||||
() => this.redis.pingCheck('redis'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user