UNPKG

@restorecommerce/chassis-srv

Version:
64 lines 2.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Health = void 0; const grpc_client_1 = require("@restorecommerce/grpc-client"); const health_1 = require("@restorecommerce/rc-grpc-clients/dist/generated/grpc/health/v1/health"); const health_2 = require("@restorecommerce/rc-grpc-clients/dist/generated-server/grpc/health/v1/health"); const nice_grpc_1 = require("nice-grpc"); class Health { constructor(ci, opts) { this.ci = ci; this.opts = opts; if (this.opts) { if (this.opts.dependencies && this.opts.cfg && this.opts.logger) { this.endpoints = {}; const clientCfg = this.opts.cfg.get('client'); for (const dependency of this.opts.dependencies) { const dep = clientCfg?.[dependency] ?? this.opts.cfg.get(dependency); if (!dep) { throw new Error(`Dependency '${dependency}' not provided in config!`); } const channel = (0, nice_grpc_1.createChannel)(dep.address); this.endpoints[dep.endpoint ?? dependency] = (0, grpc_client_1.createClient)({ ...dep, logger: this.opts.logger }, health_1.HealthDefinition, channel); } } } } async check(request, context) { const service = (request && request.service) || 'liveness'; if (service === 'readiness') { if (this.ci.redisClient && !this.ci.redisClient.ping()) { return { status: health_2.HealthCheckResponse_ServingStatus.NOT_SERVING }; } if (this.opts) { try { if (this.endpoints) { for (const service of Object.keys(this.endpoints)) { const response = await this.endpoints[service].check({}); if ('error' in response && response.error) { this.opts.logger.warn('Readiness error from ' + service + ':', response); return { status: health_2.HealthCheckResponse_ServingStatus.NOT_SERVING }; } } } if (this.opts.readiness && !await this.opts.readiness()) { return { status: health_2.HealthCheckResponse_ServingStatus.NOT_SERVING }; } } catch (e) { return { status: health_2.HealthCheckResponse_ServingStatus.NOT_SERVING }; } } } const response = await this.ci.check({}); if (!('status' in response)) { return { status: health_2.HealthCheckResponse_ServingStatus.UNKNOWN }; } return response; } } exports.Health = Health; //# sourceMappingURL=index.js.map