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;