Add health checks to Node.js services (#26)

* Move Node healthchecks to gRPC

* gitignore proto files

* Switch to standard health RPC

* Fix lint

* Update client.js

* Add protos back + update them

* node services: fix & run genproto.sh

this gets currencyservice to work but paymentservice is still crashing
in the docker container.

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>

* Fix docker breaking

* update dockerfiles with released health probe

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
Ace Nassri 2018-09-19 12:35:22 -07:00 committed by Ahmet Alp Balkan
parent 360d983512
commit 6c37a96f3a
18 changed files with 313 additions and 109 deletions

View file

@ -12,17 +12,23 @@
// See the License for the specific language governing permissions and
// limitations under the License.
const path = require('path');
const grpc = require('grpc');
const protoLoader = require('@grpc/proto-loader');
const charge = require('./charge');
class HipsterShopServer {
constructor(protoFile, port = HipsterShopServer.DEFAULT_PORT) {
constructor (protoRoot, port = HipsterShopServer.DEFAULT_PORT) {
this.port = port;
this.packages = {
hipsterShop: this.loadProto(path.join(protoRoot, 'demo.proto')),
health: this.loadProto(path.join(protoRoot, 'grpc/health/v1/health.proto'))
};
this.server = new grpc.Server();
this.loadProto(protoFile);
this.loadAllProtos(protoRoot);
}
/**
@ -30,10 +36,10 @@ class HipsterShopServer {
* @param {*} call { ChargeRequest }
* @param {*} callback fn(err, ChargeResponse)
*/
static ChargeServiceHandler(call, callback) {
static ChargeServiceHandler (call, callback) {
try {
console.log(`PaymentService#Charge invoked with request ${JSON.stringify(call.request)}`)
const response = charge(call.request)
console.log(`PaymentService#Charge invoked with request ${JSON.stringify(call.request)}`);
const response = charge(call.request);
callback(null, response);
} catch (err) {
console.warn(err);
@ -41,13 +47,17 @@ class HipsterShopServer {
}
}
listen() {
static CheckHandler (call, callback) {
callback(null, { status: 'SERVING' });
}
listen () {
this.server.bind(`0.0.0.0:${this.port}`, grpc.ServerCredentials.createInsecure());
console.log(`PaymentService grpc server listening on ${this.port}`);
this.server.start();
}
loadProto(path) {
loadProto (path) {
const packageDefinition = protoLoader.loadSync(
path,
{
@ -55,21 +65,28 @@ class HipsterShopServer {
longs: String,
enums: String,
defaults: true,
oneofs: true,
},
oneofs: true
}
);
const protoDescriptor = grpc.loadPackageDefinition(packageDefinition);
const hipsterShopPackage = protoDescriptor.hipstershop;
this.addProtoService(hipsterShopPackage.PaymentService.service);
return grpc.loadPackageDefinition(packageDefinition);
}
addProtoService(service) {
loadAllProtos (protoRoot) {
const hipsterShopPackage = this.packages.hipsterShop.hipstershop;
const healthPackage = this.packages.health.grpc.health.v1;
this.server.addService(
service,
hipsterShopPackage.PaymentService.service,
{
charge: HipsterShopServer.ChargeServiceHandler.bind(this),
},
charge: HipsterShopServer.ChargeServiceHandler.bind(this)
}
);
this.server.addService(
healthPackage.Health.service,
{
check: HipsterShopServer.CheckHandler.bind(this)
}
);
}
}