-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
Feature Request: Automatic Message Cleanup for GOWS Engine
Summary
Add automatic cleanup of old messages from the gows_messages table in PostgreSQL for all GOWS sessions, configurable via environment variables with cron-based scheduling.
Problem
When using the GOWS engine with PostgreSQL storage, the gows_messages table can grow indefinitely, consuming disk space and potentially impacting database performance. Currently, there's no built-in mechanism to automatically clean up old messages.
Proposed Solution
Implement an automatic cleanup system that:
- Periodically deletes messages older than a configurable retention period
- Processes all GOWS sessions automatically
- Uses cron-based scheduling for flexible execution times
- Provides detailed logging of cleanup operations
Technical Implementation
New Files
-
src/plus/engines/gows/services/GowsMessageCleanupService.ts- Service that handles message cleanup for individual sessions and all sessions
- Accesses PostgreSQL databases using
PsqlStore - Deletes messages where
timestamp < cutoffDate - Returns count of deleted messages
-
src/plus/engines/gows/services/GowsCleanupScheduler.ts- Scheduler service that uses
cron-parserto schedule cleanup jobs - Implements
OnModuleInitandOnApplicationShutdown - Calculates next execution time based on cron pattern
- Prevents overlapping executions
- Scheduler service that uses
-
src/plus/engines/gows/gows.module.ts- NestJS module that registers the cleanup services
- Validates environment variables using Joi
- Conditionally loads when PostgreSQL is configured
Module Integration
- Add
GowsModuletosrc/plus/app.module.plus.tswith conditional loading whenWHATSAPP_SESSIONS_POSTGRESQL_URLis defined - Export
SessionManagerandWhatsappConfigServicefromAppModulePlusfor dependency injection
Environment Variables
Number of days to keep messages (default: 90)
WAHA_GOWS_MESSAGE_RETENTION_DAYS=30
Cron pattern for cleanup schedule (default: "0 0 2 * * *" = daily at 2:00 AM)
Format: second minute hour day month day-of-week
WAHA_GOWS_CLEANUP_CRON="0 0 2 * * *"### Cron Pattern Examples
"0 0 2 * * *"- Every day at 2:00 AM (default)"0 30 3 * * *"- Every day at 3:30 AM"0 0 */6 * * *"- Every 6 hours"0 0 2 * * 0"- Every Sunday at 2:00 AM"0 0 2 1 * *"- First day of every month at 2:00 AM
Features
✅ Automatic cleanup for all GOWS sessions
✅ Configurable retention period (days)
✅ Flexible scheduling via cron patterns
✅ Detailed logging of cleanup operations
✅ Error handling per session (doesn't stop if one session fails)
✅ Prevents overlapping executions
✅ Graceful shutdown support
Dependencies
cron-parser(already in project dependencies)
Logging
The implementation provides detailed logs:
- Scheduler startup with configuration
- Next scheduled execution time
- Cleanup start/completion for each session
- Number of messages deleted per session
- Errors per session (non-blocking)