Skip to content

Instantly share code, notes, and snippets.

@gregory
Forked from nathany/config.ru
Created June 4, 2016 20:05
Show Gist options
  • Save gregory/12b1ed83b1357e9ba20f4ad41806a6d6 to your computer and use it in GitHub Desktop.
Save gregory/12b1ed83b1357e9ba20f4ad41806a6d6 to your computer and use it in GitHub Desktop.

Revisions

  1. @nathany nathany revised this gist Oct 1, 2013. 2 changed files with 9 additions and 5 deletions.
    8 changes: 8 additions & 0 deletions config.ru
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    # add something like this to config.ru
    # see https://github.com/kzk/unicorn-worker-killer

    if Integer(ENV['UNICORN_KILLER'] || 0) != 0
    require 'unicorn/worker_killer'
    # Max memory size (RSS) per worker
    use Unicorn::WorkerKiller::Oom, (350*(1024**2)), (400*(1024**2)), 30, true
    end
    6 changes: 1 addition & 5 deletions unicorn.rb
    Original file line number Diff line number Diff line change
    @@ -53,9 +53,5 @@
    # different machine. (default 1024)
    # http://unicorn.bogomips.org/Unicorn/Configurator.html#method-i-listen
    # http://manpages.ubuntu.com/manpages/gutsy/man2/listen.2.html (max 128)
    listen ENV['PORT'], :backlog => Integer(ENV['UNICORN_BACKLOG'] || 128)
    listen ENV['PORT'], :backlog => Integer(ENV['UNICORN_BACKLOG'] || 15)

    # combine Ruby 2.0.0dev or REE with "preload_app true" for memory savings
    # http://rubyenterpriseedition.com/faq.html#adapt_apps_for_cow
    GC.respond_to?(:copy_on_write_friendly=) and
    GC.copy_on_write_friendly = true
  2. @nathany nathany created this gist Feb 28, 2013.
    61 changes: 61 additions & 0 deletions unicorn.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    # sample config:
    # http://unicorn.bogomips.org/examples/unicorn.conf.rb

    # may want Out-of-band GC
    # http://unicorn.bogomips.org/Unicorn/OobGC.html

    # and unicorn killer to avoid swapping
    # https://github.com/kzk/unicorn-worker-killer

    # lots of good comments here:
    # https://blog.heroku.com/archives/2013/2/27/unicorn_rails

    worker_processes Integer(ENV['UNICORN_WORKERS'] || 2)

    # Load your app into the master before forking
    # workers for super-fast worker spawn times
    preload_app true

    # Immediately restart any workers that haven't responded
    # Heroku will hangup after 30 seconds
    # https://devcenter.heroku.com/articles/rails-unicorn#timeouts
    timeout Integer(ENV['UNICORN_TIMEOUT'] || 15)

    # https://devcenter.heroku.com/articles/forked-pg-connections
    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 sent QUIT'
    end

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

    # Dalli does not need anything special in Unicorn/Passenger since 2.0.4. It will
    # detect sockets shared with child processes and gracefully reopen the socket.
    # https://github.com/mperham/dalli/issues/208

    # Redis/Resque and other external services may need special handling:
    # https://devcenter.heroku.com/articles/rails-unicorn
    end

    # If you are running unicorn on multiple machines, lowering this number can help
    # your load balancer detect when a machine is overloaded and give requests to a
    # different machine. (default 1024)
    # http://unicorn.bogomips.org/Unicorn/Configurator.html#method-i-listen
    # http://manpages.ubuntu.com/manpages/gutsy/man2/listen.2.html (max 128)
    listen ENV['PORT'], :backlog => Integer(ENV['UNICORN_BACKLOG'] || 128)

    # combine Ruby 2.0.0dev or REE with "preload_app true" for memory savings
    # http://rubyenterpriseedition.com/faq.html#adapt_apps_for_cow
    GC.respond_to?(:copy_on_write_friendly=) and
    GC.copy_on_write_friendly = true