Running Queue Workers

Queue workers are background processes that fetch and execute jobs from the queue. This guide explains how to run and manage workers.

Starting a Worker

Basic Usage

Start a worker to process jobs from the default queue:

# Process jobs from 'default' queue
python main.py --queue-work

# Or using the routemq command
routemq --queue-work

The worker will:

  • Connect to Redis/MySQL based on QUEUE_CONNECTION

  • Poll the queue for jobs

  • Execute jobs as they become available

  • Automatically retry failed jobs

  • Run until stopped (Ctrl+C)

Worker Options

--queue

Specify which queue to process:

--connection

Override the queue connection:

--max-jobs

Process a maximum number of jobs then stop:

--max-time

Run for a maximum time (in seconds) then stop:

--sleep

Seconds to sleep when no jobs are available:

--max-tries

Override the maximum retry attempts for all jobs:

--timeout

Maximum seconds a job can run:

Multiple Workers

Run multiple workers for different queues:

Production Deployment

Using Docker Compose

The easiest way to run workers in production:

See Docker Deploymentarrow-up-right for details.

Using Supervisor

For non-Docker deployments, use Supervisor:

Then manage with supervisorctl:

Using systemd

Create systemd service files:

Manage with systemctl:

Worker Lifecycle

Understanding how workers process jobs:

Graceful Shutdown

Workers handle shutdown signals gracefully:

Monitoring Workers

View Worker Output

Check Queue Size

Monitor with Redis CLI

Monitor with MySQL

Troubleshooting

Worker Not Processing Jobs

Check if worker is running:

Check worker logs for errors:

Common issues:

  • Queue name mismatch between dispatch and worker

  • Redis/MySQL connection issues

  • Jobs failing during execution

Jobs Timing Out

If jobs are timing out:

High Memory Usage

If worker memory grows:

Worker Stuck

If worker seems stuck:

  1. Send SIGTERM to gracefully stop

  2. Check for infinite loops in job code

  3. Add timeouts to external API calls

  4. Review job logs for errors

Best Practices

1. Run Multiple Workers

2. Use Different Queues

3. Set Resource Limits

4. Log Everything

5. Monitor Queue Depth

Next Steps

Last updated