consul-resolver
Version:
A load balancer for Consul services with Redis-based metrics
42 lines (41 loc) • 1.51 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HealthCheckManager = void 0;
const utils_1 = require("@brimble/utils");
class HealthCheckManager {
constructor(consul, redis, cachePrefix, cacheTTL, cacheEnabled, debug) {
this.consul = consul;
this.redis = redis;
this.cachePrefix = cachePrefix;
this.cacheTTL = cacheTTL;
this.cacheEnabled = cacheEnabled;
this.debug = debug;
}
getHealthCacheKey(service) {
return `${this.cachePrefix}:health:${service}`;
}
async getHealthChecks(service) {
var _a, _b;
const cacheKey = this.getHealthCacheKey(service);
if (this.cacheEnabled) {
const cachedHealth = await ((_a = this.redis) === null || _a === void 0 ? void 0 : _a.get(cacheKey));
if (cachedHealth) {
return JSON.parse(cachedHealth);
}
}
try {
const healthChecks = await this.consul.health.service(service);
if (this.cacheEnabled) {
await ((_b = this.redis) === null || _b === void 0 ? void 0 : _b.set(cacheKey, JSON.stringify(healthChecks), 'EX', this.cacheTTL));
}
return healthChecks;
}
catch (error) {
if (this.debug) {
utils_1.log.error(`Error fetching health checks for ${service}:`, error);
}
return [];
}
}
}
exports.HealthCheckManager = HealthCheckManager;