refactor health module

This commit is contained in:
Philipinho
2024-07-05 18:59:26 +01:00
parent 9496ec9b57
commit 35dcd5f254
9 changed files with 261 additions and 33 deletions
@@ -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'),
]);
}
}