feat(api): health 接口添加 storage 服务监控
- StorageService 添加 healthCheck 方法 - 健康检查响应增加 storage 状态 - 更新共享类型定义 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
import { RedisService } from './common/redis/redis.service';
|
||||
import { StorageService } from './common/storage/storage.service';
|
||||
import { HealthCheckResponseDto } from './health/dto/health.dto';
|
||||
import { PrismaService } from './prisma/prisma.service';
|
||||
|
||||
@@ -8,17 +9,19 @@ import { PrismaService } from './prisma/prisma.service';
|
||||
export class AppService {
|
||||
constructor(
|
||||
private readonly prismaService: PrismaService,
|
||||
private readonly redisService: RedisService
|
||||
private readonly redisService: RedisService,
|
||||
private readonly storageService: StorageService
|
||||
) {}
|
||||
|
||||
async healthCheck(): Promise<HealthCheckResponseDto> {
|
||||
const [dbHealthy, redisHealthy] = await Promise.all([
|
||||
const [dbHealthy, redisHealthy, storageHealthy] = await Promise.all([
|
||||
this.prismaService.healthCheck(),
|
||||
this.redisService.healthCheck(),
|
||||
this.storageService.healthCheck(),
|
||||
]);
|
||||
|
||||
const allHealthy = dbHealthy && redisHealthy;
|
||||
const allDown = !dbHealthy && !redisHealthy;
|
||||
const allHealthy = dbHealthy && redisHealthy && storageHealthy;
|
||||
const allDown = !dbHealthy && !redisHealthy && !storageHealthy;
|
||||
|
||||
return {
|
||||
status: allHealthy ? 'ok' : allDown ? 'error' : 'degraded',
|
||||
@@ -30,6 +33,9 @@ export class AppService {
|
||||
redis: {
|
||||
status: redisHealthy ? 'ok' : 'error',
|
||||
},
|
||||
storage: {
|
||||
status: storageHealthy ? 'ok' : 'error',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -85,4 +85,17 @@ export class StorageService implements OnModuleInit {
|
||||
|
||||
return url;
|
||||
}
|
||||
|
||||
/**
|
||||
* 健康检查
|
||||
*/
|
||||
async healthCheck(): Promise<boolean> {
|
||||
try {
|
||||
await this.client.bucketExists(this.bucket);
|
||||
return true;
|
||||
} catch (error) {
|
||||
this.logger.error('Storage 健康检查失败', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,9 @@ export class ServicesHealthDto implements ServicesHealth {
|
||||
|
||||
@ApiProperty({ type: ServiceHealthDto, description: 'Redis 健康状态' })
|
||||
redis: ServiceHealthDto;
|
||||
|
||||
@ApiProperty({ type: ServiceHealthDto, description: '存储服务健康状态' })
|
||||
storage: ServiceHealthDto;
|
||||
}
|
||||
|
||||
export class HealthCheckResponseDto implements HealthCheckResponse {
|
||||
|
||||
@@ -215,6 +215,7 @@ export interface ServiceHealth {
|
||||
export interface ServicesHealth {
|
||||
database: ServiceHealth;
|
||||
redis: ServiceHealth;
|
||||
storage: ServiceHealth;
|
||||
}
|
||||
|
||||
/** 健康检查响应 */
|
||||
|
||||
Reference in New Issue
Block a user