Skip to main content

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:
1

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
2

Mark Messages as Picked

To prevent duplicate processing, messages are marked with isPicked: true before sending.
3

Send Messages via WAHA

For each message, the scheduler:
  • Retrieves the associated WhatsApp session
  • Calls the WAHA API /api/sendText endpoint
  • Includes message content and target group/contact ID
4

Update Status

After sending:
  • Mark message as isSent: true
  • Record sentAt timestamp
  • Update campaign status if all messages sent
5

Handle Errors

If sending fails:
  • Log detailed error information
  • Mark campaign status as FAILED
  • Continue processing other messages

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

Deploying the scheduler on a dedicated VPS ensures 24/7 availability.
1

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
2

Install Dependencies

Connect to your server and install required software:
3

Deploy Application

Clone and configure your application:
Add your environment variables:
4

Generate Prisma Client

5

Start with PM2

Launch the scheduler with PM2 for automatic restarts:

Option 2: Docker Container

Run the scheduler in a Docker container:
Build and run:

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.
Serverless options may have cold start delays. Ensure your platform supports frequent execution (every 30 seconds) without excessive costs.

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:
  • scheduledAt
  • isSent
  • isDeleted
  • isPicked

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

Error message:
Causes:
  • WAHA server is down
  • Incorrect WAHA_API_URL
  • Network/firewall blocking connection
Solutions:
  1. Verify WAHA server status
  2. Check environment variables
  3. Test connectivity: curl http://waha-url/health
Error message:
Causes:
  • MongoDB server down
  • Network issues
  • Connection string expired
Solutions:
  1. Check MongoDB status
  2. Verify DATABASE_URL is correct
  3. Restart scheduler to reconnect
Error message:
Causes:
  • Session was deleted
  • Session disconnected
Solutions:
  1. Reconnect WhatsApp in dashboard
  2. Update campaign with valid session
  3. Delete orphaned campaigns

Graceful Shutdown

The scheduler handles termination signals:

Deployment Checklist

1

Environment Variables

Verify all required variables are set:
  • DATABASE_URL
  • WAHA_API_URL
  • WAHA_API_KEY
2

Database Access

Test connection to MongoDB from server:
3

WAHA Connectivity

Verify WAHA server is accessible:
4

Prisma Client

Generate Prisma client for production:
5

Start Service

Launch with PM2 and verify:
6

Monitor Logs

Watch for successful startup:
7

Test Message

Create a test campaign scheduled for 1-2 minutes in the future and verify delivery.

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