@modernlock/common 1.0.26 → 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.
- package/build/config/elasticsearch.d.ts +1 -0
- package/build/config/elasticsearch.js +41 -0
- package/build/config/elasticsearchConnection.d.ts +11 -0
- package/build/config/elasticsearchConnection.js +81 -0
- package/build/index.d.ts +4 -0
- package/build/index.js +4 -0
- package/build/logger/custom-logger.d.ts +9 -0
- package/build/logger/custom-logger.js +27 -0
- package/build/logger/transporterOptions.d.ts +6 -0
- package/build/logger/transporterOptions.js +27 -0
- package/package.json +1 -1
|
@@ -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
|
@@ -26,3 +26,7 @@ export * from "./middlewares/error-handler";
|
|
|
26
26
|
export * from "./middlewares/session";
|
|
27
27
|
export * from "./utils/jwtEncryption";
|
|
28
28
|
export * from "./utils/logger";
|
|
29
|
+
export * from "./logger/custom-logger";
|
|
30
|
+
export * from "./logger/transporterOptions";
|
|
31
|
+
export * from "./config/elasticsearch";
|
|
32
|
+
export * from "./config/elasticsearchConnection";
|
package/build/index.js
CHANGED
|
@@ -42,3 +42,7 @@ __exportStar(require("./middlewares/error-handler"), exports);
|
|
|
42
42
|
__exportStar(require("./middlewares/session"), exports);
|
|
43
43
|
__exportStar(require("./utils/jwtEncryption"), exports);
|
|
44
44
|
__exportStar(require("./utils/logger"), exports);
|
|
45
|
+
__exportStar(require("./logger/custom-logger"), exports);
|
|
46
|
+
__exportStar(require("./logger/transporterOptions"), exports);
|
|
47
|
+
__exportStar(require("./config/elasticsearch"), exports);
|
|
48
|
+
__exportStar(require("./config/elasticsearchConnection"), exports);
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.CustomLogger = void 0;
|
|
7
|
+
const winston_1 = __importDefault(require("winston"));
|
|
8
|
+
class CustomLogger {
|
|
9
|
+
constructor() {
|
|
10
|
+
this.level = "";
|
|
11
|
+
}
|
|
12
|
+
getLogger() {
|
|
13
|
+
if (!this.transporters || !this.serviceName)
|
|
14
|
+
throw new Error("Transporters, service name and level are required...");
|
|
15
|
+
if (!CustomLogger.instance) {
|
|
16
|
+
CustomLogger.instance = winston_1.default.createLogger({
|
|
17
|
+
exitOnError: false,
|
|
18
|
+
defaultMeta: { service: this.serviceName },
|
|
19
|
+
format: winston_1.default.format.combine(winston_1.default.format.timestamp(), winston_1.default.format.errors({ stack: true }), winston_1.default.format.splat(), winston_1.default.format.json()),
|
|
20
|
+
transports: this.transporters
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
return CustomLogger.instance;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
exports.CustomLogger = CustomLogger;
|
|
27
|
+
;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import winston from "winston";
|
|
2
|
+
import { ElasticsearchTransport } from "winston-elasticsearch";
|
|
3
|
+
export declare const transporterChoose: {
|
|
4
|
+
console: winston.transports.ConsoleTransportInstance;
|
|
5
|
+
estransporter: (elasticsearchNode: string) => ElasticsearchTransport;
|
|
6
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.transporterChoose = void 0;
|
|
7
|
+
const winston_1 = __importDefault(require("winston"));
|
|
8
|
+
const winston_elasticsearch_1 = require("winston-elasticsearch");
|
|
9
|
+
const esTransformer = (logData) => {
|
|
10
|
+
return (0, winston_elasticsearch_1.ElasticsearchTransformer)(logData);
|
|
11
|
+
};
|
|
12
|
+
exports.transporterChoose = {
|
|
13
|
+
console: new winston_1.default.transports.Console({
|
|
14
|
+
handleExceptions: true
|
|
15
|
+
}),
|
|
16
|
+
estransporter: (elasticsearchNode) => {
|
|
17
|
+
return new winston_elasticsearch_1.ElasticsearchTransport({
|
|
18
|
+
transformer: esTransformer,
|
|
19
|
+
clientOpts: {
|
|
20
|
+
node: elasticsearchNode,
|
|
21
|
+
maxRetries: 2,
|
|
22
|
+
requestTimeout: 10000,
|
|
23
|
+
sniffOnStart: false
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
};
|