@modernlock/common 1.0.27 → 1.0.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ export declare function checkElasticSearchConnection(): Promise<void>;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.checkElasticSearchConnection = void 0;
13
+ const elasticsearch_1 = require("@elastic/elasticsearch");
14
+ const logger_1 = require("../utils/logger");
15
+ const log = (0, logger_1.winstonLogger)(process.env.ELASTIC_SEARCH_URL, "reservationElasticsearchServer", "debug");
16
+ const elasticSearchClient = new elasticsearch_1.Client({
17
+ node: process.env.ELASTIC_SEARCH_URL,
18
+ // auth: {
19
+ // username: process.env.ELASTIC_USERNAME as string,
20
+ // password: process.env.ELASTIC_PASSWORD as string
21
+ // }
22
+ });
23
+ function checkElasticSearchConnection() {
24
+ return __awaiter(this, void 0, void 0, function* () {
25
+ let isConnected = false;
26
+ while (!isConnected) {
27
+ try {
28
+ const health = yield elasticSearchClient.cluster.health({});
29
+ log.info(`ReservationService elasticsearch health status - ${health.status}`);
30
+ console.log(`ReservationService elasticsearch health status - ${health.status}`);
31
+ isConnected = true;
32
+ }
33
+ catch (error) {
34
+ log.error("Connection to elasticSearch faild. Retrying....");
35
+ log.log("error", "ReservationService checkConnection() method:", error);
36
+ }
37
+ }
38
+ });
39
+ }
40
+ exports.checkElasticSearchConnection = checkElasticSearchConnection;
41
+ ;
@@ -0,0 +1,11 @@
1
+ export declare class ElasticsearchConnection {
2
+ private client;
3
+ private url;
4
+ private isConnected;
5
+ private isConnecting;
6
+ private reconnectRetries;
7
+ connect(url: string, username?: string, password?: string): Promise<void>;
8
+ connectWithRetries(url: string): Promise<void>;
9
+ private reconnect;
10
+ }
11
+ export declare const elasticsearchConnection: ElasticsearchConnection;
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.elasticsearchConnection = exports.ElasticsearchConnection = void 0;
13
+ const elasticsearch_1 = require("@elastic/elasticsearch");
14
+ const MAX_RETRIES = 5; // Maximum number of reconnection attempts
15
+ const INITIAL_DELAY_MS = 1000; //
16
+ class ElasticsearchConnection {
17
+ constructor() {
18
+ this.isConnected = false;
19
+ this.isConnecting = false;
20
+ this.reconnectRetries = 0; // Number of reconnection attempts
21
+ }
22
+ connect(url, username, password) {
23
+ return __awaiter(this, void 0, void 0, function* () {
24
+ if (this.isConnected || this.isConnecting)
25
+ return;
26
+ this.isConnecting = true;
27
+ this.client = new elasticsearch_1.Client({
28
+ node: url,
29
+ // auth: {
30
+ // username: process.env.ELASTIC_USERNAME as string,
31
+ // password: process.env.ELASTIC_PASSWORD as string
32
+ // }
33
+ });
34
+ try {
35
+ const health = yield this.client.cluster.health({});
36
+ this.url = url;
37
+ this.isConnected = true;
38
+ }
39
+ catch (error) {
40
+ console.log("Failed to connect to elastic search", error);
41
+ this.isConnecting = false;
42
+ this.reconnect();
43
+ }
44
+ });
45
+ }
46
+ ;
47
+ connectWithRetries(url) {
48
+ return __awaiter(this, void 0, void 0, function* () {
49
+ try {
50
+ yield this.connect(url);
51
+ console.log('Connected to elastic search');
52
+ }
53
+ catch (error) {
54
+ console.error('Failed to connect to elastic search:', error);
55
+ // Implement reconnection logic with exponential backoff
56
+ if (this.reconnectRetries < MAX_RETRIES) {
57
+ const delay = INITIAL_DELAY_MS * Math.pow(2, this.reconnectRetries);
58
+ console.log(`Retrying connection in ${delay} ms (attempt ${this.reconnectRetries + 1} of ${MAX_RETRIES})`);
59
+ this.reconnectRetries++;
60
+ setTimeout(() => {
61
+ this.connectWithRetries(url);
62
+ }, delay);
63
+ }
64
+ else {
65
+ console.error(`Max reconnection attempts (${MAX_RETRIES}) reached. Could not reconnect to elastic search.`);
66
+ }
67
+ }
68
+ });
69
+ }
70
+ reconnect() {
71
+ return __awaiter(this, void 0, void 0, function* () {
72
+ if (!this.isConnecting && !this.isConnected) {
73
+ console.log("Attempting to reconnect to elastic search...");
74
+ this.connectWithRetries(this.url);
75
+ }
76
+ });
77
+ }
78
+ ;
79
+ }
80
+ exports.ElasticsearchConnection = ElasticsearchConnection;
81
+ exports.elasticsearchConnection = new ElasticsearchConnection();
package/build/index.d.ts CHANGED
@@ -28,3 +28,5 @@ export * from "./utils/jwtEncryption";
28
28
  export * from "./utils/logger";
29
29
  export * from "./logger/custom-logger";
30
30
  export * from "./logger/transporterOptions";
31
+ export * from "./config/elasticsearch";
32
+ export * from "./config/elasticsearchConnection";
package/build/index.js CHANGED
@@ -44,3 +44,5 @@ __exportStar(require("./utils/jwtEncryption"), exports);
44
44
  __exportStar(require("./utils/logger"), exports);
45
45
  __exportStar(require("./logger/custom-logger"), exports);
46
46
  __exportStar(require("./logger/transporterOptions"), exports);
47
+ __exportStar(require("./config/elasticsearch"), exports);
48
+ __exportStar(require("./config/elasticsearchConnection"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@modernlock/common",
3
- "version": "1.0.27",
3
+ "version": "1.0.28",
4
4
  "main": "./build/index.js",
5
5
  "types": "./build/index.d.ts",
6
6
  "files": [