Harden the Redis layer for production.
- Locks no longer fail open: an unreachable backend throws LockUnavailableError and withLock skips the run (or opts into running unlocked, as template seeding does). The lnbits payment poller keeps polling through an outage, made safe by only sending confirmation emails when a row actually transitioned. - Rate limiter INCR+PEXPIRE now runs as one Lua script, self-healing counters stranded without a TTL. - Per-email login lockout moves to a Redis-backed store shared across replicas (in-memory fallback preserved), case-insensitive on email. - Graceful shutdown on SIGTERM/SIGINT: stop periodic jobs, close the server, force-close lingering SSE sockets, close Redis. - Active PING probe backs the health flag; /health reports last ping. SSE payment streams also poll the DB as a pub/sub-gap fallback. - Scale compose gains requirepass, maxmemory 256mb with noeviction, and an authenticated healthcheck; .env.example documents passwords, TLS (rediss://), and DB-index selection. - First tests in the repo: vitest + ioredis-mock covering the lock, rate limiter, and login lockout stores (27 tests). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
71c277045b
commit
e38d14970d
+46
-6
@@ -24,10 +24,11 @@ import legalPagesRoutes from './routes/legal-pages.js';
|
||||
import legalSettingsRoutes from './routes/legal-settings.js';
|
||||
import faqRoutes from './routes/faq.js';
|
||||
import emailService from './lib/email.js';
|
||||
import { initEmailQueue } from './lib/emailQueue.js';
|
||||
import { startBookingCleanup } from './lib/bookingCleanup.js';
|
||||
import { startHoldSweep } from './lib/holdSweep.js';
|
||||
import { startEventEndSweep } from './lib/eventEndSweep.js';
|
||||
import { initEmailQueue, stopQueue } from './lib/emailQueue.js';
|
||||
import { startBookingCleanup, stopBookingCleanup } from './lib/bookingCleanup.js';
|
||||
import { startHoldSweep, stopHoldSweep } from './lib/holdSweep.js';
|
||||
import { startEventEndSweep, stopEventEndSweep } from './lib/eventEndSweep.js';
|
||||
import { closeRedis } from './lib/redis.js';
|
||||
import { getLock } from './lib/stores/lock.js';
|
||||
import { describeBackends, describeRedis, logSelectedBackends } from './lib/backends.js';
|
||||
|
||||
@@ -1943,8 +1944,11 @@ startEventEndSweep();
|
||||
// Initialize email templates on startup.
|
||||
// Guarded by a distributed lock so that, when running multiple replicas, only
|
||||
// one instance seeds/updates templates per boot instead of all of them racing.
|
||||
// onUnavailable 'run': at boot the Redis connection may not be ready yet, and
|
||||
// seeding is upsert-idempotent, so racing replicas are safe — never skipping
|
||||
// beats never seeding on a first boot during a Redis blip.
|
||||
getLock()
|
||||
.withLock('seed-templates', 30_000, () => emailService.seedDefaultTemplates())
|
||||
.withLock('seed-templates', 30_000, () => emailService.seedDefaultTemplates(), { onUnavailable: 'run' })
|
||||
.then((result) => {
|
||||
if (result === null) {
|
||||
console.log('[Email] Template seeding skipped (another instance holds the lock)');
|
||||
@@ -1961,7 +1965,43 @@ console.log(`📋 OpenAPI spec at http://localhost:${port}/openapi.json`);
|
||||
// Log which backend (memory/redis, local/s3) each subsystem selected.
|
||||
logSelectedBackends();
|
||||
|
||||
serve({
|
||||
const server = serve({
|
||||
fetch: app.fetch,
|
||||
port,
|
||||
});
|
||||
|
||||
// Graceful shutdown: stop the periodic jobs, stop accepting connections, then
|
||||
// close Redis and exit. Open SSE payment streams hold sockets forever, so
|
||||
// server.close() alone never completes — force-close remaining connections
|
||||
// after a short grace period, with a hard exit as the final backstop.
|
||||
let shuttingDown = false;
|
||||
function shutdown(signal: string): void {
|
||||
if (shuttingDown) return;
|
||||
shuttingDown = true;
|
||||
console.log(`[shutdown] ${signal} received, draining...`);
|
||||
|
||||
stopBookingCleanup();
|
||||
stopHoldSweep();
|
||||
stopEventEndSweep();
|
||||
stopQueue();
|
||||
|
||||
server.close(() => {
|
||||
console.log('[shutdown] server closed, closing redis');
|
||||
closeRedis().finally(() => process.exit(0));
|
||||
});
|
||||
|
||||
const forceClose = setTimeout(() => {
|
||||
console.warn('[shutdown] force-closing remaining connections (SSE streams)');
|
||||
(server as any).closeAllConnections?.();
|
||||
}, 5_000);
|
||||
forceClose.unref();
|
||||
|
||||
const forceExit = setTimeout(() => {
|
||||
console.warn('[shutdown] drain timed out, forcing exit');
|
||||
process.exit(1);
|
||||
}, 10_000);
|
||||
forceExit.unref();
|
||||
}
|
||||
|
||||
process.on('SIGTERM', () => shutdown('SIGTERM'));
|
||||
process.on('SIGINT', () => shutdown('SIGINT'));
|
||||
|
||||
Reference in New Issue
Block a user