Monorepo for wisp.place. A static site hosting service built on top of the AT Protocol. wisp.place

add cache only mode to hosting service

Changed files
+29 -1
hosting-service
+11 -1
hosting-service/src/index.ts
···
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';
···
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)) {
···
Health: http://localhost:${PORT}/health
Cache: ${CACHE_DIR}
Firehose: Connected to Firehose
+
Cache-Only: ${CACHE_ONLY_MODE ? 'ENABLED (no DB writes)' : 'DISABLED'}
`);
// Graceful shutdown
+16
hosting-service/src/lib/db.ts
···
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',
{
···
}
export async function upsertSite(did: string, rkey: string, displayName?: string) {
+
// Skip database writes in cache-only mode
+
if (cacheOnlyMode) {
+
console.log('[DB] Skipping upsertSite (cache-only mode)', { did, rkey });
+
return;
+
}
+
try {
// Only set display_name if provided (not undefined/null/empty)
const cleanDisplayName = displayName && displayName.trim() ? displayName.trim() : null;
+2
hosting-service/src/lib/firehose.ts
···
)
// Acquire distributed lock only for database write to prevent duplicate writes
+
// Note: upsertSite will check cache-only mode internally and skip if needed
const lockKey = `db:upsert:${did}:${site}`
const lockAcquired = await tryAcquireLock(lockKey)
···
try {
// Upsert site to database (only one instance does this)
+
// In cache-only mode, this will be a no-op
await upsertSite(did, site, fsRecord.site)
this.log(
'Successfully processed create/update (cached + DB updated)',