Skip to content

feat(listener): add automatic cleanup service for events and DB rows#210

Open
susanyusuf wants to merge 1 commit into
Core-Foundry:mainfrom
susanyusuf:feat/automatic-cleanup
Open

feat(listener): add automatic cleanup service for events and DB rows#210
susanyusuf wants to merge 1 commit into
Core-Foundry:mainfrom
susanyusuf:feat/automatic-cleanup

Conversation

@susanyusuf

Copy link
Copy Markdown

Closes #100
Closes #63
Closes #179
Closes #177

Description 1. listener/src/services/cleanup-service.ts (new file)

Introduced a Cleanup Service class that runs on a configurable
interval and deletes stale database rows from two tables:

  • scheduled_notifications — removes entries with status
    COMPLETED, FAILED, or CANCELLED older than the configured
    retention period (default: 7 days)
  • rate_limit_events — removes audit rows older than the
    configured retention period (default: 24 hours)

Also delegates in-memory event pruning to EventRegistry via
startCleanup() / stopCleanup().

Bug fixed: logger.info('CleanupService started', this.config)
caused a TypeScript compile error because CleanupConfig
an index signature required by the logger's Record string,
Record<string, unknown> parameter type. Fixed with
this.config as unknown as Record<string, unknown.

  1. listener/src/services/cleanup-service.test.ts (new file)

Test suite covering:

  • EventRegistry TTL pruning (expired events removed, fresh
    events kept, interval start/stop)
  • CleanupService DB cleanup (correct SQL fired, interval
    behavior, idempotent start(), clean stop())
  1. listener/src/store/event-registry.ts

Extended EventRegistry to support TTL-based expiry:

  • Added ttlMs constructor parameter (default: 24 hours)
  • Added pruneExpired() — filters out events whose receivedAt
    timestamp is older than ttlMs
  • Added startCleanup(intervalMs) / stopCleanup() — controls a
    self-managed setInterval for automatic pruning
  1. listener/src/types/index.ts

Added the AppCleanupConfig interface defining four configurable
retention/interval values:

  • intervalMs — how often cleanup runs
  • notificationRetentionMs — how long to keep old notifications
    in the DB
  • rateLimitEventRetentionMs — how long to keep rate-limit audit
    rows
  • eventRetentionMs — how long to keep in-memory events

Also added cleanup?: AppCleanupConfig to the main Config
interface.
5. listener/src/config.ts

Added loadCleanupConfig() which reads the four new retention
values from environment variables (CLEANUP_INTERVAL_MS,
NOTIFICATION_RETENTION_MS, RATE_LIMIT_EVENT_RETENTION_MS,
EVENT_RETENTION_MS) with sensible defaults. The result is
merged into the main config object returned by loadConfig().
6. listener/src/index.ts

Wired CleanupService into the application lifecycle:

  • Instantiated after DB initialization, using config.cleanup
    settings
  • cleanupService.start() called at boot
  • cleanupService.stop() called during graceful shutdown
    alongside other services
  • The EventRegistry's ttlMs is also overridden at startup from
    config before the service starts

@drips-wave

drips-wave Bot commented Jun 24, 2026

Copy link
Copy Markdown

@susanyusuf Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant