mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 08:04:53 +00:00
health check
This commit is contained in:
@ -43,12 +43,20 @@ import { SubspaceProductAllocationEntity } from '../modules/space/entities/subsp
|
||||
import { SubspaceEntity } from '../modules/space/entities/subspace/subspace.entity';
|
||||
import { TagEntity } from '../modules/space/entities/tag.entity';
|
||||
import { ClientEntity } from '../modules/client/entities';
|
||||
import { createLogger } from 'winston';
|
||||
import { winstonLoggerOptions } from 'src/common/filters/http-exception/logger/winston.logger';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
TypeOrmModule.forRootAsync({
|
||||
imports: [ConfigModule],
|
||||
inject: [ConfigService],
|
||||
useFactory: (configService: ConfigService) => ({
|
||||
useFactory: (configService: ConfigService) => {
|
||||
const sslEnabled = JSON.parse(configService.get<string>('DB_SSL'));
|
||||
const winstonLogger = createLogger(winstonLoggerOptions);
|
||||
const typeOrmLogger = new TypeOrmWinstonLogger(winstonLogger);
|
||||
|
||||
return {
|
||||
name: 'default',
|
||||
type: 'postgres',
|
||||
host: configService.get('DB_HOST'),
|
||||
@ -56,6 +64,20 @@ import { ClientEntity } from '../modules/client/entities';
|
||||
username: configService.get('DB_USER'),
|
||||
password: configService.get('DB_PASSWORD'),
|
||||
database: configService.get('DB_NAME'),
|
||||
synchronize: Boolean(JSON.parse(configService.get('DB_SYNC'))),
|
||||
logging: ['query', 'error', 'warn', 'schema', 'migration'],
|
||||
namingStrategy: new SnakeNamingStrategy(),
|
||||
ssl: sslEnabled ? { rejectUnauthorized: false } : false,
|
||||
extra: {
|
||||
charset: 'utf8mb4',
|
||||
max: 20,
|
||||
idleTimeoutMillis: 5000,
|
||||
connectionTimeoutMillis: 11000,
|
||||
maxUses: 7500,
|
||||
...(sslEnabled && {
|
||||
ssl: { rejectUnauthorized: false }, // Required for Azure PostgreSQL
|
||||
}),
|
||||
},
|
||||
entities: [
|
||||
NewTagEntity,
|
||||
ProjectEntity,
|
||||
@ -72,7 +94,6 @@ import { ClientEntity } from '../modules/client/entities';
|
||||
SubspaceEntity,
|
||||
TagEntity,
|
||||
UserSpaceEntity,
|
||||
DeviceUserPermissionEntity,
|
||||
RoleTypeEntity,
|
||||
UserNotificationEntity,
|
||||
DeviceNotificationEntity,
|
||||
@ -96,20 +117,10 @@ import { ClientEntity } from '../modules/client/entities';
|
||||
SubspaceProductAllocationEntity,
|
||||
ClientEntity,
|
||||
],
|
||||
namingStrategy: new SnakeNamingStrategy(),
|
||||
synchronize: Boolean(JSON.parse(configService.get('DB_SYNC'))),
|
||||
logging: false,
|
||||
extra: {
|
||||
charset: 'utf8mb4',
|
||||
max: 20, // set pool max size
|
||||
idleTimeoutMillis: 5000, // close idle clients after 5 second
|
||||
connectionTimeoutMillis: 11_000, // return an error after 11 second if connection could not be established
|
||||
maxUses: 7500, // close (and replace) a connection after it has been used 7500 times (see below for discussion)
|
||||
};
|
||||
},
|
||||
continuationLocalStorage: true,
|
||||
ssl: Boolean(JSON.parse(configService.get('DB_SSL'))),
|
||||
}),
|
||||
}),
|
||||
],
|
||||
providers: [TypeOrmWinstonLogger],
|
||||
})
|
||||
export class DatabaseModule {}
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { ApiTags } from '@nestjs/swagger';
|
||||
import {
|
||||
HealthCheck,
|
||||
HealthCheckService,
|
||||
@ -8,6 +9,7 @@ import {
|
||||
HttpHealthIndicator,
|
||||
} from '@nestjs/terminus';
|
||||
|
||||
@ApiTags('Health Module')
|
||||
@Controller('health')
|
||||
export class HealthController {
|
||||
constructor(
|
||||
@ -29,7 +31,7 @@ export class HealthController {
|
||||
path: '/',
|
||||
}),
|
||||
() => this.memory.checkHeap('memory_heap', 300 * 1024 * 1024),
|
||||
() => this.http.pingCheck('tuya', 'https://openapi.tuya.com'),
|
||||
() => this.http.pingCheck('tuya', process.env.TUYA_EU_URL),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,9 +3,9 @@ import { Module } from '@nestjs/common';
|
||||
import { TerminusModule } from '@nestjs/terminus';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { HealthController } from './controllers';
|
||||
|
||||
import { HttpModule } from '@nestjs/axios';
|
||||
@Module({
|
||||
imports: [TerminusModule, TypeOrmModule],
|
||||
imports: [TerminusModule, HttpModule, TypeOrmModule],
|
||||
controllers: [HealthController],
|
||||
})
|
||||
export class HealthModule {}
|
||||
|
||||
Reference in New Issue
Block a user