Skip to content

Commit

Permalink
feat: add more info to status endpoint #16
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeromanowicz committed Mar 26, 2024
1 parent 8bdf2f1 commit 15b9908
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 11 deletions.
12 changes: 12 additions & 0 deletions apps/scv-gateway/src/status/dto/service-status.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ export class ServiceStatusDto {
@ApiProperty()
applicationVersion: string;

@ApiProperty()
databaseConnection: 'OK' | 'DOWN';

@ApiProperty()
lastMigration?: string;

@ApiProperty()
queueConnection: 'OK' | 'DOWN';

@ApiProperty()
middlewareConnection: 'OK' | 'DOWN';

@ApiProperty()
middlewareVersion?: string;
}
31 changes: 31 additions & 0 deletions apps/scv-gateway/src/status/status.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,39 @@
import { Module } from '@nestjs/common';
import { StatusService } from './status.service';
import { StatusController } from './status.controller';
import { HttpModule } from '@nestjs/axios';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { ClientsModule, Transport } from '@nestjs/microservices';
import { MqConfig } from '../config/config.type';

@Module({
imports: [
HttpModule,
ConfigModule,
ClientsModule.registerAsync([
{
name: 'MQ_SERVICE',
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
transport: Transport.RMQ,
options: {
urls: [
`amqp://${configService.get<MqConfig>('mq').username}:${
configService.get<MqConfig>('mq').password
}@${configService.get<MqConfig>('mq').host}:${
configService.get<MqConfig>('mq').port
}`,
],
queue: configService.get<MqConfig>('mq').verificationQueue,
queueOptions: {
durable: true,
},
},
}),
inject: [ConfigService],
},
]),
],
controllers: [StatusController],
providers: [StatusService],
})
Expand Down
70 changes: 59 additions & 11 deletions apps/scv-gateway/src/status/status.service.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,74 @@
import { Injectable } from '@nestjs/common';
import { Inject, Injectable } from '@nestjs/common';
import { ServiceStatusDto } from './dto/service-status.dto';
import { DataSource } from 'typeorm';
import { HttpService } from '@nestjs/axios';
import { ConfigService } from '@nestjs/config';
import { firstValueFrom } from 'rxjs';
import { ClientProxy } from '@nestjs/microservices';

@Injectable()
export class StatusService {
constructor(private dataSource: DataSource) {}
constructor(
private dataSource: DataSource,
private readonly httpService: HttpService,
private readonly configService: ConfigService,
@Inject('MQ_SERVICE')
private readonly mqClient: ClientProxy,
) {}

async getStatus(): Promise<ServiceStatusDto> {
let lastMigration: string;
const [
[databaseConnection, lastMigration],
queueConnection,
[middlewareConnection, middlewareVersion],
] = await Promise.all([
this.getDatabaseConnection(),
this.getQueueConnection(),
this.getMiddlewareConnection(),
]);
const serviceStatusDto: ServiceStatusDto = {
applicationVersion: APPLICATION_VERSION,
lastMigration,
databaseConnection: databaseConnection ? 'OK' : 'DOWN',
queueConnection: queueConnection ? 'OK' : 'DOWN',
middlewareConnection: middlewareConnection ? 'OK' : 'DOWN',
middlewareVersion,
};

return serviceStatusDto;
}

private async getDatabaseConnection(): Promise<[boolean, string]> {
try {
const lastMigrations = await this.dataSource.query(
'SELECT name FROM migrations ORDER BY id DESC LIMIT 1',
);
lastMigration = lastMigrations[0]?.name;
} catch (e) {
lastMigration = 'unable to check';
return [true, lastMigrations[0]?.name];
} catch (error) {
return [false, 'UNABLE TO CHECK'];
}
const serviceStatusDto: ServiceStatusDto = {
applicationVersion: APPLICATION_VERSION,
lastMigration: lastMigration,
};
}

return serviceStatusDto;
private async getQueueConnection(): Promise<boolean> {
try {
await this.mqClient.connect();
await this.mqClient.close();
return true;
} catch (error) {
return false;
}
}

private async getMiddlewareConnection(): Promise<[boolean, string]> {
try {
const response = await firstValueFrom(
this.httpService.get(
`${this.configService.get('app').aeMdwUrl}/v2/status`,
),
);
return [true, response.data.mdw_version];
} catch (error) {
return [false, 'UNABLE TO CHECK'];
}
}
}

0 comments on commit 15b9908

Please sign in to comment.