Queue Setup and Worker Management #

The Server Monitor plugin uses Laravel queues to process server checks efficiently. This guide covers complete queue setup and worker management.

Queue Architecture #

The plugin uses three separate queues:

  • premium - 1-minute SLA checks for paid plans
  • free - 5-minute SLA checks for free plans
  • calculations - Uptime recalculation jobs

Prerequisites #

Before setting up queues:

  • [x] Plugin installed and configured
  • [x] Queue driver configured (database or redis)
  • [x] Supervisor installed on your server

Installing Supervisor #

Ubuntu/Debian #

sudo apt update
sudo apt install supervisor

CentOS/RHEL #

sudo yum install supervisor
# or
sudo dnf install supervisor

Start Supervisor #

sudo systemctl enable supervisor
sudo systemctl start supervisor

Supervisor Configuration #

1. Create Configuration File #

Create /etc/supervisor/conf.d/servermonitor-workers.conf:

[program:servermonitor-premium]
process_name=%(program_name)s_%(process_num)02d
command=php /path/to/your/site/artisan queue:work --queue=premium --sleep=3 --tries=3 --timeout=3600
autostart=true
autorestart=true
numprocs=2
redirect_stderr=true
stdout_logfile=/path/to/your/site/storage/logs/worker-premium.log
user=www-data
stopwaitsecs=3600

[program:servermonitor-free]
process_name=%(program_name)s_%(process_num)02d
command=php /path/to/your/site/artisan queue:work --queue=free --sleep=3 --tries=3 --timeout=3600
autostart=true
autorestart=true
numprocs=1
redirect_stderr=true
stdout_logfile=/path/to/your/site/storage/logs/worker-free.log
user=www-data
stopwaitsecs=3600

[program:servermonitor-calculations]
process_name=%(program_name)s_%(process_num)02d
command=php /path/to/your/site/artisan queue:work --queue=calculations --sleep=3 --tries=3 --timeout=3600
autostart=true
autorestart=true
numprocs=1
redirect_stderr=true
stdout_logfile=/path/to/your/site/storage/logs/worker-calculations.log
user=www-data
stopwaitsecs=3600

Important: Replace /path/to/your/site with your actual site path and www-data with your web server user.

2. Apply Configuration #

# Read new configuration
sudo supervisorctl reread

# Update supervisor with new programs
sudo supervisorctl update

# Start all workers
sudo supervisorctl start servermonitor-premium:*
sudo supervisorctl start servermonitor-free:*
sudo supervisorctl start servermonitor-calculations:*

3. Verify Workers #

# Check worker status
sudo supervisorctl status

# Should show output like:
# servermonitor-premium:servermonitor-premium_00   RUNNING   pid 12345, uptime 0:01:23
# servermonitor-premium:servermonitor-premium_01   RUNNING   pid 12346, uptime 0:01:23
# servermonitor-free:servermonitor-free_00         RUNNING   pid 12347, uptime 0:01:23
# servermonitor-calculations:servermonitor-calculations_00 RUNNING pid 12348, uptime 0:01:23

Worker Scaling Guidelines #

Premium Workers (1-minute SLA) #

Capacity: Each worker can handle ~60 checks per minute

Scaling Formula:

Workers Needed = (Total Premium Servers รท 60) ร— Average Response Time ร— 1.5

When to Scale:

  • Queue backlog > 30 jobs
  • SLA compliance < 95%
  • Response times > 45 seconds

Free Workers (5-minute SLA) #

Capacity: Each worker can handle ~12 checks per minute

Scaling Formula:

Workers Needed = (Total Free Servers รท 300) ร— Average Response Time ร— 1.2

When to Scale:

  • Queue backlog > 60 jobs
  • SLA compliance < 95%
  • Response times > 4 minutes

Resource Requirements #

Workers RAM Usage CPU Usage
1-2 100-200MB 5-10%
3-5 200-500MB 10-20%
6-10 500MB-1GB 20-40%

Scaling Workers #

