Message Scheduler Setup
The message scheduler is a background service that automatically sends scheduled WhatsApp messages. It needs to run continuously on a server, separate from the Vercel-hosted frontend.
Important : The scheduler must run 24/7 on a VPS. Vercel’s serverless functions are not suitable for long-running background tasks. We recommend DigitalOcean, AWS EC2, or similar VPS providers.
How the Scheduler Works
The scheduler service:
Checks for pending messages every 30 seconds
Automatically sends messages when scheduled time arrives
Updates campaign status and tracking in real-time
Handles errors with retry logic and detailed logging
Runs independently from the web application
DigitalOcean VPS Setup
1. Create a Droplet
Get $200 in Credits Use this referral link to get $200 in DigitalOcean credits!
Recommended specifications:
OS : Ubuntu 22.04 LTS (or latest LTS)
Plan : Basic Droplet with 1GB RAM minimum
CPU : 1 vCPU (sufficient for scheduler)
Storage : 25GB SSD
Region : Choose closest to your WAHA server
Authentication:
For beginners: Use password authentication
Recommended: Use SSH keys for better security
2. Initial Server Setup
Connect to your server and perform initial setup:
SSH Connection
System Update
Install Node.js
Install pnpm
Install PM2
# Connect to your server
ssh root@your-server-ip
# Enter your password or use your SSH key
3. Deploy the Scheduler
Clone Repository
Install Dependencies
Environment Configuration
Generate Prisma Client
# Clone your repository
git clone https://github.com/yourusername/whatsapp-group-manager.git
cd whatsapp-group-manager
4. Start Scheduler with PM2
PM2 ensures the scheduler runs continuously and restarts automatically if it crashes.
Start with PM2
Save PM2 Configuration
# Start the scheduler service
pm2 start src/scripts/messageScheduler.ts \
--interpreter ./node_modules/.bin/tsx \
--name whatsapp-scheduler \
--env production
PM2 Startup : After running pm2 startup, it will output a command that you need to copy and execute. This ensures PM2 starts automatically when the server reboots.
PM2 Management Commands
Monitor the Scheduler
Check Status
View Logs
Monitor Resources
# View all PM2 processes
pm2 status
# View detailed info for scheduler
pm2 show whatsapp-scheduler
Control the Scheduler
Restart
Stop and Start
Delete Process
# Restart the scheduler
pm2 restart whatsapp-scheduler
# Restart with zero-downtime
pm2 reload whatsapp-scheduler
PM2 Configuration File
For advanced configuration, create an ecosystem.config.js file:
ecosystem.config.js
Start with Config
module . exports = {
apps: [{
name: 'whatsapp-scheduler' ,
script: 'src/scripts/messageScheduler.ts' ,
interpreter: './node_modules/.bin/tsx' ,
instances: 1 ,
exec_mode: 'fork' ,
env_production: {
NODE_ENV: 'production' ,
DATABASE_URL: process . env . DATABASE_URL ,
WAHA_API_KEY: process . env . WAHA_API_KEY ,
WAHA_API_URL: process . env . WAHA_API_URL ,
},
error_file: './logs/scheduler-error.log' ,
out_file: './logs/scheduler-out.log' ,
log_date_format: 'YYYY-MM-DD HH:mm:ss Z' ,
max_memory_restart: '500M' ,
autorestart: true ,
watch: false ,
max_restarts: 10 ,
min_uptime: '10s' ,
}]
};
Updating the Scheduler
When you need to deploy updates:
Pull Updates
Zero-Downtime Update
# Navigate to project directory
cd whatsapp-group-manager
# Pull latest changes
git pull origin main
# Install any new dependencies
pnpm install
# Regenerate Prisma client if schema changed
pnpm prisma:generate
# Restart the scheduler
pm2 restart whatsapp-scheduler
Troubleshooting
Scheduler Not Starting
Check logs:
pm2 logs whatsapp-scheduler --err --lines 50
Common issues:
Missing environment variables
Database connection failure
Invalid Prisma client (run pnpm prisma:generate)
Node.js version mismatch
Database Connection Issues
# Test database connection
pnpm prisma db push
# If successful, restart scheduler
pm2 restart whatsapp-scheduler
High Memory Usage
# Check current memory usage
pm2 monit
# Set memory limit in ecosystem.config.js
max_memory_restart: '500M'
Scheduler Stopped After Reboot
# Verify PM2 startup is configured
pm2 startup
# Save current process list
pm2 save
# Check if PM2 is running
systemctl status pm2-root
Alternative VPS Providers
AWS EC2
# Similar process on Amazon Linux 2
sudo yum update -y
sudo yum install -y nodejs npm
npm install -g pnpm pm2
# Follow same deployment steps
# On Debian/Ubuntu image
sudo apt update && sudo apt upgrade -y
# Follow DigitalOcean steps above
Linode
# On Ubuntu 22.04 image
# Follow DigitalOcean steps above
Security Best Practices
Firewall Configuration
# Enable UFW firewall
ufw allow OpenSSH
ufw enable
Keep System Updated
# Regular updates
apt update && apt upgrade -y
Use SSH Keys
Disable password authentication
Use strong SSH key passphrases
Environment Variables
Never commit .env.production to git
Use strong, unique credentials
Monitor Logs
# Regularly check for errors
pm2 logs whatsapp-scheduler --lines 100
Log Rotation
# Install PM2 log rotate
pm2 install pm2-logrotate
# Configure retention (keep 7 days)
pm2 set pm2-logrotate:retain 7
# Configure max file size (10MB)
pm2 set pm2-logrotate:max_size 10M
Resource Monitoring
# Enable PM2 Plus for advanced monitoring (optional)
pm2 link [secret-key] [public-key]
Next Steps
Environment Variables Review all configuration options
Vercel Deployment Deploy the web application