Skip to content

Add logs retention#38

Merged
mishankov merged 4 commits into
mainfrom
issue-21-logs-retention
Feb 26, 2026
Merged

Add logs retention#38
mishankov merged 4 commits into
mainfrom
issue-21-logs-retention

Conversation

@mishankov
Copy link
Copy Markdown
Owner

Closes #21

Summary

Adds a retention parameter to config that enables automatic cleanup of old request logs from the database.

Changes

  • Config (internal/config/config.go): Added Retention field (int, seconds)
  • Service (internal/requestlog/service.go): Added DeleteOlderThan(thresholdUnix int64) method
  • App (internal/app/build.go):
    • Starts retention scheduler when retention > 0
    • Cleanup runs every hour (configurable via cleanupInterval constant)
    • Proper context cancellation for graceful shutdown
    • Scheduler registered as startup task
  • Tests (internal/requestlog/handler_test.go): Added test for DeleteOlderThan functionality

Behavior

  • If retention is not set or 0, no cleanup occurs (existing behavior)
  • If retention is set to N seconds, logs older than N seconds are deleted every hour
  • Cleanup runs as a background goroutine with proper lifecycle management
  • Errors during cleanup are logged but do not crash the application

Testing

  • All existing tests pass
  • New test TestDeleteOlderThan_RemovesOnlyOldLogs verifies correct deletion behavior
  • Build succeeds with race detector enabled

Code Review

Addressed P2 findings from code review:

  • Added context cancellation for graceful shutdown
  • Changed cleanup interval to fixed 1 hour instead of retention period (to avoid logs living up to 2x retention)

- Add retention parameter to config (seconds)
- Add DeleteOlderThan method to RequestLogService
- Start scheduler to cleanup old logs when retention is configured
- Run cleanup every hour to avoid excessive DB load
- Add context cancellation for graceful shutdown
- Add test for DeleteOlderThan functionality
Fixes P2 finding from code review: DELETE queries on time column
would perform full table scans without an index. Also fixes SQLite
locking issue by closing PRAGMA query rows explicitly before DDL.
@mishankov mishankov changed the title Fix #21: Add logs retention feature Add logs retention Feb 26, 2026
@mishankov mishankov merged commit 7ff4ac6 into main Feb 26, 2026
4 checks passed
@mishankov mishankov deleted the issue-21-logs-retention branch February 26, 2026 19:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Logs retention

1 participant