Skip to content

Instantly share code, notes, and snippets.

@slothelle
Last active December 18, 2018 02:43
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save slothelle/50ca2c6d6f78656e267a to your computer and use it in GitHub Desktop.
Save slothelle/50ca2c6d6f78656e267a to your computer and use it in GitHub Desktop.
Deploying Rails 4 apps with Resque and Redis to Heroku using Unicorn with a Procfile.
# and whatever other gems you need
gem 'resque', '~> 1.24.1'
gem 'unicorn', '~> 4.6.2'
# lives in app root
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
resque: env TERM_CHILD=1 QUEUE=* bundle exec rake resque:work
# lives in config/initializers/redis.rb
# assumes you're using the Redis Cloud addon (NOT Redis TOGO)
if ENV["REDISCLOUD_URL"]
$redis = Resque.redis = Redis.new(:url => ENV["REDISCLOUD_URL"])
end
# lives in lib/tasks/resque.rake
require 'resque/tasks'
task "resque:preload" => :environment
# lives in config/unicorn.rb
worker_processes Integer(ENV["WEB_CONCURRENCY"] || 3)
timeout 15
preload_app true
before_fork do |server, worker|
Signal.trap 'TERM' do
puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
Process.kill 'QUIT', Process.pid
end
defined?(ActiveRecord::Base) and
ActiveRecord::Base.connection.disconnect!
if defined?(Resque)
Resque.redis.quit
Rails.logger.info('Disconnected from Redis')
end
end
after_fork do |server, worker|
Signal.trap 'TERM' do
puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT'
end
if defined?(Resque)
Rails.logger.info('Connected to Redis')
end
end
@dcangulo
Copy link

This solves my 6 hours problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment