Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions backend/src/opsce/health/decorator/public-decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { SetMetadata } from '@nestjs/common';

export const IS_PUBLIC_KEY = 'isPublic';

export const Public = () =>
SetMetadata(IS_PUBLIC_KEY, true);
55 changes: 55 additions & 0 deletions backend/src/opsce/health/healthController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@



export class healthController {
constructor(private readonly healthService: HealthService) {}

@Get()
getHealth(): string {
return this.healthService.getHealthStatus();
}
}


import {
Controller,
Get,
} from '@nestjs/common';

import {
HealthCheck,
HealthCheckService,
TypeOrmHealthIndicator,
DiskHealthIndicator,
} from '@nestjs/terminus';

import { RedisHealthIndicator } from './indicators/redis.health';
import { Public } from '../../auth/decorators/public.decorator';

@Controller('health')
export class HealthController {
constructor(
private readonly health: HealthCheckService,
private readonly db: TypeOrmHealthIndicator,
private readonly disk: DiskHealthIndicator,
private readonly redis: RedisHealthIndicator,
) {}

@Get()
@Public()
@HealthCheck()
check() {
return this.health.check([
() => this.db.pingCheck('postgres'),

() =>
this.redis.isHealthy('redis'),

() =>
this.disk.checkStorage('storage', {
path: '/',
thresholdPercent: 0.9,
}),
]);
}
}
12 changes: 12 additions & 0 deletions backend/src/opsce/health/healthModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Module } from '@nestjs/common';
import { TerminusModule } from '@nestjs/terminus';

import { HealthController } from './health.controller';
import { RedisHealthIndicator } from './indicators/redis.health';

@Module({
imports: [TerminusModule],
controllers: [HealthController],
providers: [RedisHealthIndicator],
})
export class HealthModule {}
63 changes: 63 additions & 0 deletions backend/src/opsce/health/healthSerivce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@


@Injectable()
export class HealthService {
getHealthStatus(): string {
return 'OK';
}
}


import {
Injectable,
} from '@nestjs/common';

import {
HealthIndicator,
HealthIndicatorResult,
HealthCheckError,
} from '@nestjs/terminus';

import Redis from 'ioredis';

@Injectable()
export class RedisHealthIndicator extends HealthIndicator {
private readonly redis: Redis;

constructor() {
super();

this.redis = new Redis({
host: process.env.REDIS_HOST,
port: Number(process.env.REDIS_PORT),
password: process.env.REDIS_PASSWORD,
});
}

async isHealthy(
key: string,
): Promise<HealthIndicatorResult> {
try {
const response = await this.redis.ping();

const result = this.getStatus(

Check failure on line 43 in backend/src/opsce/health/healthSerivce.ts

View workflow job for this annotation

GitHub Actions / Backend (NestJS)

'error' is defined but never used
key,
response === 'PONG',
);

if (response !== 'PONG') {
throw new HealthCheckError(
'Redis check failed',
result,
);
}

return result;
} catch (error) {
throw new HealthCheckError(
'Redis unavailable',
this.getStatus(key, false),
);
}
}
}
Loading