All files / src/features/health health.controller.ts

86.66% Statements 13/15
100% Branches 4/4
100% Functions 5/5
86.66% Lines 13/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46                            7x 7x 7x 7x 7x           1x 1x 1x   1x         1x 1x 1x 1x                  
import { Controller, Get } from '@nestjs/common'
import {
    HealthCheckService,
    HealthCheck,
    PrismaHealthIndicator,
    MemoryHealthIndicator,
} from '@nestjs/terminus'
import { PrismaService } from '../../database/prisma.service'
import { ConfigService } from '@nestjs/config'
import Redis from 'ioredis'
 
@Controller('health')
export class HealthController {
    constructor(
        private health: HealthCheckService,
        private prismaHealth: PrismaHealthIndicator,
        private prisma: PrismaService,
        private memory: MemoryHealthIndicator,
        private configService: ConfigService,
    ) {}
 
    @Get()
    @HealthCheck()
    check() {
        return this.health.check([
            () => this.prismaHealth.pingCheck('database', this.prisma),
            () => this.memory.checkHeap('memory_heap', 300 * 1024 * 1024),
            async () => {
                const redis = new Redis({
                    host: this.configService.get('REDIS_HOST') || 'localhost',
                    port: parseInt(this.configService.get('REDIS_PORT') || '6379'),
                    connectTimeout: 2000,
                })
                try {
                    await redis.ping()
                    redis.disconnect()
                    return { redis: { status: 'up' } }
                } catch (e) {
                    redis.disconnect()
                    return { redis: { status: 'down', message: (e as Error).message } }
                }
            },
        ])
    }
}