1.0.11 • Published 1 year ago

@relab/fastify-health-check v1.0.11

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

@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.

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago