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,
HttpStatus,
InternalServerErrorException,
Post,
Logger,
} from '@nestjs/common';
import { sql } from 'kysely';
import { InjectKysely } from 'nestjs-kysely';
import { Redis } from 'ioredis';
import { EnvironmentService } from '../environment/environment.service';
@Controller()
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')
@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) {
throw new InternalServerErrorException('Health check failed');
this.logger.error('Health check failed');
throw new InternalServerErrorException();
}
}
}