Overview
The Message Scheduler is a background service that automatically processes and sends scheduled WhatsApp messages. It runs independently from the main web application, continuously checking for pending messages and dispatching them at the scheduled time.How the Scheduler Works
Execution Cycle
The scheduler operates on a simple but effective polling mechanism:Check for Pending Messages
Every 30 seconds, the scheduler queries the database for messages that:
- Have
isSent: false - Have
isDeleted: false - Have
isPicked: false - Are scheduled for current time or up to 2 minutes ago
Mark Messages as Picked
To prevent duplicate processing, messages are marked with
isPicked: true before sending.Send Messages via WAHA
For each message, the scheduler:
- Retrieves the associated WhatsApp session
- Calls the WAHA API
/api/sendTextendpoint - Includes message content and target group/contact ID
Update Status
After sending:
- Mark message as
isSent: true - Record
sentAttimestamp - Update campaign status if all messages sent
Time Window Logic
The scheduler uses a 2-minute lookback window:This 2-minute window provides resilience against brief service interruptions while preventing very old messages from being sent unexpectedly.
Message Sending Process
When sending a message, the scheduler makes this API call:Deployment Options
Option 1: DigitalOcean VPS (Recommended)
Deploying the scheduler on a dedicated VPS ensures 24/7 availability.Create Droplet
Sign up for DigitalOcean (get $200 credit) and create a droplet:
- OS: Ubuntu 22.04 LTS
- Plan: Basic ($6/month or higher)
- RAM: Minimum 1GB
- Authentication: SSH keys (recommended) or password
Option 2: Docker Container
Run the scheduler in a Docker container:Option 3: Serverless (Advanced)
For serverless deployment, you’ll need to modify the scheduler to run as a cron job rather than a continuous process.Monitoring the Scheduler
PM2 Commands
Manage your scheduler with these PM2 commands:Log Output
The scheduler provides detailed logging:Health Monitoring
Implement health checks to ensure the scheduler is running:Performance Considerations
Scaling
For high-volume deployments:Increase Check Frequency
Modify the 30-second interval for faster processing:
Parallel Processing
Process multiple messages simultaneously using Promise.all:
Database Indexing
Ensure indexes on frequently queried fields:
scheduledAtisSentisDeletedisPicked
Message Batching
Group messages by session to optimize API calls.
Resource Usage
Typical resource consumption:- CPU: < 5% (idle), up to 30% (active sending)
- RAM: ~150-300 MB
- Network: Minimal (depends on message volume)
- Database Connections: 1 persistent connection
Error Handling
Automatic Recovery
The scheduler includes built-in error handling:Failed messages don’t stop the scheduler. It continues processing other messages in the queue.
Common Errors
WAHA API Connection Error
WAHA API Connection Error
Error message:Causes:
- WAHA server is down
- Incorrect
WAHA_API_URL - Network/firewall blocking connection
- Verify WAHA server status
- Check environment variables
- Test connectivity:
curl http://waha-url/health
Database Connection Lost
Database Connection Lost
Error message:Causes:
- MongoDB server down
- Network issues
- Connection string expired
- Check MongoDB status
- Verify
DATABASE_URLis correct - Restart scheduler to reconnect
WhatsApp Session Not Found
WhatsApp Session Not Found
Error message:Causes:
- Session was deleted
- Session disconnected
- Reconnect WhatsApp in dashboard
- Update campaign with valid session
- Delete orphaned campaigns
Graceful Shutdown
The scheduler handles termination signals:Deployment Checklist
Best Practices
Use Process Manager
Always run the scheduler with PM2 or similar for automatic restarts and monitoring.
Monitor Regularly
Check logs daily to catch issues early and ensure messages are being processed.
Backup Strategy
Keep your server configuration and PM2 ecosystem file in version control.
Alert on Failures
Set up monitoring to alert you when the scheduler process stops.
Separate from Web App
Run scheduler on a dedicated server or container separate from your web application.
Test Thoroughly
Before production, test with various campaign types and edge cases.
Next Steps
Create Campaigns
Start scheduling messages with the scheduler running
Admin Dashboard
Monitor campaign status and system health