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

View File

@@ -0,0 +1,38 @@
import { InjectKysely } from 'nestjs-kysely';
import {
HealthCheckError,
HealthIndicator,
HealthIndicatorResult,
} from '@nestjs/terminus';
import { Injectable, Logger } from '@nestjs/common';
import { sql } from 'kysely';
import { KyselyDB } from '@docmost/db/types/kysely.types';
@Injectable()
export class PostgresHealthIndicator extends HealthIndicator {
private readonly logger = new Logger(PostgresHealthIndicator.name);
constructor(@InjectKysely() private readonly db: KyselyDB) {
super();
}
async pingCheck(key: string): Promise<HealthIndicatorResult> {
let isHealthy = false;
try {
await sql`SELECT 1=1`.execute(this.db);
isHealthy = true;
} catch (e) {
this.logger.error(JSON.stringify(e));
}
if (isHealthy) {
return this.getStatus(key, isHealthy);
} else {
throw new HealthCheckError(
`${key} is not available`,
this.getStatus(key, isHealthy),
);
}
}
}