32 أسطر
1.1 KiB
TypeScript
32 أسطر
1.1 KiB
TypeScript
import { Controller, Get, Inject, ServiceUnavailableException } from '@nestjs/common';
|
|
import { InjectConnection } from '@nestjs/mongoose';
|
|
import { Connection } from 'mongoose';
|
|
import { NOTIFICATION_SERVICE_STATE, NotificationServiceState } from './service-state';
|
|
|
|
@Controller('health')
|
|
export class HealthController {
|
|
constructor(
|
|
@InjectConnection() private readonly connection: Connection,
|
|
@Inject(NOTIFICATION_SERVICE_STATE) private readonly state: NotificationServiceState,
|
|
) {}
|
|
|
|
@Get()
|
|
@Get('live')
|
|
live() {
|
|
return { status: this.state.draining ? 'draining' : 'ok', service: 'notification-service' };
|
|
}
|
|
|
|
@Get('ready')
|
|
async ready() {
|
|
const mongodbReady = this.connection.readyState === 1;
|
|
const ready = !this.state.draining && mongodbReady && this.state.rabbitmqReady;
|
|
const response = {
|
|
status: ready ? 'ready' : 'not-ready',
|
|
service: 'notification-service',
|
|
checks: { mongodb: mongodbReady, rabbitmq: this.state.rabbitmqReady },
|
|
};
|
|
if (!ready) throw new ServiceUnavailableException(response);
|
|
return response;
|
|
}
|
|
}
|