Socket
Book a DemoInstallSign in
Socket

@relab/fastify-health-check

Package Overview
Dependencies
Maintainers
0
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@relab/fastify-health-check

Fastify plugin that provides health probes and exposes corresponding endpoints.

latest
Source
npmnpm
Version
1.0.11
Version published
Maintainers
0
Created
Source

@relab/fastify-health-check

Fastify plugin that provides health probes and exposes corresponding endpoints.

Requirements

  • Node 18+
  • Fastify 4+

Installation

NPM

npm install --save @relab/fastify-health-check

PNPM

npm add @relab/fastify-health-check

Usage

import { HealthCheck, ComponentsHealthCheck, HealthChecks } from '@relab/fastify-health-check'
import { handleShutdown, onShutdown } from '@relab/graceful-shutdown'

// Just generic health check
// Returns HTTP 200 unless you throw HealthCheckError
const startup: HealthCheck = {
    url: 'startup',
    check: () => {
        // check your resources if needed
    },
}

// Health check that allows to track multiple component
// Use probe.setHealthy('<component name>') to make it healthy
// Health check returns HTTP once all components are healthy
export const ready = new ComponentsHealthCheck('ready', ['http'])

const live: HealthCheck = {
    url: 'live',
    check: () => {
        // check your resources if needed
        if (/* something wrong */) {
            throw new HealthCheckError({ database: 'Unhealthy' })
        }
        return { uptime: process.uptime() }
    },
}

const fastify = Fastify({
        logger: true,
    })
    // Now your probes available at /health/startup, /health/ready and /health/live
    .register(HealthChecks, {
        prefix: 'health',
        probes: [startup, ready, live],
    })

fastify.addHook('onListen', done => {
    ready.setHealthy('http')
    done()
})

onShutdown(async () => {
    ready.setUnhealthy('http')
})

fastify.listen({ port: 3000 })

handleShutdown()

License

Released under MIT by Sergey Zwezdin.

Keywords

node

FAQs

Package last updated on 22 Jun 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts