Skip to content

Commit

Permalink
Add readiness and liveness to microservices
Browse files Browse the repository at this point in the history
  • Loading branch information
masterj3y committed Oct 26, 2024
1 parent 8d2cf0f commit bd24a1f
Show file tree
Hide file tree
Showing 23 changed files with 260 additions and 1 deletion.
6 changes: 5 additions & 1 deletion apps/auth/src/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { ConfigModule, ConfigService } from '@nestjs/config';
import * as Joi from 'joi';
import { JwtModule } from '@nestjs/jwt';
import { JwtStrategy } from './strategies/jwt.strategy';
import { LivnessModule } from './liveness/liveness.module';
import { ReadinessModule } from './readiness/readiness.module';

@Module({
imports: [
Expand All @@ -32,8 +34,10 @@ import { JwtStrategy } from './strategies/jwt.strategy';
}),
inject: [ConfigService],
}),
LivnessModule,
ReadinessModule,
],
controllers: [AuthController],
providers: [AuthService, JwtStrategy],
})
export class AuthModule {}
export class AuthModule { }
9 changes: 9 additions & 0 deletions apps/auth/src/liveness/liveness.controller.ts
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' };
}
}
7 changes: 7 additions & 0 deletions apps/auth/src/liveness/liveness.module.ts
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 { }
16 changes: 16 additions & 0 deletions apps/auth/src/readiness/readiness.controller.ts
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' };
}
}
10 changes: 10 additions & 0 deletions apps/auth/src/readiness/readiness.module.ts
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 {}
16 changes: 16 additions & 0 deletions apps/auth/src/readiness/readiness.service.ts
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;
}
}
9 changes: 9 additions & 0 deletions apps/quotes/src/liveness/liveness.controller.ts
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' };
}
}
7 changes: 7 additions & 0 deletions apps/quotes/src/liveness/liveness.module.ts
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 {}
4 changes: 4 additions & 0 deletions apps/quotes/src/quotes.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
import { ClientsModule, Transport } from '@nestjs/microservices';
import { QuotesController } from './quotes.controller';
import { QuoteDocument, QuoteSchema } from './model/quote.schema';
import { LivenessModule } from './liveness/liveness.module';
import { ReadinessModule } from './readiness/readiness.module';

@Module({
imports: [
Expand Down Expand Up @@ -54,6 +56,8 @@ import { QuoteDocument, QuoteSchema } from './model/quote.schema';
schema: QuoteSchema,
},
]),
LivenessModule,
ReadinessModule,
],
controllers: [QuotesController],
providers: [QuotesService],
Expand Down
16 changes: 16 additions & 0 deletions apps/quotes/src/readiness/readiness.controller.ts
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' };
}
}
10 changes: 10 additions & 0 deletions apps/quotes/src/readiness/readiness.module.ts
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 {}
16 changes: 16 additions & 0 deletions apps/quotes/src/readiness/readiness.service.ts
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;
}
}
9 changes: 9 additions & 0 deletions apps/search/src/liveness/liveness.controller.ts
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' };
}
}
7 changes: 7 additions & 0 deletions apps/search/src/liveness/liveness.module.ts
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 {}
16 changes: 16 additions & 0 deletions apps/search/src/readiness/readiness.controller.ts
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' };
}
}
10 changes: 10 additions & 0 deletions apps/search/src/readiness/readiness.module.ts
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 {}
16 changes: 16 additions & 0 deletions apps/search/src/readiness/readiness.service.ts
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;
}
}
4 changes: 4 additions & 0 deletions apps/search/src/search.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import * as Joi from 'joi';
import { AUTH_SERVICE, LoggerModule } from '@app/common';
import { ElasticsearchModule } from '@nestjs/elasticsearch';
import { ClientsModule, Transport } from '@nestjs/microservices';
import { LivenessModule } from './liveness/liveness.module';
import { ReadinessModule } from './readiness/readiness.module';

@Module({
imports: [
Expand Down Expand Up @@ -38,6 +40,8 @@ import { ClientsModule, Transport } from '@nestjs/microservices';
}),
inject: [ConfigService],
}),
LivenessModule,
ReadinessModule,
],
controllers: [SearchController],
providers: [SearchService],
Expand Down
1 change: 1 addition & 0 deletions libs/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './auth';
export * from './constants';
export * from './decorators';
export * from './dto';
export * from './utils';
24 changes: 24 additions & 0 deletions libs/common/src/utils/es-readiness-checker.utils.ts
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;
}
}
}
3 changes: 3 additions & 0 deletions libs/common/src/utils/index.ts
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';
22 changes: 22 additions & 0 deletions libs/common/src/utils/mdb-readiness-checker.utils.ts
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;
}
}
}
23 changes: 23 additions & 0 deletions libs/common/src/utils/rmq-readiness-checker.utils.ts
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;
}
}
}

0 comments on commit bd24a1f

Please sign in to comment.