-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add more info to status endpoint #16
- Loading branch information
1 parent
8bdf2f1
commit 15b9908
Showing
3 changed files
with
102 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; | ||
} | ||
} | ||
} |