Skip to content

Commit 12d1493

Browse files
authored
Merge pull request #6 from modl-gg/dev
Initial modl-admin release
2 parents a7d4498 + 2b3150f commit 12d1493

File tree

6 files changed

+88
-31
lines changed

6 files changed

+88
-31
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ jobs:
3838
- name: Install client dependencies
3939
run: |
4040
cd client
41-
npm ci
41+
npm i
4242
env:
4343
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4444

4545
- name: Install dependencies
46-
run: npm ci
46+
run: npm i
4747
env:
4848
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4949

5050
- name: Run build
51-
run: npm run build
51+
run: npm run build

.github/workflows/deploy-staging.yml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,10 @@ jobs:
3333
cd /home/modl/modl-admin
3434
git stash
3535
git pull origin dev
36-
if git diff --name-only HEAD@{1} HEAD | grep -q "^package.*\.json$"; then
37-
npm ci
38-
fi
39-
if git diff --name-only HEAD@{1} HEAD | grep -q "^client/package.*\.json$"; then
40-
cd client
41-
npm ci
42-
cd ..
43-
fi
36+
npm i
37+
cd client
38+
npm i
39+
cd ..
4440
npm run build
4541
pm2 reload modl-admin --wait-ready
4642
if pm2 describe modl-admin | grep -q "online"; then

.github/workflows/deploy.yml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,10 @@ jobs:
3333
cd /home/modl/modl-admin
3434
git stash
3535
git pull origin main
36-
if git diff --name-only HEAD@{1} HEAD | grep -q "^package.*\.json$"; then
37-
npm ci
38-
fi
39-
if git diff --name-only HEAD@{1} HEAD | grep -q "^client/package.*\.json$"; then
40-
cd client
41-
npm ci
42-
cd ..
43-
fi
36+
npm i
37+
cd client
38+
npm i
39+
cd ..
4440
npm run build
4541
pm2 reload modl-admin --wait-ready
4642
if pm2 describe modl-admin | grep -q "online"; then

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"author": "modl-gg",
2424
"license": "AGPL-3.0-only",
2525
"dependencies": {
26-
"@modl-gg/shared-web": "^1.0.0",
26+
"@modl-gg/shared-web": "1.0.1",
2727
"@tanstack/react-query-devtools": "^5.81.2",
2828
"@vitejs/plugin-react": "^4.6.0",
2929
"compression": "^1.7.4",

server/db/connectionManager.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import mongoose, { Connection } from 'mongoose';
2+
3+
let globalConnection: Connection | null = null;
4+
5+
/**
6+
* Get or create a connection to the global MongoDB database
7+
* This function returns the existing mongoose connection or creates a new one
8+
*/
9+
export async function connectToGlobalModlDb(): Promise<Connection> {
10+
try {
11+
// If we already have a connection and it's ready, return it
12+
if (globalConnection && globalConnection.readyState === 1) {
13+
return globalConnection;
14+
}
15+
16+
// If mongoose is already connected, use the existing connection
17+
if (mongoose.connection.readyState === 1) {
18+
globalConnection = mongoose.connection;
19+
return globalConnection;
20+
}
21+
22+
// If not connected, establish a new connection
23+
const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/modl-global';
24+
25+
// Set connection options for better timeout handling
26+
const connectionOptions = {
27+
serverSelectionTimeoutMS: 30000, // 30 seconds
28+
socketTimeoutMS: 30000, // 30 seconds
29+
bufferMaxEntries: 0,
30+
bufferCommands: false,
31+
};
32+
33+
await mongoose.connect(mongoUri, connectionOptions);
34+
globalConnection = mongoose.connection;
35+
36+
console.log('✅ Global MongoDB connection established via connectionManager');
37+
return globalConnection;
38+
} catch (error) {
39+
console.error('❌ Failed to connect to global MongoDB:', error);
40+
throw error;
41+
}
42+
}
43+
44+
/**
45+
* Close the global connection
46+
*/
47+
export async function closeGlobalConnection(): Promise<void> {
48+
if (globalConnection) {
49+
await globalConnection.close();
50+
globalConnection = null;
51+
console.log('🔒 Global MongoDB connection closed');
52+
}
53+
}
54+
55+
/**
56+
* Get the current connection status
57+
*/
58+
export function getConnectionStatus(): number {
59+
return globalConnection?.readyState ?? mongoose.connection.readyState;
60+
}

server/routes/servers.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import mongoose, { Schema, model, Document, Model } from 'mongoose';
33
import { IModlServer as IModlServerShared, ApiResponse, ModlServerSchema } from '@modl-gg/shared-web';
44
import { requireAuth } from '../middleware/authMiddleware';
55
import { discordWebhookService } from '../services/DiscordWebhookService';
6+
import { connectToGlobalModlDb } from '../db/connectionManager';
67

78
type 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

Comments
 (0)