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

grpc-health

v1.6.1

Published

Nest TypeScript starter repository

Downloads

29

Readme

grpc-health

A grpc health check module

Build Status

Usage example

Installation

If you are using nest js 7, then

npm i grpc-health --save

If you are still using nest js 6, then you should install the v1.3.2 for this package:

npm i [email protected] --save

Usage

In nest js application:

Full example: https://github.com/Jeff-Tian/nestjs-hero-grpc-sample-with-health-check

with nest js 7

In your app module, import HealthModule

@Module({
  imports: [HealthModule.register(grpcClientOptions as GrpcOptions)],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

In your main.ts, extend your grpc client options:

import { extendedGrpcOptions } from 'grpc-health/dist/health/health-grpc-client.options';
import { GrpcOptions } from '@nestjs/microservices';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create<NestFastifyApplication>(
    AppModule,
    new FastifyAdapter({ logger: true }),
  );

  app.connectMicroservice(
    extendedGrpcOptions(grpcClientOptions as GrpcOptions),
  );
    
  await app.startAllMicroservicesAsync();
  await app.listen(3000, '0.0.0.0');
}

bootstrap();

with nest js 6

As when creating this package the nest js doesn't support multiple root namespaces of proto files, that is to say, all your proto files should be packaged in the same root namespace such as myService.xxx: https://github.com/nestjs/nest/pull/1733

However, the standard grpc health check protocol is under package grpc.health.v1. This makes you embarrassment when you want to utilize the grpc_health_probe in kubernetes.

Before this PR get merged, you can include this package into your project to let your nest js support standard grpc health check interfaces with only a few code changes:

// main.ts
import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";
import { grpcClientOptions } from "./grpc-client.options";
import {
  extendedGrpcOptions,
} from "grpc-health/dist/health/health-grpc-client.options";
import { GrpcOptions } from "@nestjs/microservices";

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.connectMicroservice(
    extendedGrpcOptions(grpcClientOptions as GrpcOptions)
  );

  await app.startAllMicroservicesAsync();
  await app.listen(3001);
}

bootstrap();

Or if you are creating a hybrid application, the code change is as follows:

import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";
import { extendedGrpcOptions } from "grpc-health/dist/health/grpc-client.options";

async function bootstrap() {
  /**
   * Your original code might look like this:
   * const options = yourGrpcOptions;
   * Now wrap it with the extendedGrpcOptions method
   */
  const options = extendedGrpcOptions(yourGrpcOptions);
  const app = await NestFactory.create(AppModule);
  app.connectMicroservice(options);

  await app.startAllMicroservicesAsync();
  await app.listen(3001);
}

bootstrap();

Now you can add your own logic that implements the health check functionality, simply add a HealthCheckController and imports it from your Module:

// health.controller.ts
import { Controller, Get, OnModuleInit } from "@nestjs/common";
import { Client, ClientGrpc, GrpcMethod } from "@nestjs/microservices";
import { Observable } from "rxjs";
import { grpcClientOptions } from "grpc-health/dist/health/grpc-client.options";
import { grpc } from "grpc-health/src/health/interfaces/compiled";
import ServingStatus = grpc.health.v1.HealthCheckResponse.ServingStatus;
import { HealthCheckRequest, HealthCheckResponse } from "grpc-ts-health-check";

export interface HealthService {
  check(data: HealthCheckRequest.AsObject): Observable<any>;
}

@Controller()
export class HealthController implements OnModuleInit {
  @Client(grpcClientOptions)
  private readonly client: ClientGrpc;

  private healthService: HealthService;

  onModuleInit() {
    this.healthService = this.client.getService<HealthService>("Health");
  }

  @Get("/version")
  version(): Observable<any> {
    return require("../../package.json").version;
  }

  @Get()
  execute(): Observable<any> {
    return this.healthService.check({ service: "whatever" });
  }

  @GrpcMethod("Health")
  Check(data: HealthCheckRequest.AsObject): HealthCheckResponse.AsObject {
    return {
      status: ServingStatus.SERVING
    };
  }
}
// app.module.ts
import { Module } from "@nestjs/common";
import { HeroModule } from "./hero/hero.module";
import { HealthController } from "./health.controller";

@Module({
  imports: [HeroModule],
  controllers: [HealthController]
})
export class AppModule {}

All your other code remains the same!

You can also check out this repo: https://github.com/Jeff-Tian/nestjs-hero-grpc-sample-with-health-check, it includes this grpc-health package to the official hero grpc app.

Gear with health check probe in kubernetes cluster

Now you can append the following to your deployment yaml file:

  name: ...
  readinessProbe:
    exec:
      command: ["/bin/grpc_health_probe", "-addr=:5005"]
    initialDelaySeconds: 5
  livenessProbe:
    exec:
      command: ["/bin/grpc_health_probe", "-addr=:5005"]
    initialDelaySeconds: 10
  resources:...

How

The main change to the original sample code is this commit: https://github.com/Jeff-Tian/nestjs-hero-grpc-sample-with-health-check/commit/b76249ccf76d183143d55130825967d3bebe47de

Development

git clone https://github.com/Jeff-Tian/grpc-health.git
cd grpc-health
npm i
npm test

Support me

If you find this repo useful, you can buy me a coffee:

Buy me a Coffee!