Pre-launch Chromium on server startup to avoid cold-start blocking

Chromium cold launch takes several seconds and blocks the event
loop, preventing SSE events from flushing. Now the browser is
warmed up during server startup if any store uses render_js,
so the first search doesn't pay the launch penalty.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
mariosemes
2026-03-26 22:42:15 +01:00
parent 80335d213c
commit 0e2e8d1766
2 changed files with 14 additions and 0 deletions

View File

@@ -7,6 +7,8 @@ import { config } from './config.js';
import { initDatabase, startAutoSave, saveDatabase } from './db/connection.js'; import { initDatabase, startAutoSave, saveDatabase } from './db/connection.js';
import { runMigrations } from './db/migrate.js'; import { runMigrations } from './db/migrate.js';
import { syncFromFiles } from './services/store-sync.js'; import { syncFromFiles } from './services/store-sync.js';
import { warmupBrowser } from './scraper/browser-scraper.js';
import { getEnabledStores } from './models/store.js';
import { storeRoutes } from './routes/stores.js'; import { storeRoutes } from './routes/stores.js';
import { categoryRoutes } from './routes/categories.js'; import { categoryRoutes } from './routes/categories.js';
import { searchRoutes } from './routes/search.js'; import { searchRoutes } from './routes/search.js';
@@ -63,6 +65,12 @@ if (sync.errors.length > 0) {
startAutoSave(); startAutoSave();
// Pre-launch Chromium if any store needs JS rendering
const hasJsStores = getEnabledStores().some((s) => s.render_js);
if (hasJsStores) {
warmupBrowser().catch((err) => app.log.warn(`Browser warmup failed: ${err}`));
}
// Save database on shutdown // Save database on shutdown
process.on('SIGINT', () => { saveDatabase(); process.exit(0); }); process.on('SIGINT', () => { saveDatabase(); process.exit(0); });
process.on('SIGTERM', () => { saveDatabase(); process.exit(0); }); process.on('SIGTERM', () => { saveDatabase(); process.exit(0); });

View File

@@ -116,6 +116,12 @@ export async function scrapeStoreWithBrowser(store: Store, searchUrl: string): P
} }
} }
export async function warmupBrowser(): Promise<void> {
log('Warming up browser...');
await getBrowser();
log('Browser ready');
}
export async function closeBrowser(): Promise<void> { export async function closeBrowser(): Promise<void> {
if (browser) { if (browser) {
await browser.close(); await browser.close();