small fixes and repair shit code

This commit is contained in:
2025-07-08 00:12:12 +03:00
parent 3f81046783
commit 015fe8b4b1
5 changed files with 62 additions and 21 deletions

View File

@@ -12,51 +12,90 @@ let clientPromise;
const clientOptions = { const clientOptions = {
maxPoolSize: 10, maxPoolSize: 10,
minPoolSize: 0, minPoolSize: 0,
serverSelectionTimeoutMS: 5000, serverSelectionTimeoutMS: 30000,
socketTimeoutMS: 45000, socketTimeoutMS: 45000,
connectTimeoutMS: 30000, connectTimeoutMS: 30000,
retryWrites: true, retryWrites: true,
w: 'majority', w: 'majority',
tlsAllowInvalidCertificates: true,
heartbeatFrequencyMS: 10000, serverApi: {
minHeartbeatFrequencyMS: 500, version: '1',
maxIdleTimeMS: 30000, strict: true,
waitQueueTimeoutMS: 30000 deprecationErrors: true
}
}; };
// Connection management
async function initializeClient() {
try {
client = new MongoClient(uri, clientOptions);
// Minimal essential monitoring
client.on('serverHeartbeatFailed', (error) => {
console.error('MongoDB server heartbeat failed:', error);
});
client.on('connectionPoolCleared', () => {
console.warn('MongoDB connection pool cleared');
});
await client.connect();
console.log('MongoDB connection established');
return client;
} catch (error) {
console.error('Failed to initialize MongoDB client:', error);
throw error;
}
}
if (process.env.NODE_ENV === 'development') { if (process.env.NODE_ENV === 'development') {
if (!global._mongoClientPromise) { if (!global._mongoClientPromise) {
client = new MongoClient(uri, clientOptions); global._mongoClientPromise = initializeClient();
global._mongoClientPromise = client.connect();
} }
clientPromise = global._mongoClientPromise; clientPromise = global._mongoClientPromise;
} else { } else {
client = new MongoClient(uri, clientOptions); clientPromise = initializeClient();
clientPromise = client.connect();
} }
async function getDb() { async function getDb() {
const _client = await clientPromise; try {
return _client.db(); const _client = await clientPromise;
const db = _client.db();
// Basic connection validation
if (!_client.topology || !_client.topology.isConnected()) {
console.warn('MongoDB connection lost, reconnecting...');
await _client.connect();
}
return db;
} catch (error) {
console.error('Error getting MongoDB database:', error);
throw error;
}
} }
async function closeConnection() { async function closeConnection() {
if (client) { if (client) {
await client.close(); try {
await client.close(true);
client = null;
global._mongoClientPromise = null;
console.log('MongoDB connection closed');
} catch (error) {
console.error('Error closing MongoDB connection:', error);
}
} }
} }
module.exports = { getDb, closeConnection }; // Clean up handlers
const cleanup = async () => {
process.on('SIGINT', async () => {
await closeConnection(); await closeConnection();
process.exit(0); process.exit(0);
}); };
process.on('SIGTERM', async () => { process.on('SIGTERM', cleanup);
await closeConnection(); process.on('SIGINT', cleanup);
process.exit(0);
});
process.on('uncaughtException', async (err) => { process.on('uncaughtException', async (err) => {
console.error('Uncaught Exception:', err); console.error('Uncaught Exception:', err);
@@ -69,3 +108,5 @@ process.on('unhandledRejection', async (reason) => {
await closeConnection(); await closeConnection();
process.exit(1); process.exit(1);
}); });
module.exports = { getDb, closeConnection };

0
src/models/user.js Normal file
View File

View File

View File

View File