Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring Unicorn & Sidekiq correctly on Heroku

I was getting ActiveRecord::StatementInvalid (PG::Error: SSL error: decryption failed or bad record mac errors so I followed this guide about deploying Unicorn to Heroku and it seems to have fixed it. However under caveats it shows how to configure Resque for such a setup - would I have to do something similar with Sidekiq?

Sample code from Heroku:

before_fork do |server, worker|

  ...

  # If you are using Redis but not Resque, change this
  if defined?(Resque)
    Resque.redis.quit
    Rails.logger.info('Disconnected from Redis')
  end
end

after_fork do |server, worker|

  ...

  # If you are using Redis but not Resque, change this
  if defined?(Resque)
    Resque.redis = ENV['REDIS_URI']
    Rails.logger.info('Connected to Redis')
  end
end

This is what I currently have set up:

config/unicorn.rb

worker_processes 2
timeout 30
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!
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
  defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
end

config/initializers/sidekiq.rb

require 'sidekiq'

Sidekiq.configure_client do |config|
  config.redis = { :size => 1 }
end

Sidekiq.configure_server do |config|
  config.redis = { :size => 6 }
end

Procfile

web: bundle exec unicorn -p $PORT -E $RACK_ENV -c ./config/unicorn.rb
worker: bundle exec sidekiq -e production -c 4
like image 399
mind.blank Avatar asked Mar 23 '13 07:03

mind.blank


2 Answers

As of version 2.9.0 of sidekiq, no configuration is necessary in unicorn/passenger after_fork.

Here are the release notes for version 2.9.0 with the issue mentioned.

Here's the resolved issue that addresses forked connections.

Finally, here's a comment from the maintainer confirming that configuration in after_fork is no longer required.

like image 152
jerhinesmith Avatar answered Oct 24 '22 22:10

jerhinesmith


This is what I have and it works:

config/unicorn.rb

worker_processes Integer(ENV["WEB_CONCURRENCY"] || 5)
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!
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

  defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection

  Sidekiq.configure_client do |config|
    config.redis = { size: 1, namespace: 'sidekiq' }
  end
end

config/initializers/sidekiq.rb

ENV["REDIS_URL"] ||= "redis://localhost:6379"

Sidekiq.configure_server do |config|
  config.redis = { url: ENV["REDIS_URL"], namespace: 'sidekiq' }
end

unless Rails.env.production?
  Sidekiq.configure_client do |config|
    config.redis = { url: ENV["REDIS_URL"], namespace: 'sidekiq'  }
  end
end

Note: I use thin on development and unicorn on heroku.

like image 20
siong1987 Avatar answered Oct 24 '22 20:10

siong1987