feat(health): enhance health check to include database and Redis status
This commit is contained in:
@@ -1,28 +1,44 @@
|
|||||||
import { Controller, Get } from "@nestjs/common";
|
import { CACHE_MANAGER } from "@nestjs/cache-manager";
|
||||||
|
import { Controller, Get, Inject } from "@nestjs/common";
|
||||||
|
import { Cache } from "cache-manager";
|
||||||
import { sql } from "drizzle-orm";
|
import { sql } from "drizzle-orm";
|
||||||
import { DatabaseService } from "./database/database.service";
|
import { DatabaseService } from "./database/database.service";
|
||||||
|
|
||||||
@Controller("health")
|
@Controller("health")
|
||||||
export class HealthController {
|
export class HealthController {
|
||||||
constructor(private readonly databaseService: DatabaseService) {}
|
constructor(
|
||||||
|
private readonly databaseService: DatabaseService,
|
||||||
|
@Inject(CACHE_MANAGER) private cacheManager: Cache,
|
||||||
|
) {}
|
||||||
|
|
||||||
@Get()
|
@Get()
|
||||||
async check() {
|
async check() {
|
||||||
|
const health: any = {
|
||||||
|
status: "ok",
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Check database connection
|
// Check database connection
|
||||||
await this.databaseService.db.execute(sql`SELECT 1`);
|
await this.databaseService.db.execute(sql`SELECT 1`);
|
||||||
return {
|
health.database = "connected";
|
||||||
status: "ok",
|
|
||||||
database: "connected",
|
|
||||||
timestamp: new Date().toISOString(),
|
|
||||||
};
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return {
|
health.status = "error";
|
||||||
status: "error",
|
health.database = "disconnected";
|
||||||
database: "disconnected",
|
health.databaseError = error.message;
|
||||||
message: error.message,
|
|
||||||
timestamp: new Date().toISOString(),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Check Redis connection via cache-manager
|
||||||
|
// We try to set a temporary key to verify the connection
|
||||||
|
await this.cacheManager.set("health-check", "ok", 1000);
|
||||||
|
health.redis = "connected";
|
||||||
|
} catch (error) {
|
||||||
|
health.status = "error";
|
||||||
|
health.redis = "disconnected";
|
||||||
|
health.redisError = error.message;
|
||||||
|
}
|
||||||
|
|
||||||
|
return health;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user