[Cron] Implement scheduled cleanup job for expired tokens and stale data (issue #66)#848
Open
Emmanard wants to merge 1 commit into
Open
[Cron] Implement scheduled cleanup job for expired tokens and stale data (issue #66)#848Emmanard wants to merge 1 commit into
Emmanard wants to merge 1 commit into
Conversation
- Add migration: user_sessions, password_reset_tokens, distributions tables; deleted_at on notification_jobs - Add Drizzle schema definitions for new tables + deleted_at on notification_jobs - Create metrics.service.ts with four prom-client counters (sessions, reset tokens, notifications, distributions) - Create cron.service.ts with CronDbAdapter interface and four cleanup job functions; each increments its Prometheus counter - Create cleanup.cron.ts scheduling four node-cron jobs (hourly/daily/weekly) with overlap guards and CLEANUP_CRON_DISABLED env gate - Register startCleanupCron() in index.ts alongside existing crons - Add 11-test unit suite covering delete conditions and counter increments with mocked DB adapter Co-Authored-By: Claude Sonnet 4.6 <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 #66.
Implements four scheduled DB maintenance jobs via a new
cron.service.ts+cleanup.cron.ts, with Prometheus counter metrics and full unit-test coverage.DELETE FROM user_sessions WHERE expires_at < NOW()DELETE FROM password_reset_tokens WHERE expires_at < NOW()notification_jobsrows older than 90 days viadeleted_atarchived_atondistributionsrows withstatus='failed'older than 30 daysprom-clientCounters exported from newmetrics.service.tsChanged files
migrations/1719360000000_add-cleanup-tables.jsuser_sessions,password_reset_tokens,distributionstables; addsdeleted_attonotification_jobssrc/db/schema.tsdeleted_atonnotification_jobssrc/services/metrics.service.tsprom-clientCounters — one per cleanup jobsrc/services/cron.service.tsCronDbAdapterinterface + four job functions; each increments its countersrc/cron/cleanup.cron.tsCLEANUP_CRON_DISABLEDenv gatesrc/index.tsstartCleanupCron()alongside existing cronstests/services/cron.service.test.tspackage.jsonprom-client ^15.1.3Test plan
jest tests/services/cron.service.test.ts— 11/11 passingtsc --noEmit— zero new errors in changed/created filesnpm run migrate up/metricsendpoint (requires wiring metrics route — follow-up)Notes
user_sessions,password_reset_tokens,distributions) are clean-room additions. The auth service currently uses Redis/in-memory for sessions and reset tokens; a follow-up PR can dual-write to DB to populate these tables.CLEANUP_CRON_DISABLED=trueto gate all four jobs off in any environment.closes #463