@heliyos/heliyos-api-core 1.0.43 → 1.0.44
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 +0 -5
- package/dist/mongoose.js +5 -110
- package/package.json +1 -1
package/dist/mongoose.d.ts
CHANGED
|
@@ -5,8 +5,6 @@ export declare class MongoConnectionManager {
|
|
|
5
5
|
private initializationPromise;
|
|
6
6
|
private isInitialized;
|
|
7
7
|
private static connectionCount;
|
|
8
|
-
private lastConnectedTimestamp;
|
|
9
|
-
private monitoringInterval;
|
|
10
8
|
private constructor();
|
|
11
9
|
private getConnectionOptions;
|
|
12
10
|
static getInstance(): MongoConnectionManager;
|
|
@@ -15,9 +13,6 @@ export declare class MongoConnectionManager {
|
|
|
15
13
|
getConnection(): mongoose.Connection;
|
|
16
14
|
logConnectionStatus(): void;
|
|
17
15
|
private checkConnectionLatency;
|
|
18
|
-
private startConnectionMonitoring;
|
|
19
|
-
private getDetailedConnectionStatus;
|
|
20
|
-
private getReadyStateText;
|
|
21
16
|
}
|
|
22
17
|
export declare const mongoInstance: MongoConnectionManager;
|
|
23
18
|
export declare const mongooseConnection: mongoose.Connection;
|
package/dist/mongoose.js
CHANGED
|
@@ -38,48 +38,21 @@ class MongoConnectionManager {
|
|
|
38
38
|
constructor() {
|
|
39
39
|
this.initializationPromise = null;
|
|
40
40
|
this.isInitialized = false;
|
|
41
|
-
|
|
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
|
-
});
|
|
41
|
+
// Setup connection listeners only once
|
|
47
42
|
mongoose_1.default.connection.on("error", (err) => {
|
|
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
|
-
});
|
|
43
|
+
logger_1.logger.error("MongoDB connection error:", err);
|
|
54
44
|
});
|
|
55
45
|
mongoose_1.default.connection.on("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
|
-
});
|
|
46
|
+
logger_1.logger.warn("MongoDB disconnected. Attempting to reconnect...");
|
|
62
47
|
});
|
|
63
48
|
mongoose_1.default.connection.on("reconnected", () => {
|
|
64
|
-
logger_1.logger.info("MongoDB reconnected"
|
|
65
|
-
reconnectAttempts: MongoConnectionManager.connectionCount,
|
|
66
|
-
downtime: this.lastConnectedTimestamp
|
|
67
|
-
? Date.now() - this.lastConnectedTimestamp
|
|
68
|
-
: "unknown",
|
|
69
|
-
});
|
|
49
|
+
logger_1.logger.info("MongoDB reconnected");
|
|
70
50
|
this.isInitialized = true;
|
|
71
|
-
this.lastConnectedTimestamp = Date.now();
|
|
72
51
|
});
|
|
73
52
|
mongoose_1.default.connection.on("connected", () => {
|
|
74
|
-
|
|
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
|
-
});
|
|
53
|
+
logger_1.logger.info("MongoDB connected");
|
|
80
54
|
this.isInitialized = true;
|
|
81
55
|
this.checkConnectionLatency();
|
|
82
|
-
this.startConnectionMonitoring();
|
|
83
56
|
});
|
|
84
57
|
// Add pool monitoring
|
|
85
58
|
mongoose_1.default.connection.on("connected", () => {
|
|
@@ -154,15 +127,10 @@ class MongoConnectionManager {
|
|
|
154
127
|
}
|
|
155
128
|
disconnect() {
|
|
156
129
|
return __awaiter(this, void 0, void 0, function* () {
|
|
157
|
-
if (this.monitoringInterval) {
|
|
158
|
-
clearInterval(this.monitoringInterval);
|
|
159
|
-
this.monitoringInterval = null;
|
|
160
|
-
}
|
|
161
130
|
if (mongoose_1.default.connection.readyState === 1) {
|
|
162
131
|
yield mongoose_1.default.disconnect();
|
|
163
132
|
this.isInitialized = false;
|
|
164
133
|
this.initializationPromise = null;
|
|
165
|
-
this.lastConnectedTimestamp = null;
|
|
166
134
|
}
|
|
167
135
|
});
|
|
168
136
|
}
|
|
@@ -196,79 +164,6 @@ class MongoConnectionManager {
|
|
|
196
164
|
}
|
|
197
165
|
});
|
|
198
166
|
}
|
|
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
|
-
}
|
|
272
167
|
}
|
|
273
168
|
exports.MongoConnectionManager = MongoConnectionManager;
|
|
274
169
|
MongoConnectionManager.connectionCount = 0; // Add connection counter
|