eventstore-interconnect
Version:
Eventstore interconnector
65 lines • 3.45 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NextConnectionGuardService = void 0;
const common_1 = require("@nestjs/common");
const constants_1 = require("../../constants");
const connection_guard_constants_1 = require("../connection-guard.constants");
const logger_1 = require("../../logger");
const db_client_1 = require("@eventstore/db-client");
let NextConnectionGuardService = class NextConnectionGuardService {
constructor(logger) {
this.logger = logger;
}
async startConnectionLinkPinger(connection, connectionConfiguration) {
this.loadNextPing(connection, connectionConfiguration);
const timer = setTimeout(() => {
this.logger.error(`Connection broken on ${connectionConfiguration.connectionString}`);
process.exit(1);
}, constants_1.EVENT_WRITER_TIMEOUT_IN_MS);
this.logger.debug(`Checking connection on ${connectionConfiguration.connectionString}...`);
await this.testConnection(connection, connectionConfiguration);
clearTimeout(timer);
this.logger.debug(`Connection on ${connectionConfiguration.connectionString} is OK`);
}
loadNextPing(connection, connectionConfiguration) {
setTimeout(() => {
this.startConnectionLinkPinger(connection, connectionConfiguration);
}, connection_guard_constants_1.CONNECTION_LINK_CHECK_INTERVAL_IN_MS);
}
async testConnection(connection, connectionConfiguration, tryCount = 0) {
try {
this.logger.debug(`Checking connection on ${connectionConfiguration.connectionString}...`);
await connection.listAllPersistentSubscriptions();
}
catch (error) {
if (error instanceof db_client_1.UnavailableError && tryCount < 10) {
this.logger.log(`EventStore is unavailable. Try ${tryCount} Retrying in 500 ms...`);
await new Promise((resolve) => setTimeout(resolve, 500));
return this.testConnection(connection, connectionConfiguration, tryCount + 1);
}
else {
this.logger.error(error);
throw error;
}
}
}
};
NextConnectionGuardService = __decorate([
(0, common_1.Injectable)(),
__param(0, (0, common_1.Inject)(logger_1.LOGGER)),
__metadata("design:paramtypes", [common_1.Logger])
], NextConnectionGuardService);
exports.NextConnectionGuardService = NextConnectionGuardService;
//# sourceMappingURL=next-connection-guard.service.js.map