cloud-ide-model-schema 1.1.133 → 1.1.137
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/lib/config/database.d.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import 'dotenv/config';
|
|
2
|
+
/**
|
|
3
|
+
* Connect to MongoDB with robust error handling and retry logic
|
|
4
|
+
* This function is idempotent - safe to call multiple times
|
|
5
|
+
*/
|
|
2
6
|
declare function connectDB(): Promise<void>;
|
|
3
7
|
declare const customUUID: () => string;
|
|
4
8
|
export { connectDB, customUUID };
|
package/lib/config/database.js
CHANGED
|
@@ -40,31 +40,179 @@ exports.customUUID = void 0;
|
|
|
40
40
|
exports.connectDB = connectDB;
|
|
41
41
|
var mongoose = require("mongoose");
|
|
42
42
|
require("dotenv/config");
|
|
43
|
+
// Connection state tracking
|
|
44
|
+
var isConnecting = false;
|
|
45
|
+
var connectionRetryCount = 0;
|
|
46
|
+
var MAX_RETRY_ATTEMPTS = 10;
|
|
47
|
+
var INITIAL_RETRY_DELAY = 1000; // 1 second
|
|
48
|
+
var MAX_RETRY_DELAY = 30000; // 30 seconds
|
|
49
|
+
var retryTimeout = null;
|
|
50
|
+
/**
|
|
51
|
+
* Check if MongoDB is already connected
|
|
52
|
+
*/
|
|
53
|
+
function isConnected() {
|
|
54
|
+
return mongoose.connection.readyState === 1; // 1 = connected
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Check if MongoDB is connecting
|
|
58
|
+
*/
|
|
59
|
+
function isConnectingState() {
|
|
60
|
+
return mongoose.connection.readyState === 2; // 2 = connecting
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Setup MongoDB connection event listeners (idempotent)
|
|
64
|
+
*/
|
|
65
|
+
function setupConnectionListeners() {
|
|
66
|
+
// Only setup once
|
|
67
|
+
if (mongoose.connection.listeners('connected').length > 0) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
// Connection successful
|
|
71
|
+
mongoose.connection.on('connected', function () {
|
|
72
|
+
console.log('✅ MongoDB connected successfully');
|
|
73
|
+
connectionRetryCount = 0; // Reset retry count on success
|
|
74
|
+
isConnecting = false;
|
|
75
|
+
});
|
|
76
|
+
// Connection error
|
|
77
|
+
mongoose.connection.on('error', function (error) {
|
|
78
|
+
console.error('❌ MongoDB connection error:', error.message);
|
|
79
|
+
isConnecting = false;
|
|
80
|
+
// Don't throw - let retry logic handle it
|
|
81
|
+
});
|
|
82
|
+
// Connection disconnected
|
|
83
|
+
mongoose.connection.on('disconnected', function () {
|
|
84
|
+
console.warn('⚠️ MongoDB disconnected. Will attempt to reconnect...');
|
|
85
|
+
isConnecting = false;
|
|
86
|
+
// Attempt reconnection
|
|
87
|
+
attemptReconnection();
|
|
88
|
+
});
|
|
89
|
+
// Connection timeout
|
|
90
|
+
mongoose.connection.on('timeout', function () {
|
|
91
|
+
console.warn('⚠️ MongoDB connection timeout. Will attempt to reconnect...');
|
|
92
|
+
isConnecting = false;
|
|
93
|
+
// Attempt reconnection
|
|
94
|
+
attemptReconnection();
|
|
95
|
+
});
|
|
96
|
+
// Connection closed
|
|
97
|
+
mongoose.connection.on('close', function () {
|
|
98
|
+
console.warn('⚠️ MongoDB connection closed. Will attempt to reconnect...');
|
|
99
|
+
isConnecting = false;
|
|
100
|
+
// Attempt reconnection
|
|
101
|
+
attemptReconnection();
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Attempt to reconnect to MongoDB with exponential backoff
|
|
106
|
+
*/
|
|
107
|
+
function attemptReconnection() {
|
|
108
|
+
var _this = this;
|
|
109
|
+
// Don't attempt if already connecting or connected
|
|
110
|
+
if (isConnecting || isConnected()) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
// Clear any existing retry timeout
|
|
114
|
+
if (retryTimeout) {
|
|
115
|
+
clearTimeout(retryTimeout);
|
|
116
|
+
retryTimeout = null;
|
|
117
|
+
}
|
|
118
|
+
// Check retry limit
|
|
119
|
+
if (connectionRetryCount >= MAX_RETRY_ATTEMPTS) {
|
|
120
|
+
console.error("\u274C MongoDB reconnection failed after ".concat(MAX_RETRY_ATTEMPTS, " attempts. Server will continue running but database operations may fail."));
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
// Calculate exponential backoff delay
|
|
124
|
+
var delay = Math.min(INITIAL_RETRY_DELAY * Math.pow(2, connectionRetryCount), MAX_RETRY_DELAY);
|
|
125
|
+
connectionRetryCount++;
|
|
126
|
+
console.log("\uD83D\uDD04 Attempting MongoDB reconnection (".concat(connectionRetryCount, "/").concat(MAX_RETRY_ATTEMPTS, ") in ").concat(delay, "ms..."));
|
|
127
|
+
retryTimeout = setTimeout(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
128
|
+
return __generator(this, function (_a) {
|
|
129
|
+
switch (_a.label) {
|
|
130
|
+
case 0:
|
|
131
|
+
retryTimeout = null;
|
|
132
|
+
return [4 /*yield*/, connectDB()];
|
|
133
|
+
case 1:
|
|
134
|
+
_a.sent(); // Recursive call
|
|
135
|
+
return [2 /*return*/];
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
}); }, delay);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Connect to MongoDB with robust error handling and retry logic
|
|
142
|
+
* This function is idempotent - safe to call multiple times
|
|
143
|
+
*/
|
|
43
144
|
function connectDB() {
|
|
44
145
|
return __awaiter(this, void 0, void 0, function () {
|
|
45
|
-
var options;
|
|
146
|
+
var mongoUri, options, connectPromise, timeoutPromise, error_1, errorMessage;
|
|
46
147
|
return __generator(this, function (_a) {
|
|
47
148
|
switch (_a.label) {
|
|
48
149
|
case 0:
|
|
150
|
+
// If already connected, do nothing
|
|
151
|
+
if (isConnected()) {
|
|
152
|
+
return [2 /*return*/];
|
|
153
|
+
}
|
|
154
|
+
// If already connecting, wait for it
|
|
155
|
+
if (isConnecting || isConnectingState()) {
|
|
156
|
+
return [2 /*return*/];
|
|
157
|
+
}
|
|
158
|
+
// Setup event listeners (idempotent)
|
|
159
|
+
setupConnectionListeners();
|
|
160
|
+
mongoUri = process.env.MONGODB_URI;
|
|
161
|
+
if (!mongoUri || mongoUri.trim() === '') {
|
|
162
|
+
console.error('❌ MONGODB_URI is not set in environment variables');
|
|
163
|
+
// Don't throw - server should continue running
|
|
164
|
+
return [2 /*return*/];
|
|
165
|
+
}
|
|
166
|
+
isConnecting = true;
|
|
167
|
+
_a.label = 1;
|
|
168
|
+
case 1:
|
|
169
|
+
_a.trys.push([1, 3, , 4]);
|
|
49
170
|
options = {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
171
|
+
connectTimeoutMS: 15000, // Increased timeout
|
|
172
|
+
socketTimeoutMS: 45000, // Increased socket timeout
|
|
173
|
+
serverSelectionTimeoutMS: 15000, // Timeout for server selection
|
|
174
|
+
heartbeatFrequencyMS: 10000, // Heartbeat frequency
|
|
175
|
+
retryWrites: true,
|
|
176
|
+
retryReads: true,
|
|
177
|
+
// Buffer commands if connection is not ready
|
|
178
|
+
bufferCommands: true,
|
|
179
|
+
maxPoolSize: 10, // Maximum number of connections in the pool
|
|
180
|
+
minPoolSize: 2, // Minimum number of connections in the pool
|
|
54
181
|
};
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
})
|
|
60
|
-
|
|
182
|
+
connectPromise = mongoose.connect(mongoUri, options);
|
|
183
|
+
timeoutPromise = new Promise(function (_, reject) {
|
|
184
|
+
setTimeout(function () {
|
|
185
|
+
reject(new Error('Connection timeout'));
|
|
186
|
+
}, 20000); // 20 second timeout
|
|
187
|
+
});
|
|
188
|
+
return [4 /*yield*/, Promise.race([connectPromise, timeoutPromise])];
|
|
189
|
+
case 2:
|
|
61
190
|
_a.sent();
|
|
62
|
-
|
|
191
|
+
// If we get here, connection was successful
|
|
192
|
+
console.log('✅ Connected to MongoDB');
|
|
193
|
+
connectionRetryCount = 0; // Reset retry count
|
|
194
|
+
isConnecting = false;
|
|
195
|
+
return [3 /*break*/, 4];
|
|
196
|
+
case 3:
|
|
197
|
+
error_1 = _a.sent();
|
|
198
|
+
isConnecting = false;
|
|
199
|
+
errorMessage = (error_1 === null || error_1 === void 0 ? void 0 : error_1.message) || 'Unknown error';
|
|
200
|
+
console.error("\u274C MongoDB connection failed: ".concat(errorMessage));
|
|
201
|
+
// Only attempt retry if we haven't exceeded max attempts
|
|
202
|
+
if (connectionRetryCount < MAX_RETRY_ATTEMPTS) {
|
|
203
|
+
attemptReconnection();
|
|
204
|
+
}
|
|
205
|
+
else {
|
|
206
|
+
console.error("\u274C MongoDB connection failed after ".concat(MAX_RETRY_ATTEMPTS, " attempts. Server will continue running but database operations may fail."));
|
|
207
|
+
console.error('⚠️ To retry connection, restart the server or wait for automatic reconnection.');
|
|
208
|
+
}
|
|
209
|
+
return [3 /*break*/, 4];
|
|
210
|
+
case 4: return [2 /*return*/];
|
|
63
211
|
}
|
|
64
212
|
});
|
|
65
213
|
});
|
|
66
214
|
}
|
|
67
|
-
// Event
|
|
215
|
+
// Event listener for success and error
|
|
68
216
|
var customUUID = function () {
|
|
69
217
|
var _a, _b, _c;
|
|
70
218
|
var key = (_c = "".concat(process.env.UUID_PREFIX).concat((_b = (_a = (new mongoose.Types.ObjectId())) === null || _a === void 0 ? void 0 : _a.toHexString()) === null || _b === void 0 ? void 0 : _b.slice(3, 24))) === null || _c === void 0 ? void 0 : _c.toLowerCase();
|
|
@@ -32,9 +32,9 @@ var fdsk_lead_config_master = new mongoose_1.Schema({
|
|
|
32
32
|
fdlcm_form_endpoint: {
|
|
33
33
|
type: String,
|
|
34
34
|
minlength: 0,
|
|
35
|
-
maxlength:
|
|
35
|
+
maxlength: 500,
|
|
36
36
|
trim: true,
|
|
37
|
-
comment: "this is like https://cloudidesys.com/leads/admissions where admissions is the endpoint, user can define its own endpoint, on the basics of that we will identify which lead config was applicable to that"
|
|
37
|
+
comment: "this is like https://cloudidesys.com/leads/admissions where admissions is the endpoint, user can define its own endpoint, on the basics of that we will identify which lead config was applicable to that. Stores the path after 'lead/' in the public URL (e.g., fdlcm_id=xxx&syen_id=yyy)"
|
|
38
38
|
},
|
|
39
39
|
fdlcm_default_counselor_id_user: {
|
|
40
40
|
type: mongoose_1.default.Schema.Types.ObjectId,
|
package/package.json
CHANGED