import { fileURLToPath } from 'node:url'; import { parseAppConfig } from '../../config/app-config.schema.js'; import { DatabaseClient } from '../../database/database.client.js'; import { MetricsService } from '../../metrics/metrics.service.js'; import { IdempotencyRepository } from './idempotency.repository.js'; // Manual operator pass over the idempotency ledger: the API also purges expired // claims in the background, but an operator must be able to reclaim space // without waiting for traffic. It never touches unexpired claims. // in the stand: docker compose exec api node dist/messages/idempotency/idempotency-gc.cli.js // on the host: npm run idempotency:gc (needs a reachable DATABASE_URL) async function main(): Promise { const config = parseAppConfig(process.env as Record); const database = new DatabaseClient( { getOrThrow: (key: keyof typeof config) => config[key], } as never, new MetricsService(), ); try { const repository = new IdempotencyRepository(database); const purged = await repository.purgeExpired( config.IDEMPOTENCY_TTL_SECONDS, ); process.stdout.write( `Purged ${purged} idempotency key(s) older than ${config.IDEMPOTENCY_TTL_SECONDS} seconds\n`, ); } finally { await database.onModuleDestroy(); } } if (process.argv[1] === fileURLToPath(import.meta.url)) await main();