Resque Deployment and Usage Guide
1. Prerequisites
- Ruby (version 2.3 or higher recommended)
- Redis (version 2.2 or higher) - Resque requires Redis as its backend
- Bundler - For managing Ruby dependencies
- MultiJson gem - Required for JSON encoding/decoding (automatically installed via Bundler)
2. Installation
Installing Resque
# Clone the repository
git clone https://github.com/resque/resque.git
cd resque
# Install dependencies
bundle install
# Install the gem locally
rake install
Adding Resque to Your Application
Add Resque to your Gemfile:
gem 'resque'
Then install:
bundle install
3. Configuration
Redis Configuration
Resque uses Redis for data persistence. Configure your Redis connection in your application:
# In an initializer or configuration file
require 'resque'
Resque.redis = Redis.new(host: 'localhost', port: 6379)
Environment Variables
| Variable | Description |
|---|---|
REDIS_URL | Redis connection URL (optional, alternative to direct Redis configuration) |
QUEUES | Comma-separated list of queues to process (for workers) |
Worker Configuration
Configure worker behavior using these attributes:
Resque::Worker.new(*queues).tap do |worker|
worker.term_timeout = 5 # Timeout for graceful shutdown
worker.term_child = true # Use new kill child logic
worker.graceful_term = true # Make SIGTERM work like SIGQUIT
worker.run_at_exit_hooks = true # Run at_exit handlers in forked workers
end
4. Build & Run
Running Resque Web Interface
# Start the Resque web interface
resque-web
# Or with custom options
resque-web -p 5678 -h localhost
Starting Workers
# Start a worker for specific queues
QUEUE=high,low,medium rake resque:work
# Start multiple workers
COUNT=5 QUEUE=* rake resque:workers
# Start a worker with verbose logging
VERBOSE=1 QUEUE=* rake resque:work
Creating and Enqueuing Jobs
# Define a job class
class MyJob
@queue = :high
def self.perform(*args)
# Your job logic here
puts "Processing job with args: #{args.inspect}"
end
end
# Enqueue a job
Resque.enqueue(MyJob, 'arg1', 'arg2', 3)
Job Processing
# Reserve and process a job
job = Resque::Job.reserve(:high)
klass = Resque::Job.constantize(job.payload['class'])
klass.perform(*job.payload['args'])
5. Deployment
Platform Recommendations
Heroku:
# Add Redis add-on
heroku addons:create heroku-redis:hobby-dev
# Deploy your application
git push heroku main
# Scale workers
heroku scale worker=2
Docker:
# Dockerfile
FROM ruby:3.1
RUN gem install resque
# Add your application code
COPY . /app
WORKDIR /app
# Start worker
CMD ["rake", "resque:work", "QUEUE=*"]
Kubernetes:
# worker-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: resque-worker
spec:
replicas: 3
template:
spec:
containers:
- name: resque-worker
image: your-ruby-app
command: ["rake", "resque:work"]
env:
- name: QUEUE
value: "*"
Production Considerations
- Use Redis with persistence enabled (RDB/AOF)
- Monitor worker memory usage and restart if needed
- Set up proper logging and monitoring
- Configure graceful shutdown timeouts
- Use Redis Sentinel or Cluster for high availability
6. Troubleshooting
Common Issues and Solutions
Problem: Workers not processing jobs
# Check Redis connection
redis-cli ping
# Verify queues exist
Resque.queues
# Check worker status
Resque.workers
Problem: Memory leaks in workers
# Configure worker to exit after N jobs
Resque::Worker.after_fork do |job|
if job.attempts > 100
exit! # Force exit without at_exit hooks
end
end
Problem: Jobs failing silently
# Enable verbose logging
VERBOSE=1 QUEUE=* rake resque:work
# Check failed jobs
Resque::Failure.all
Problem: Redis connection issues
# Configure retry logic
Resque.redis = Redis.new(
host: 'localhost',
port: 6379,
retry_timeout: 10,
reconnect_attempts: 5
)
Problem: Jobs not being enqueued
# Verify job class is defined
defined?(MyJob) # => true
# Check queue name
MyJob.instance_variable_get(:@queue) # => :high
# Test enqueue manually
Resque.enqueue(MyJob, 'test')
Problem: Workers not starting
# Check port conflicts
lsof -i :5678
# Verify Redis is running
redis-cli info
# Check worker logs
tail -f log/resque.log
Debugging Tips
- Use
resque-webto monitor job queues and failures - Enable
VERBOSE=1for detailed logging - Check Redis keyspace for job data
- Use
Resque.peek(queue, start, count)to inspect queued jobs - Monitor worker heartbeats with
Resque::Worker.all_heartbeats