Skip to content

Instantly share code, notes, and snippets.

@lucashungaro
Created October 31, 2013 16:36
Show Gist options
  • Save lucashungaro/7252835 to your computer and use it in GitHub Desktop.
Save lucashungaro/7252835 to your computer and use it in GitHub Desktop.
Unicorn + Heroku + worker_killer + GC vars tweak
# This file is used by Rack-based servers to start the application.
# GC_FREQUENCY = 8
# require "unicorn/oob_gc"
# GC.disable # Don't run GC during requests
# use Unicorn::OobGC, GC_FREQUENCY # Only GC once every GC_FREQUENCY requests
# # Unicorn self-process killer
require "unicorn/worker_killer"
# # Max memory size (RSS) per worker
use Unicorn::WorkerKiller::Oom, (170*(1024**2)), (180*(1024**2))
require ::File.expand_path("../config/environment", __FILE__)
# remaining lines omitted
worker_processes Integer(ENV["WEB_CONCURRENCY"] || 3)
timeout Integer(ENV["UNICORN_TIMEOUT"] || 30)
preload_app true
listen ENV["PORT"], :backlog => Integer(ENV["UNICORN_BACKLOG"] || 1)
before_exec do |server|
ENV["RUBY_HEAP_MIN_SLOTS"]=800000
ENV["RUBY_GC_MALLOC_LIMIT"]=59000000
ENV["RUBY_HEAP_SLOTS_INCREMENT"]=10000
ENV["RUBY_HEAP_SLOTS_GROWTH_FACTOR"]=1
ENV["RUBY_HEAP_FREE_MIN"]=100000
end
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!
defined?(Resque) and
Resque.redis.quit
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?(ActiveRecord::Base)
config = Rails.application.config.database_configuration[Rails.env]
config["reaping_frequency"] = ENV["DB_REAP_FREQ"] || 10 # seconds
config["pool"] = ENV["DB_POOL"] || 6
ActiveRecord::Base.establish_connection(config)
end
defined?(Resque) and
Resque.redis = ENV["REDISTOGO_URL"]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment