@heliyos/heliyos-api-core 1.0.42 → 1.0.43
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/dist/mongoose.d.ts +5 -0
- package/dist/mongoose.js +110 -5
- package/package.json +1 -1
package/dist/mongoose.d.ts
CHANGED
|
@@ -5,6 +5,8 @@ export declare class MongoConnectionManager {
|
|
|
5
5
|
private initializationPromise;
|
|
6
6
|
private isInitialized;
|
|
7
7
|
private static connectionCount;
|
|
8
|
+
private lastConnectedTimestamp;
|
|
9
|
+
private monitoringInterval;
|
|
8
10
|
private constructor();
|
|
9
11
|
private getConnectionOptions;
|
|
10
12
|
static getInstance(): MongoConnectionManager;
|
|
@@ -13,6 +15,9 @@ export declare class MongoConnectionManager {
|
|
|
13
15
|
getConnection(): mongoose.Connection;
|
|
14
16
|
logConnectionStatus(): void;
|
|
15
17
|
private checkConnectionLatency;
|
|
18
|
+
private startConnectionMonitoring;
|
|
19
|
+
private getDetailedConnectionStatus;
|
|
20
|
+
private getReadyStateText;
|
|
16
21
|
}
|
|
17
22
|
export declare const mongoInstance: MongoConnectionManager;
|
|
18
23
|
export declare const mongooseConnection: mongoose.Connection;
|
package/dist/mongoose.js
CHANGED
|
@@ -38,21 +38,48 @@ class MongoConnectionManager {
|
|
|
38
38
|
constructor() {
|
|
39
39
|
this.initializationPromise = null;
|
|
40
40
|
this.isInitialized = false;
|
|
41
|
-
|
|
41
|
+
this.lastConnectedTimestamp = null;
|
|
42
|
+
this.monitoringInterval = null;
|
|
43
|
+
// Add detailed connection state logging
|
|
44
|
+
mongoose_1.default.connection.on("connecting", () => {
|
|
45
|
+
logger_1.logger.info("MongoDB is attempting to connect...");
|
|
46
|
+
});
|
|
42
47
|
mongoose_1.default.connection.on("error", (err) => {
|
|
43
|
-
logger_1.logger.error("MongoDB connection error:",
|
|
48
|
+
logger_1.logger.error("MongoDB connection error:", {
|
|
49
|
+
error: err,
|
|
50
|
+
state: mongoose_1.default.connection.readyState,
|
|
51
|
+
host: mongoose_1.default.connection.host,
|
|
52
|
+
name: mongoose_1.default.connection.name,
|
|
53
|
+
});
|
|
44
54
|
});
|
|
45
55
|
mongoose_1.default.connection.on("disconnected", () => {
|
|
46
|
-
logger_1.logger.warn("MongoDB disconnected.
|
|
56
|
+
logger_1.logger.warn("MongoDB disconnected.", {
|
|
57
|
+
lastConnectedAt: this.lastConnectedTimestamp,
|
|
58
|
+
timeSinceLastConnection: this.lastConnectedTimestamp
|
|
59
|
+
? Date.now() - this.lastConnectedTimestamp
|
|
60
|
+
: "never connected",
|
|
61
|
+
});
|
|
47
62
|
});
|
|
48
63
|
mongoose_1.default.connection.on("reconnected", () => {
|
|
49
|
-
logger_1.logger.info("MongoDB reconnected"
|
|
64
|
+
logger_1.logger.info("MongoDB reconnected", {
|
|
65
|
+
reconnectAttempts: MongoConnectionManager.connectionCount,
|
|
66
|
+
downtime: this.lastConnectedTimestamp
|
|
67
|
+
? Date.now() - this.lastConnectedTimestamp
|
|
68
|
+
: "unknown",
|
|
69
|
+
});
|
|
50
70
|
this.isInitialized = true;
|
|
71
|
+
this.lastConnectedTimestamp = Date.now();
|
|
51
72
|
});
|
|
52
73
|
mongoose_1.default.connection.on("connected", () => {
|
|
53
|
-
|
|
74
|
+
this.lastConnectedTimestamp = Date.now();
|
|
75
|
+
logger_1.logger.info("MongoDB connected", {
|
|
76
|
+
host: mongoose_1.default.connection.host,
|
|
77
|
+
name: mongoose_1.default.connection.name,
|
|
78
|
+
attemptCount: MongoConnectionManager.connectionCount,
|
|
79
|
+
});
|
|
54
80
|
this.isInitialized = true;
|
|
55
81
|
this.checkConnectionLatency();
|
|
82
|
+
this.startConnectionMonitoring();
|
|
56
83
|
});
|
|
57
84
|
// Add pool monitoring
|
|
58
85
|
mongoose_1.default.connection.on("connected", () => {
|
|
@@ -127,10 +154,15 @@ class MongoConnectionManager {
|
|
|
127
154
|
}
|
|
128
155
|
disconnect() {
|
|
129
156
|
return __awaiter(this, void 0, void 0, function* () {
|
|
157
|
+
if (this.monitoringInterval) {
|
|
158
|
+
clearInterval(this.monitoringInterval);
|
|
159
|
+
this.monitoringInterval = null;
|
|
160
|
+
}
|
|
130
161
|
if (mongoose_1.default.connection.readyState === 1) {
|
|
131
162
|
yield mongoose_1.default.disconnect();
|
|
132
163
|
this.isInitialized = false;
|
|
133
164
|
this.initializationPromise = null;
|
|
165
|
+
this.lastConnectedTimestamp = null;
|
|
134
166
|
}
|
|
135
167
|
});
|
|
136
168
|
}
|
|
@@ -164,6 +196,79 @@ class MongoConnectionManager {
|
|
|
164
196
|
}
|
|
165
197
|
});
|
|
166
198
|
}
|
|
199
|
+
// Add new method for continuous connection monitoring
|
|
200
|
+
startConnectionMonitoring() {
|
|
201
|
+
if (this.monitoringInterval) {
|
|
202
|
+
clearInterval(this.monitoringInterval);
|
|
203
|
+
}
|
|
204
|
+
this.monitoringInterval = setInterval(() => __awaiter(this, void 0, void 0, function* () {
|
|
205
|
+
try {
|
|
206
|
+
const start = Date.now();
|
|
207
|
+
const status = yield this.getDetailedConnectionStatus();
|
|
208
|
+
const latency = Date.now() - start;
|
|
209
|
+
logger_1.logger.info("MongoDB Connection Health Check:", Object.assign(Object.assign({}, status), { latency, timestamp: new Date().toISOString() }));
|
|
210
|
+
if (latency > 1000) {
|
|
211
|
+
logger_1.logger.warn("MongoDB Connection Latency Alert:", {
|
|
212
|
+
latency,
|
|
213
|
+
threshold: 1000,
|
|
214
|
+
status,
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
catch (error) {
|
|
219
|
+
logger_1.logger.error("MongoDB Connection Health Check Failed:", {
|
|
220
|
+
error,
|
|
221
|
+
state: mongoose_1.default.connection.readyState,
|
|
222
|
+
timestamp: new Date().toISOString(),
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
}), 30000); // Check every 30 seconds
|
|
226
|
+
}
|
|
227
|
+
// Add method to get detailed connection status
|
|
228
|
+
getDetailedConnectionStatus() {
|
|
229
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
230
|
+
const conn = mongoose_1.default.connection;
|
|
231
|
+
try {
|
|
232
|
+
const adminDb = conn.db.admin();
|
|
233
|
+
const serverStatus = yield adminDb.serverStatus();
|
|
234
|
+
return {
|
|
235
|
+
readyState: conn.readyState,
|
|
236
|
+
readyStateText: this.getReadyStateText(conn.readyState),
|
|
237
|
+
host: conn.host,
|
|
238
|
+
port: conn.port,
|
|
239
|
+
name: conn.name,
|
|
240
|
+
connections: serverStatus.connections,
|
|
241
|
+
uptime: serverStatus.uptime,
|
|
242
|
+
lastConnectedAt: this.lastConnectedTimestamp,
|
|
243
|
+
timeSinceLastConnection: this.lastConnectedTimestamp
|
|
244
|
+
? Date.now() - this.lastConnectedTimestamp
|
|
245
|
+
: null,
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
catch (error) {
|
|
249
|
+
logger_1.logger.error("Failed to get detailed server status:", error);
|
|
250
|
+
return {
|
|
251
|
+
readyState: conn.readyState,
|
|
252
|
+
readyStateText: this.getReadyStateText(conn.readyState),
|
|
253
|
+
host: conn.host,
|
|
254
|
+
port: conn.port,
|
|
255
|
+
name: conn.name,
|
|
256
|
+
error: error.message,
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
// Helper method to convert readyState to human readable text
|
|
262
|
+
getReadyStateText(state) {
|
|
263
|
+
const states = {
|
|
264
|
+
0: "disconnected",
|
|
265
|
+
1: "connected",
|
|
266
|
+
2: "connecting",
|
|
267
|
+
3: "disconnecting",
|
|
268
|
+
99: "uninitialized",
|
|
269
|
+
};
|
|
270
|
+
return states[state] || "unknown";
|
|
271
|
+
}
|
|
167
272
|
}
|
|
168
273
|
exports.MongoConnectionManager = MongoConnectionManager;
|
|
169
274
|
MongoConnectionManager.connectionCount = 0; // Add connection counter
|