feat(jobs): daily cleanup for expired idempotency_keys#120
Merged
Conversation
`withIdempotency()` only purges rows lazily on the next lookup with the same `(userId, key, method, path)` tuple — most rows never see a retry, so without a sweeper the table grows unbounded with stale 24h-expired entries (Security-Review HIGH-2 from PR #117). Adds a pg-boss queue that runs daily at 03:00 Europe/Berlin (parallel to the weekly Sunday data-backup job) and bulk-deletes expired rows. Outcome surfaces as a Wide Event annotation `idempotency_cleanup_deleted` so the run is queryable in Loki. Helper extracted to `src/lib/jobs/idempotency-cleanup.ts` for clean unit testing without mocking pg-boss internals. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes Security-Review HIGH-2 from PR #117.
withIdempotency()only purges rows lazily on the next lookup with the same(userId, key, method, path)tuple — most rows never see a retry, so without a sweeper theidempotency_keystable grows unbounded with stale 24h-expired entries.prisma.idempotencyKey.deleteMany({ where: { expiresAt: { lt: new Date() } } })— uses the existingidxonexpiresAtidempotency_cleanup_deletedso the run is queryable in Lokisrc/lib/jobs/idempotency-cleanup.tsfor unit-testable separation from the pg-boss harnessFiles
src/lib/jobs/idempotency-cleanup.ts(new) — exportedcleanupExpiredIdempotencyKeys(prisma)returns deleted countsrc/lib/jobs/__tests__/idempotency-cleanup.test.ts(new) — 3 tests: success path, empty table, error propagationsrc/lib/jobs/reminder-worker.ts— register queue, schedule, payload type, handler, worker (+29 LOC, surgical)Test plan
pnpm typecheckcleanpnpm test— 282/282 pass (3 new inidempotency-cleanup.test.ts)@@index([expiresAt])on the model — sweep is index-backed, not a full scanidempotency_cleanup_deletedin Grafana/Loki🤖 Generated with Claude Code