Skip to main content
Glama
db-warmer.cjsโ€ข5 kB
#!/usr/bin/env node const { SimpleEGWDatabase } = require('./database-utils.js'); class DatabaseWarmer { constructor() { this.db = null; this.warmupQueries = [ 'faith', 'love', 'prayer', 'hope', 'charity' ]; this.isWarm = false; this.lastWarmup = null; this.warmupInterval = 5 * 60 * 1000; // 5 minutes this.sharedDB = null; // Shared warm database instance } async initialize() { console.log('๐Ÿ”ฅ Initializing database warmer...'); this.db = new SimpleEGWDatabase(); // Perform initial warmup await this.warmupDatabase(); // Set up periodic warmup to keep database hot setInterval(() => { this.maintenanceWarmup(); }, this.warmupInterval); console.log('โœ… Database warmer initialized and ready'); } async warmupDatabase() { console.log('๐Ÿ”ฅ Warming up database (cold start)...'); const startTime = Date.now(); try { // Warmup with common search queries for (const query of this.warmupQueries) { await this.db.findEGWQuotes(query, 1); } // Additional warmup: Get stats to load book data await this.db.getStats(); const warmupTime = Date.now() - startTime; console.log(`โœ… Database warmup completed in ${warmupTime}ms`); this.isWarm = true; this.lastWarmup = new Date(); this.sharedDB = this.db; // Set shared reference return warmupTime; } catch (error) { console.error('โŒ Database warmup failed:', error.message); throw error; } } async maintenanceWarmup() { if (!this.isWarm) { console.log('๐Ÿ”ฅ Performing maintenance warmup...'); return await this.warmupDatabase(); } // Light maintenance - just one quick query try { const startTime = Date.now(); await this.db.findEGWQuotes('faith', 1); const duration = Date.now() - startTime; console.log(`๐Ÿ”ฅ Maintenance warmup: ${duration}ms`); this.lastWarmup = new Date(); } catch (error) { console.error('โŒ Maintenance warmup failed:', error.message); this.isWarm = false; } } async ensureWarm() { if (!this.isWarm) { console.log('๐Ÿ”ฅ Database not warm, performing warmup...'); await this.warmupDatabase(); } // Check if last warmup was more than 2 minutes ago const twoMinutesAgo = new Date(Date.now() - 2 * 60 * 1000); if (this.lastWarmup < twoMinutesAgo) { console.log('๐Ÿ”ฅ Database cooling down, quick warmup...'); await this.maintenanceWarmup(); } } // NEW: Get shared warm database instance getSharedDatabase() { if (!this.isWarm || !this.sharedDB) { throw new Error('โŒ Database not warm! Run warmer.initialize() first.'); } return this.sharedDB; } // NEW: Create warm database wrapper that prevents cold starts static async createWarmedDatabase() { const warmer = new DatabaseWarmer(); await warmer.initialize(); // Return a wrapper that only uses the warm connection return { findEGWQuotes: (query, numQuotes) => warmer.getSharedDatabase().findEGWQuotes(query, numQuotes), getStats: () => warmer.getSharedDatabase().getStats(), search: (query, limit, offset) => warmer.getSharedDatabase().search(query, limit, offset), getBook: (bookId) => warmer.getSharedDatabase().getBook(bookId), getParagraphs: (bookId, limit, offset) => warmer.getSharedDatabase().getParagraphs(bookId, limit, offset), getBooks: (limit) => warmer.getSharedDatabase().getBooks(limit), close: () => warmer.close() }; } getStatus() { return { isWarm: this.isWarm, lastWarmup: this.lastWarmup, warmupInterval: this.warmupInterval }; } close() { if (this.db) { this.db.close(); } } } // Standalone execution for testing if (require.main === module) { async function testWarmup() { const warmer = new DatabaseWarmer(); try { await warmer.initialize(); console.log('๐Ÿงช Testing shared warm database...'); // Test performance with shared database const testQueries = ['patience', 'grace', 'mercy']; for (const query of testQueries) { console.log(`\n๐Ÿ” Testing shared warm query: "${query}"`); const start = Date.now(); const result = await warmer.getSharedDatabase().findEGWQuotes(query, 2); const duration = Date.now() - start; console.log(`โฑ๏ธ Shared warm query time: ${duration}ms`); if (result.success) { console.log(`๐Ÿ“Š Found ${result.total_found} quotes`); } } console.log('\nโœ… Shared warm database test completed successfully!'); } catch (error) { console.error('โŒ Warmup test failed:', error.message); } finally { warmer.close(); } } testWarmup(); } module.exports = DatabaseWarmer;

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/pythondev-pro/egw_writings_mcp_server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server