Skip to content

Commit 2e6bf20

Browse files
committed
feat: added service unavailable env variable and middleware
1 parent e32a989 commit 2e6bf20

File tree

7 files changed

+60
-1
lines changed

7 files changed

+60
-1
lines changed

src/common/config/env.validation.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import { CronExpression } from '@nestjs/schedule';
22
import { plainToClass, Transform } from 'class-transformer';
3-
import { IsEnum, IsNumber, IsString, IsOptional, validateSync, Min, IsArray, ArrayMinSize } from 'class-validator';
3+
import {
4+
IsEnum,
5+
IsNumber,
6+
IsString,
7+
IsOptional,
8+
validateSync,
9+
Min,
10+
IsArray,
11+
ArrayMinSize,
12+
IsBoolean,
13+
} from 'class-validator';
414
import { Environment, LogLevel, LogFormat } from './interfaces';
515

616
const toNumber =
@@ -81,6 +91,11 @@ export class EnvironmentVariables {
8191
@IsNumber()
8292
@Transform(({ value }) => Number(value))
8393
CHAIN_ID: number = null;
94+
95+
@IsOptional()
96+
@Transform(({ value }) => value === 'true')
97+
@IsBoolean()
98+
IS_SERVICE_UNAVAILABLE = false;
8499
}
85100
export const ENV_KEYS = Object.keys(new EnvironmentVariables());
86101

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { ConfigService } from '@nestjs/config';
2+
import { HttpStatus } from '@nestjs/common';
3+
import { FastifyRequest, FastifyReply } from 'fastify';
4+
import { NestFastifyApplication } from '@nestjs/platform-fastify';
5+
6+
const excludedRoutes = ['/api', '/health'];
7+
8+
export const setupServiceUnavailableMiddleware = (app: NestFastifyApplication, configService: ConfigService) => {
9+
app
10+
.getHttpAdapter()
11+
.getInstance()
12+
.addHook('onRequest', async (req: FastifyRequest, res: FastifyReply) => {
13+
if (excludedRoutes.some((route) => req.url.startsWith(route))) {
14+
return;
15+
}
16+
const isMaintenance = configService.get('IS_SERVICE_UNAVAILABLE');
17+
18+
if (isMaintenance) {
19+
res.code(HttpStatus.SERVICE_UNAVAILABLE).send({
20+
statusCode: HttpStatus.SERVICE_UNAVAILABLE,
21+
message: 'Service is temporarily unavailable. Please try again later.',
22+
});
23+
}
24+
});
25+
};

src/events/rewards/rewards.service.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ export class RewardsService {
4343
* Initializes the job
4444
*/
4545
public async initialize(): Promise<void> {
46+
if (this.configService.get('IS_SERVICE_UNAVAILABLE')) {
47+
return;
48+
}
49+
4650
await this.updateRewards();
4751

4852
// getting total rewards per frame starts from TokenRebased event because it contains

src/jobs/contract-config/contract-config.service.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ export class ContractConfigService {
3232
* Initializes the job
3333
*/
3434
public async initialize(): Promise<void> {
35+
if (this.configService.get('IS_SERVICE_UNAVAILABLE')) {
36+
return;
37+
}
38+
3539
await this.updateContractConfig();
3640

3741
const cronTime = this.configService.get('JOB_INTERVAL_CONTRACT_CONFIG');

src/jobs/queue-info/queue-info.service.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export class QueueInfoService {
3030
* Initializes the job
3131
*/
3232
public async initialize(): Promise<void> {
33+
if (this.configService.get('IS_SERVICE_UNAVAILABLE')) {
34+
return;
35+
}
36+
3337
const cronTime = this.configService.get('JOB_INTERVAL_QUEUE_INFO');
3438
this.job = new CronJob(cronTime, () => this.updateQueueInfo());
3539
await this.updateQueueInfo();

src/jobs/validators/validators.service.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ export class ValidatorsService {
4444
* Initializes the job
4545
*/
4646
public async initialize(): Promise<void> {
47+
if (this.configService.get('IS_SERVICE_UNAVAILABLE')) {
48+
return;
49+
}
50+
4751
await this.validatorsCacheService.initializeFromCache();
4852

4953
const envCronTime = this.configService.get('JOB_INTERVAL_VALIDATORS');

src/main.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { ConfigService } from 'common/config';
99
import { AppModule, APP_DESCRIPTION, APP_NAME, APP_VERSION } from 'app';
1010
import { satanizer, commonPatterns } from '@lidofinance/satanizer';
1111
import { useContainer } from 'class-validator';
12+
import { setupServiceUnavailableMiddleware } from './common/middlewares/service-unavailable.middleware';
1213

1314
async function bootstrap() {
1415
const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter({ trustProxy: true }), {
@@ -77,6 +78,8 @@ async function bootstrap() {
7778
const swaggerDocument = SwaggerModule.createDocument(app, swaggerConfig);
7879
SwaggerModule.setup(SWAGGER_URL, app, swaggerDocument);
7980

81+
setupServiceUnavailableMiddleware(app, configService);
82+
8083
// app
8184
await app.listen(appPort, '0.0.0.0');
8285
}

0 commit comments

Comments
 (0)