Split Node health check code into a separate package and make it use static codegen

pull/7203/head
murgatroid99 9 years ago
parent 0d12151d29
commit 356758a7ee
  1. 1
      package.json
  2. 18
      src/node/health_check/health.js
  3. 48
      src/node/test/health_test.js
  4. 1
      templates/package.json.template
  5. 31
      templates/src/node/health_check/package.json.template

@ -59,7 +59,6 @@
"files": [ "files": [
"LICENSE", "LICENSE",
"src/node/README.md", "src/node/README.md",
"src/node/health_check",
"src/proto", "src/proto",
"etc", "etc",
"src/node/index.js", "src/node/index.js",

@ -33,14 +33,12 @@
'use strict'; 'use strict';
var grpc = require('../'); var grpc = require('grpc');
var _ = require('lodash'); var _ = require('lodash');
var health_proto = grpc.load(__dirname + var health_messages = require('./v1/health_pb');
'/../../proto/grpc/health/v1/health.proto'); var health_service = require('./v1/health_grpc_pb');
var HealthClient = health_proto.grpc.health.v1.Health;
function HealthImplementation(statusMap) { function HealthImplementation(statusMap) {
this.statusMap = _.clone(statusMap); this.statusMap = _.clone(statusMap);
@ -51,17 +49,19 @@ HealthImplementation.prototype.setStatus = function(service, status) {
}; };
HealthImplementation.prototype.check = function(call, callback){ HealthImplementation.prototype.check = function(call, callback){
var service = call.request.service; var service = call.request.getService();
var status = _.get(this.statusMap, service, null); var status = _.get(this.statusMap, service, null);
if (status === null) { if (status === null) {
callback({code:grpc.status.NOT_FOUND}); callback({code:grpc.status.NOT_FOUND});
} else { } else {
callback(null, {status: status}); var response = new health_messages.HealthCheckResponse();
response.setStatus(status);
callback(null, response);
} }
}; };
module.exports = { module.exports = {
Client: HealthClient, Client: health_service.HealthClient,
service: HealthClient.service, service: health_service.HealthService,
Implementation: HealthImplementation Implementation: HealthImplementation
}; };

@ -35,15 +35,19 @@
var assert = require('assert'); var assert = require('assert');
var health = require('../health_check/health.js'); var health = require('../health_check/health');
var health_messages = require('../health_check/v1/health_pb');
var ServingStatus = health_messages.HealthCheckResponse.ServingStatus;
var grpc = require('../'); var grpc = require('../');
describe('Health Checking', function() { describe('Health Checking', function() {
var statusMap = { var statusMap = {
'': 'SERVING', '': ServingStatus.SERVING,
'grpc.test.TestServiceNotServing': 'NOT_SERVING', 'grpc.test.TestServiceNotServing': ServingStatus.NOT_SERVING,
'grpc.test.TestServiceServing': 'SERVING' 'grpc.test.TestServiceServing': ServingStatus.SERVING
}; };
var healthServer; var healthServer;
var healthImpl; var healthImpl;
@ -51,7 +55,7 @@ describe('Health Checking', function() {
before(function() { before(function() {
healthServer = new grpc.Server(); healthServer = new grpc.Server();
healthImpl = new health.Implementation(statusMap); healthImpl = new health.Implementation(statusMap);
healthServer.addProtoService(health.service, healthImpl); healthServer.addService(health.service, healthImpl);
var port_num = healthServer.bind('0.0.0.0:0', var port_num = healthServer.bind('0.0.0.0:0',
grpc.ServerCredentials.createInsecure()); grpc.ServerCredentials.createInsecure());
healthServer.start(); healthServer.start();
@ -62,43 +66,51 @@ describe('Health Checking', function() {
healthServer.forceShutdown(); healthServer.forceShutdown();
}); });
it('should say an enabled service is SERVING', function(done) { it('should say an enabled service is SERVING', function(done) {
healthClient.check({service: ''}, function(err, response) { var request = new health_messages.HealthCheckRequest();
request.setService('');
healthClient.check(request, function(err, response) {
assert.ifError(err); assert.ifError(err);
assert.strictEqual(response.status, 'SERVING'); assert.strictEqual(response.getStatus(), ServingStatus.SERVING);
done(); done();
}); });
}); });
it('should say that a disabled service is NOT_SERVING', function(done) { it('should say that a disabled service is NOT_SERVING', function(done) {
healthClient.check({service: 'grpc.test.TestServiceNotServing'}, var request = new health_messages.HealthCheckRequest();
function(err, response) { request.setService('grpc.test.TestServiceNotServing');
healthClient.check(request, function(err, response) {
assert.ifError(err); assert.ifError(err);
assert.strictEqual(response.status, 'NOT_SERVING'); assert.strictEqual(response.getStatus(), ServingStatus.NOT_SERVING);
done(); done();
}); });
}); });
it('should say that an enabled service is SERVING', function(done) { it('should say that an enabled service is SERVING', function(done) {
healthClient.check({service: 'grpc.test.TestServiceServing'}, var request = new health_messages.HealthCheckRequest();
function(err, response) { request.setService('grpc.test.TestServiceServing');
healthClient.check(request, function(err, response) {
assert.ifError(err); assert.ifError(err);
assert.strictEqual(response.status, 'SERVING'); assert.strictEqual(response.getStatus(), ServingStatus.SERVING);
done(); done();
}); });
}); });
it('should get NOT_FOUND if the service is not registered', function(done) { it('should get NOT_FOUND if the service is not registered', function(done) {
healthClient.check({service: 'not_registered'}, function(err, response) { var request = new health_messages.HealthCheckRequest();
request.setService('not_registered');
healthClient.check(request, function(err, response) {
assert(err); assert(err);
assert.strictEqual(err.code, grpc.status.NOT_FOUND); assert.strictEqual(err.code, grpc.status.NOT_FOUND);
done(); done();
}); });
}); });
it('should get a different response if the status changes', function(done) { it('should get a different response if the status changes', function(done) {
healthClient.check({service: 'transient'}, function(err, response) { var request = new health_messages.HealthCheckRequest();
request.setService('transient');
healthClient.check(request, function(err, response) {
assert(err); assert(err);
assert.strictEqual(err.code, grpc.status.NOT_FOUND); assert.strictEqual(err.code, grpc.status.NOT_FOUND);
healthImpl.setStatus('transient', 'SERVING'); healthImpl.setStatus('transient', ServingStatus.SERVING);
healthClient.check({service: 'transient'}, function(err, response) { healthClient.check(request, function(err, response) {
assert.ifError(err); assert.ifError(err);
assert.strictEqual(response.status, 'SERVING'); assert.strictEqual(response.getStatus(), ServingStatus.SERVING);
done(); done();
}); });
}); });

@ -61,7 +61,6 @@
"files": [ "files": [
"LICENSE", "LICENSE",
"src/node/README.md", "src/node/README.md",
"src/node/health_check",
"src/proto", "src/proto",
"etc", "etc",
"src/node/index.js", "src/node/index.js",

@ -0,0 +1,31 @@
%YAML 1.2
--- |
{
"name": "grpc-health-check",
"version": "${settings.node_version}",
"author": "Google Inc.",
"description": "Health check service for use with gRPC",
"repository": {
"type": "git",
"url": "https://github.com/grpc/grpc.git"
},
"bugs": "https://github.com/grpc/grpc/issues",
"contributors": [
{
"name": "Michael Lumish",
"email": "mlumish@google.com"
}
],
"dependencies": {
"grpc": "^0.15.0",
"lodash": "^3.9.3",
"google-protobuf": "^3.0.0-alpha.5"
},
"files": {
"LICENSE",
"health.js",
"v1"
},
"main": "src/node/index.js",
"license": "BSD-3-Clause"
}
Loading…
Cancel
Save