feat(api): health 接口添加 storage 服务监控

- StorageService 添加 healthCheck 方法
- 健康检查响应增加 storage 状态
- 更新共享类型定义

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
charilezhou
2026-01-20 10:37:33 +08:00
parent 140c268412
commit 66cce5a765
4 changed files with 27 additions and 4 deletions

View File

@@ -1,6 +1,7 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { RedisService } from './common/redis/redis.service'; import { RedisService } from './common/redis/redis.service';
import { StorageService } from './common/storage/storage.service';
import { HealthCheckResponseDto } from './health/dto/health.dto'; import { HealthCheckResponseDto } from './health/dto/health.dto';
import { PrismaService } from './prisma/prisma.service'; import { PrismaService } from './prisma/prisma.service';
@@ -8,17 +9,19 @@ import { PrismaService } from './prisma/prisma.service';
export class AppService { export class AppService {
constructor( constructor(
private readonly prismaService: PrismaService, private readonly prismaService: PrismaService,
private readonly redisService: RedisService private readonly redisService: RedisService,
private readonly storageService: StorageService
) {} ) {}
async healthCheck(): Promise<HealthCheckResponseDto> { async healthCheck(): Promise<HealthCheckResponseDto> {
const [dbHealthy, redisHealthy] = await Promise.all([ const [dbHealthy, redisHealthy, storageHealthy] = await Promise.all([
this.prismaService.healthCheck(), this.prismaService.healthCheck(),
this.redisService.healthCheck(), this.redisService.healthCheck(),
this.storageService.healthCheck(),
]); ]);
const allHealthy = dbHealthy && redisHealthy; const allHealthy = dbHealthy && redisHealthy && storageHealthy;
const allDown = !dbHealthy && !redisHealthy; const allDown = !dbHealthy && !redisHealthy && !storageHealthy;
return { return {
status: allHealthy ? 'ok' : allDown ? 'error' : 'degraded', status: allHealthy ? 'ok' : allDown ? 'error' : 'degraded',
@@ -30,6 +33,9 @@ export class AppService {
redis: { redis: {
status: redisHealthy ? 'ok' : 'error', status: redisHealthy ? 'ok' : 'error',
}, },
storage: {
status: storageHealthy ? 'ok' : 'error',
},
}, },
}; };
} }

View File

@@ -85,4 +85,17 @@ export class StorageService implements OnModuleInit {
return url; return url;
} }
/**
* 健康检查
*/
async healthCheck(): Promise<boolean> {
try {
await this.client.bucketExists(this.bucket);
return true;
} catch (error) {
this.logger.error('Storage 健康检查失败', error);
return false;
}
}
} }

View File

@@ -22,6 +22,9 @@ export class ServicesHealthDto implements ServicesHealth {
@ApiProperty({ type: ServiceHealthDto, description: 'Redis 健康状态' }) @ApiProperty({ type: ServiceHealthDto, description: 'Redis 健康状态' })
redis: ServiceHealthDto; redis: ServiceHealthDto;
@ApiProperty({ type: ServiceHealthDto, description: '存储服务健康状态' })
storage: ServiceHealthDto;
} }
export class HealthCheckResponseDto implements HealthCheckResponse { export class HealthCheckResponseDto implements HealthCheckResponse {

View File

@@ -215,6 +215,7 @@ export interface ServiceHealth {
export interface ServicesHealth { export interface ServicesHealth {
database: ServiceHealth; database: ServiceHealth;
redis: ServiceHealth; redis: ServiceHealth;
storage: ServiceHealth;
} }
/** 健康检查响应 */ /** 健康检查响应 */