-
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.
Add readiness and liveness to microservices
- Loading branch information
Showing
23 changed files
with
260 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
|
||
@Controller('auth/liveness') | ||
export class LivnessController { | ||
@Get() | ||
livness() { | ||
return { live: 'yes' }; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { LivnessController } from './liveness.controller'; | ||
|
||
@Module({ | ||
controllers: [LivnessController] | ||
}) | ||
export class LivnessModule { } |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Controller, Get, InternalServerErrorException } from '@nestjs/common'; | ||
import { ReadinessService } from './readiness.service'; | ||
|
||
@Controller('auth/readiness') | ||
export class ReadinessController { | ||
constructor(private readonly readinessService: ReadinessService) {} | ||
|
||
@Get() | ||
async checkReadiness() { | ||
const isReady = (await this.readinessService.checkReadiness()) === true; | ||
if (!isReady) { | ||
throw new InternalServerErrorException(); | ||
} | ||
return { ready: isReady ? 'yes' : 'no' }; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ReadinessService } from './readiness.service'; | ||
import { ReadinessController } from './readiness.controller'; | ||
import { MDBReadinessChecker, RMQReadinessChecker } from '@app/common'; | ||
|
||
@Module({ | ||
providers: [ReadinessService, RMQReadinessChecker, MDBReadinessChecker], | ||
controllers: [ReadinessController], | ||
}) | ||
export class ReadinessModule {} |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { MDBReadinessChecker, RMQReadinessChecker } from '@app/common'; | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class ReadinessService { | ||
constructor( | ||
private readonly mdbReadinessChecker: MDBReadinessChecker, | ||
private readonly rmqReadinessChecker: RMQReadinessChecker, | ||
) {} | ||
|
||
async checkReadiness() { | ||
const isMDBReady = await this.mdbReadinessChecker.check(); | ||
const isRMQReady = await this.rmqReadinessChecker.check(); | ||
return isMDBReady && isRMQReady; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
|
||
@Controller('quotes/liveness/check') | ||
export class LivenessController { | ||
@Get() | ||
livness() { | ||
return { live: 'yes' }; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { LivenessController } from './liveness.controller'; | ||
|
||
@Module({ | ||
controllers: [LivenessController] | ||
}) | ||
export class LivenessModule {} |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Controller, Get, InternalServerErrorException } from '@nestjs/common'; | ||
import { ReadinessService } from './readiness.service'; | ||
|
||
@Controller('quotes/readiness/check') | ||
export class ReadinessController { | ||
constructor(private readonly readinessService: ReadinessService) {} | ||
|
||
@Get() | ||
async checkReadiness() { | ||
const isReady = (await this.readinessService.checkReadiness()) === true; | ||
if (!isReady) { | ||
throw new InternalServerErrorException(); | ||
} | ||
return { ready: isReady ? 'yes' : 'no' }; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ReadinessService } from './readiness.service'; | ||
import { ReadinessController } from './readiness.controller'; | ||
import { MDBReadinessChecker, RMQReadinessChecker } from '@app/common'; | ||
|
||
@Module({ | ||
providers: [ReadinessService, MDBReadinessChecker, RMQReadinessChecker], | ||
controllers: [ReadinessController], | ||
}) | ||
export class ReadinessModule {} |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { MDBReadinessChecker, RMQReadinessChecker } from '@app/common'; | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class ReadinessService { | ||
constructor( | ||
private readonly mdbReadinessChecker: MDBReadinessChecker, | ||
private readonly rmqReadinessChecker: RMQReadinessChecker, | ||
) {} | ||
|
||
async checkReadiness() { | ||
const isMDBReady = await this.mdbReadinessChecker.check(); | ||
const isRMQReady = await this.rmqReadinessChecker.check(); | ||
return isMDBReady && isRMQReady; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
|
||
@Controller('search/liveness') | ||
export class LivenessController { | ||
@Get() | ||
livness() { | ||
return { live: 'yes' }; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { LivenessController } from './liveness.controller'; | ||
|
||
@Module({ | ||
controllers: [LivenessController] | ||
}) | ||
export class LivenessModule {} |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Controller, Get, InternalServerErrorException } from '@nestjs/common'; | ||
import { ReadinessService } from './readiness.service'; | ||
|
||
@Controller('search/readiness') | ||
export class ReadinessController { | ||
constructor(private readonly readinessService: ReadinessService) {} | ||
|
||
@Get() | ||
async checkReadiness() { | ||
const isReady = (await this.readinessService.checkReadiness()) === true; | ||
if (!isReady) { | ||
throw new InternalServerErrorException(); | ||
} | ||
return { ready: isReady ? 'yes' : 'no' }; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ReadinessService } from './readiness.service'; | ||
import { ReadinessController } from './readiness.controller'; | ||
import { ESReadinessChecker, RMQReadinessChecker } from '@app/common'; | ||
|
||
@Module({ | ||
controllers: [ReadinessController], | ||
providers: [ReadinessService, ESReadinessChecker, RMQReadinessChecker], | ||
}) | ||
export class ReadinessModule {} |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { ESReadinessChecker, RMQReadinessChecker } from '@app/common'; | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class ReadinessService { | ||
constructor( | ||
private readonly esReadinessChecker: ESReadinessChecker, | ||
private readonly rmqReadinessChecker: RMQReadinessChecker, | ||
) {} | ||
|
||
async checkReadiness() { | ||
const isESReady = await this.esReadinessChecker.check(); | ||
const isRMQReady = await this.rmqReadinessChecker.check(); | ||
return isESReady && isRMQReady; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Client } from '@elastic/elasticsearch'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
|
||
@Injectable() | ||
export class ESReadinessChecker { | ||
private readonly esClient: Client; | ||
|
||
constructor(configService: ConfigService) { | ||
this.esClient = new Client({ | ||
node: configService.get<string>('ELASTIC_URI'), | ||
}); | ||
} | ||
|
||
async check() { | ||
try { | ||
await this.esClient.ping(); | ||
return true; | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './es-readiness-checker.utils'; | ||
export * from './rmq-readiness-checker.utils'; | ||
export * from './mdb-readiness-checker.utils'; |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import * as mongoose from 'mongoose'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
|
||
@Injectable() | ||
export class MDBReadinessChecker { | ||
private readonly mdbUri: string; | ||
|
||
constructor(configService: ConfigService) { | ||
this.mdbUri = configService.get<string>('MONGODB_URI'); | ||
} | ||
|
||
async check() { | ||
try { | ||
await mongoose.connect(this.mdbUri); | ||
return true; | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
} catch (error) { | ||
return false; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import * as amqp from 'amqplib'; | ||
|
||
@Injectable() | ||
export class RMQReadinessChecker { | ||
private readonly rmqUri: string; | ||
|
||
constructor(configService: ConfigService) { | ||
this.rmqUri = configService.get<string>('RABBITMQ_URI'); | ||
} | ||
|
||
async check() { | ||
try { | ||
const connection = await amqp.connect(this.rmqUri); | ||
await connection.close(); | ||
return true; | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
} catch (error) { | ||
return false; | ||
} | ||
} | ||
} |