cr - add redis check and logging

This commit is contained in:
Will H
2024-07-05 13:40:51 +12:00
parent 66773dfaca
commit ce6a05ab66

View File

@ -5,22 +5,33 @@ import {
HttpCode, HttpCode,
HttpStatus, HttpStatus,
InternalServerErrorException, InternalServerErrorException,
Post, Logger,
} from '@nestjs/common'; } from '@nestjs/common';
import { sql } from 'kysely'; import { sql } from 'kysely';
import { InjectKysely } from 'nestjs-kysely'; import { InjectKysely } from 'nestjs-kysely';
import { Redis } from 'ioredis';
import { EnvironmentService } from '../environment/environment.service';
@Controller() @Controller()
export class HealthController { export class HealthController {
constructor(@InjectKysely() private readonly db: KyselyDB) {} constructor(
@InjectKysely() private readonly db: KyselyDB,
private environmentService: EnvironmentService,
) {}
private readonly logger = new Logger(HealthController.name);
@Get('health') @Get('health')
@HttpCode(HttpStatus.OK) @HttpCode(HttpStatus.OK)
async health() { async health() {
try { try {
const redis = new Redis(this.environmentService.getRedisUrl());
await sql`SELECT 1=1`.execute(this.db); await sql`SELECT 1=1`.execute(this.db);
await redis.ping();
} catch (error) { } catch (error) {
throw new InternalServerErrorException('Health check failed'); this.logger.error('Health check failed');
throw new InternalServerErrorException();
} }
} }
} }