voyage-and-consumption-mcp-server
Version:
Voyage and consumption management server handling vessel voyages, fuel consumption, performance monitoring, and operational data with ERP access for data extraction
129 lines • 5.33 kB
JavaScript
import { MongoClient } from 'mongodb';
import { logger } from '../utils/logger.js';
export class ConnectionManager {
constructor(primaryConfig, secondaryConfig) {
this.primaryClient = null;
this.secondaryClient = null;
this.primaryDb = null;
this.secondaryDb = null;
this.primaryConfig = {
minPoolSize: 5,
maxPoolSize: 20,
maxIdleTimeMS: 30000,
serverSelectionTimeoutMS: 30000,
socketTimeoutMS: 45000,
connectTimeoutMS: 30000,
...primaryConfig
};
this.secondaryConfig = {
minPoolSize: 5,
maxPoolSize: 20,
maxIdleTimeMS: 30000,
serverSelectionTimeoutMS: 30000,
socketTimeoutMS: 45000,
connectTimeoutMS: 30000,
...secondaryConfig
};
}
async initializeConnections() {
try {
logger.info('Initializing MongoDB connection pools...');
// Initialize primary database connection
this.primaryClient = new MongoClient(this.primaryConfig.uri, {
minPoolSize: this.primaryConfig.minPoolSize,
maxPoolSize: this.primaryConfig.maxPoolSize,
maxIdleTimeMS: this.primaryConfig.maxIdleTimeMS,
serverSelectionTimeoutMS: this.primaryConfig.serverSelectionTimeoutMS,
socketTimeoutMS: this.primaryConfig.socketTimeoutMS,
connectTimeoutMS: this.primaryConfig.connectTimeoutMS,
});
await this.primaryClient.connect();
this.primaryDb = this.primaryClient.db(this.primaryConfig.dbName);
logger.info(`Primary database connected: ${this.primaryConfig.dbName}`);
// Initialize secondary database connection
this.secondaryClient = new MongoClient(this.secondaryConfig.uri, {
minPoolSize: this.secondaryConfig.minPoolSize,
maxPoolSize: this.secondaryConfig.maxPoolSize,
maxIdleTimeMS: this.secondaryConfig.maxIdleTimeMS,
serverSelectionTimeoutMS: this.secondaryConfig.serverSelectionTimeoutMS,
socketTimeoutMS: this.secondaryConfig.socketTimeoutMS,
connectTimeoutMS: this.secondaryConfig.connectTimeoutMS,
});
await this.secondaryClient.connect();
this.secondaryDb = this.secondaryClient.db(this.secondaryConfig.dbName);
logger.info(`Secondary database connected: ${this.secondaryConfig.dbName}`);
// Test connections
await this.testConnections();
logger.info('MongoDB connection pools initialized successfully');
}
catch (error) {
logger.error('Failed to initialize MongoDB connection pools:', error);
throw error;
}
}
async testConnections() {
try {
if (this.primaryDb) {
await this.primaryDb.admin().ping();
logger.info('Primary database connection test successful');
}
if (this.secondaryDb) {
await this.secondaryDb.admin().ping();
logger.info('Secondary database connection test successful');
}
}
catch (error) {
logger.error('Connection test failed:', error);
throw error;
}
}
getPrimaryDatabase() {
if (!this.primaryDb) {
throw new Error('Primary database not initialized. Call initializeConnections() first.');
}
return this.primaryDb;
}
getSecondaryDatabase() {
if (!this.secondaryDb) {
throw new Error('Secondary database not initialized. Call initializeConnections() first.');
}
return this.secondaryDb;
}
async close() {
try {
logger.info('Closing MongoDB connection pools...');
if (this.primaryClient) {
await this.primaryClient.close();
this.primaryClient = null;
this.primaryDb = null;
logger.info('Primary database connection closed');
}
if (this.secondaryClient) {
await this.secondaryClient.close();
this.secondaryClient = null;
this.secondaryDb = null;
logger.info('Secondary database connection closed');
}
logger.info('MongoDB connection pools closed successfully');
}
catch (error) {
logger.error('Error closing MongoDB connection pools:', error);
throw error;
}
}
async getConnectionStatus() {
try {
const primaryStatus = this.primaryDb ? await this.primaryDb.admin().ping().then(() => true).catch(() => false) : false;
const secondaryStatus = this.secondaryDb ? await this.secondaryDb.admin().ping().then(() => true).catch(() => false) : false;
return {
primary: primaryStatus,
secondary: secondaryStatus
};
}
catch (error) {
logger.error('Error checking connection status:', error);
return { primary: false, secondary: false };
}
}
}
//# sourceMappingURL=connection-manager.js.map