Adding More Workers #

  1. Edit Configuration:

    sudo nano /etc/supervisor/conf.d/servermonitor-workers.conf
  2. Increase numprocs:

    # For premium workers (increase from 2 to 4)
    [program:servermonitor-premium]
    numprocs=4
    
    # For free workers (increase from 1 to 2)
    [program:servermonitor-free]
    numprocs=2
  3. Apply Changes:

    sudo supervisorctl reread
    sudo supervisorctl update
    sudo supervisorctl restart servermonitor-premium:*
    sudo supervisorctl restart servermonitor-free:*

Monitoring Worker Performance #

The plugin includes built-in worker monitoring. Access via:

  • Dashboard: /albrightlabs/servermonitor/workerperformance (Albright Labs staff only)
  • Console: php artisan servermonitor:check-capacity
  • API: /api/servermonitor/worker-status/YOUR_API_KEY

See Worker Monitoring Guide for detailed information.

Log Management #

Worker Logs #

Logs are stored in:

  • Premium: storage/logs/worker-premium.log
  • Free: storage/logs/worker-free.log
  • Calculations: storage/logs/worker-calculations.log

Log Rotation #

Add to /etc/logrotate.d/servermonitor:

/path/to/your/site/storage/logs/worker-*.log {
    daily
    missingok
    rotate 14
    compress
    delaycompress
    notifempty
    create 0644 www-data www-data
    postrotate
        supervisorctl restart servermonitor-premium:*
        supervisorctl restart servermonitor-free:*
        supervisorctl restart servermonitor-calculations:*
    endscript
}

Queue Driver Configuration #

Database Queue (Default) #

Pros: Easy setup, no additional services Cons: Slower with high volume

Configuration (config/queue.php):

'connections' => [
    'database' => [
        'driver' => 'database',
        'table' => 'jobs',
        'queue' => 'default',
        'retry_after' => 3600,
    ],
],

Pros: Better performance, handles high volume Cons: Requires Redis installation

Installation:

# Ubuntu/Debian
sudo apt install redis-server

# Start Redis
sudo systemctl enable redis
sudo systemctl start redis

Configuration (config/queue.php):

'connections' => [
    'redis' => [
        'driver' => 'redis',
        'connection' => 'default',
        'queue' => env('REDIS_QUEUE', 'default'),
        'retry_after' => 3600,
        'block_for' => null,
    ],
],

Environment (.env):

QUEUE_CONNECTION=redis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

Troubleshooting #

Workers Not Starting #

# Check supervisor status
sudo systemctl status supervisor

# Check configuration syntax
sudo supervisorctl reread

# Check log files
tail -f /path/to/site/storage/logs/worker-premium.log

High Memory Usage #

# Restart workers to clear memory
sudo supervisorctl restart servermonitor-premium:*

# Check for memory leaks
top -p $(pgrep -f "queue:work")

Queue Backlog #

# Check queue size (database)
php artisan queue:work --once --queue=premium

# Check queue size (Redis)
redis-cli llen queues:premium

# Clear stuck jobs
php artisan queue:clear

Failed Jobs #

# View failed jobs
php artisan queue:failed

# Retry failed jobs
php artisan queue:retry all

# Clear failed jobs
php artisan queue:flush

Common Commands #

# Worker Management
sudo supervisorctl status                    # Check all workers
sudo supervisorctl stop servermonitor-premium:*  # Stop premium workers
sudo supervisorctl start servermonitor-premium:* # Start premium workers
sudo supervisorctl restart all              # Restart all workers

# Queue Monitoring
php artisan queue:work --once              # Process one job
php artisan queue:listen                   # Listen for jobs (dev only)
php artisan queue:monitor                  # Monitor queue performance

# Troubleshooting
php artisan queue:failed                   # List failed jobs
php artisan queue:clear                    # Clear all jobs
php artisan queue:restart                  # Restart all workers

Performance Tips #

  1. Use Redis: Better performance than database queue
  2. Monitor Queue Size: Don't let backlogs build up
  3. Scale Proactively: Add workers before hitting limits
  4. Log Rotation: Prevent log files from growing too large
  5. Memory Management: Restart workers periodically to clear memory

Next Steps #

After queue setup:

  1. Configure API Endpoints - Set up cron jobs to dispatch jobs
  2. Set Up Monitoring - Monitor worker performance
  3. Add Servers - Start monitoring your first server

Previous: Configuration | Next: API Reference