From 37856435a567562223fd6e694804c55817217528 Mon Sep 17 00:00:00 2001 From: "@nekomimi.pet" Date: Tue, 11 Nov 2025 05:59:57 -0500 Subject: [PATCH] add cache only mode to hosting service --- hosting-service/src/index.ts | 12 +++++++++++- hosting-service/src/lib/db.ts | 16 ++++++++++++++++ hosting-service/src/lib/firehose.ts | 2 ++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/hosting-service/src/index.ts b/hosting-service/src/index.ts index dbe321b..4a3706d 100644 --- a/hosting-service/src/index.ts +++ b/hosting-service/src/index.ts @@ -4,7 +4,7 @@ import { FirehoseWorker } from './lib/firehose'; import { logger } from './lib/observability'; import { mkdirSync, existsSync } from 'fs'; import { backfillCache } from './lib/backfill'; -import { startDomainCacheCleanup, stopDomainCacheCleanup } from './lib/db'; +import { startDomainCacheCleanup, stopDomainCacheCleanup, setCacheOnlyMode } from './lib/db'; const PORT = process.env.PORT ? parseInt(process.env.PORT) : 3001; const CACHE_DIR = process.env.CACHE_DIR || './cache/sites'; @@ -14,6 +14,15 @@ const args = process.argv.slice(2); const hasBackfillFlag = args.includes('--backfill'); const backfillOnStartup = hasBackfillFlag || process.env.BACKFILL_ON_STARTUP === 'true'; +// Cache-only mode: service will only cache files locally, no DB writes +const hasCacheOnlyFlag = args.includes('--cache-only'); +export const CACHE_ONLY_MODE = hasCacheOnlyFlag || process.env.CACHE_ONLY_MODE === 'true'; + +// Configure cache-only mode in database module +if (CACHE_ONLY_MODE) { + setCacheOnlyMode(true); +} + // Ensure cache directory exists if (!existsSync(CACHE_DIR)) { mkdirSync(CACHE_DIR, { recursive: true }); @@ -65,6 +74,7 @@ Server: http://localhost:${PORT} Health: http://localhost:${PORT}/health Cache: ${CACHE_DIR} Firehose: Connected to Firehose +Cache-Only: ${CACHE_ONLY_MODE ? 'ENABLED (no DB writes)' : 'DISABLED'} `); // Graceful shutdown diff --git a/hosting-service/src/lib/db.ts b/hosting-service/src/lib/db.ts index a0cf8f2..d6dd678 100644 --- a/hosting-service/src/lib/db.ts +++ b/hosting-service/src/lib/db.ts @@ -1,6 +1,16 @@ import postgres from 'postgres'; import { createHash } from 'crypto'; +// Global cache-only mode flag (set by index.ts) +let cacheOnlyMode = false; + +export function setCacheOnlyMode(enabled: boolean) { + cacheOnlyMode = enabled; + if (enabled) { + console.log('[DB] Cache-only mode enabled - database writes will be skipped'); + } +} + const sql = postgres( process.env.DATABASE_URL || 'postgres://postgres:postgres@localhost:5432/wisp', { @@ -130,6 +140,12 @@ export async function getCustomDomainByHash(hash: string): Promise