@@ -3,6 +3,7 @@ import mongoose, { Schema, model, Document, Model } from 'mongoose';
33import { IModlServer as IModlServerShared , ApiResponse , ModlServerSchema } from '@modl-gg/shared-web' ;
44import { requireAuth } from '../middleware/authMiddleware' ;
55import { discordWebhookService } from '../services/DiscordWebhookService' ;
6+ import { connectToGlobalModlDb } from '../db/connectionManager' ;
67
78type IModlServer = IModlServerShared & Document ;
89
@@ -165,8 +166,9 @@ router.get('/:id/stats', async (req: Request, res: Response) => {
165166 } ) ;
166167 }
167168
168- // Connect to the specific server's database
169- const serverDb = mongoose . connection . useDb ( server . databaseName , { useCache : true } ) ;
169+ // Connect to the specific server's database using connection manager for proper timeout settings
170+ const globalConnection = await connectToGlobalModlDb ( ) ;
171+ const serverDb = globalConnection . useDb ( server . databaseName , { useCache : true } ) ;
170172
171173 // Fetch stats from the server's database
172174 const [
@@ -460,18 +462,21 @@ router.post('/:id/reset-database', async (req: Request, res: Response) => {
460462 // Only drop the database if it exists and is configured
461463 if ( server . databaseName ) {
462464 try {
463- // Check if main connection is ready before attempting database operations
464- if ( mongoose . connection . readyState !== 1 ) {
465- console . warn ( `MongoDB connection not ready (state: ${ mongoose . connection . readyState } ). Skipping database drop for ${ server . databaseName } ` ) ;
465+ // Use connection manager to get a connection with proper timeout settings
466+ const globalConnection = await connectToGlobalModlDb ( ) ;
467+
468+ if ( globalConnection . readyState !== 1 ) {
469+ console . warn ( `Global MongoDB connection not ready (state: ${ globalConnection . readyState } ). Skipping database drop for ${ server . databaseName } ` ) ;
466470 } else {
467- const serverDb = mongoose . connection . useDb ( server . databaseName , { useCache : true } ) ;
468-
469- // Add timeout to prevent hanging
471+ // Use the global connection to access the specific server database
472+ const serverDb = globalConnection . useDb ( server . databaseName , { useCache : false } ) ;
473+
474+ // Add timeout to prevent hanging - increased to 30 seconds to match connection timeout settings
470475 const dropPromise = serverDb . dropDatabase ( ) ;
471- const timeoutPromise = new Promise ( ( _ , reject ) =>
472- setTimeout ( ( ) => reject ( new Error ( 'Database drop operation timed out after 5 seconds' ) ) , 5000 )
476+ const timeoutPromise = new Promise ( ( _ , reject ) =>
477+ setTimeout ( ( ) => reject ( new Error ( 'Database drop operation timed out after 30 seconds' ) ) , 30000 )
473478 ) ;
474-
479+
475480 await Promise . race ( [ dropPromise , timeoutPromise ] ) ;
476481 console . log ( `Database ${ server . databaseName } dropped for server ${ server . serverName } ` ) ;
477482 }
0 commit comments