npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@r2d2bzh/moleculer-healthcheck-middleware

v2.0.2

Published

See https://gist.github.com/icebob/c717ae22002b9ecaa4b253a67952da3a

Downloads

5,171

Readme

Health-check middleware for Moleculer

The following originally comes from a gist available here: https://gist.github.com/icebob/c717ae22002b9ecaa4b253a67952da3a

Most credits go to @icebob. This repository only provides us a way to publish, maintain and perhaps customize this middleware.

This module is available on the public NPM registry:

npm install @r2d2bzh/moleculer-healthcheck-middleware

Use it for Kubernetes liveness & readiness checks. The middleware opens a HTTP server on port 3001. To check, open the http://localhost:3001/live & http://localhost:3001/ready URLs.

Response

{
  "state": "up",
  "uptime": 7.419,
  "timestamp": 1562790370161
}

The state can be "starting" (503), "up" (200), "stopping" (503) or "down" (503).

Usage

Load with default options

// moleculer.config.js
const HealthMiddleware = require('@r2d2bzh/moleculer-healthcheck-middleware');

module.exports = {
  middlewares: [
    HealthMiddleware()
  ]
};

Load with custom options

// moleculer.config.js
const HealthMiddleware = require('@r2d2bzh/moleculer-healthcheck-middleware');

module.exports = {
  middlewares: [
    HealthMiddleware({
      port: 3333,
      readiness: {
        path: "/ready"
      },
      liveness: {
        path: "/live"
      }
    })
  ]
};

In order to check liveness and/or readiness a little deeper, you can also give the module your own checker function which takes a callback. If you give a parameter to the next callback you will tell the middlware to fail the liveness and/or readiness check. Also if you do not respond in a certain amount of time, the liveness and/or readiness check will fail.

// moleculer.config.js
const HealthMiddleware = require('@r2d2bzh/moleculer-healthcheck-middleware');

module.exports = {
  middlewares: [
    HealthMiddleware({
      liveness: {
        checkerTimeoutMs: 20000, // default value
        checker: function(next) {
          // Execute here your liveness check...
          if (ok) {
            next();
          } else {
            next('error');
          }
        },
      },
    }),
  ],
};

The checker can also be initialized through a createChecker factory which takes the broker as a parameter. Provide your own implementation of createChecker to the healthcheck middleware if you need the checker function to have access to the broker. It will be called during the started Moleculer middleware hook.

const HealthMiddleware = require('@r2d2bzh/moleculer-healthcheck-middleware');

module.exports = {
  middlewares: [
    HealthMiddleware({
      liveness: {
        createChecker: (broker) =>
          (next) => {
            if (ok) {
              broker.getLogger('healthcheck').info('Everything is fine.');
              next();
            } else {
              broker.getLogger('healthcheck').error('error');
              next('error');
            }
          }
        }
    }),
  ],
};

Usage in Kubernetes

apiVersion: apps/v1
kind: Deployment
metadata:
  name: greeter
spec:
  selector:
    matchLabels:
      app: greeter
  replicas: 1
  template:
    metadata:
      labels:
        app: greeter
    spec:
      containers:
      - name: greeter
        image: moleculer/demo:1.4.2
        livenessProbe:
          httpGet:
            path: /live
            port: 3001
        readinessProbe:
          httpGet:
            path: /ready
            port: 3001