Skip to content

Instantly share code, notes, and snippets.

@guivinicius
Created March 7, 2014 21:44
Show Gist options
  • Select an option

  • Save guivinicius/9420832 to your computer and use it in GitHub Desktop.

Select an option

Save guivinicius/9420832 to your computer and use it in GitHub Desktop.

Revisions

  1. guivinicius created this gist Mar 7, 2014.
    56 changes: 56 additions & 0 deletions unicorn.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    # See http://unicorn.bogomips.org/Unicorn/Configurator.html for complete
    # documentation.

    worker_processes <%= fetch(:unicorn_workers) %>

    listen "/tmp/unicorn.<%= fetch(:application) %>.sock", :backlog => 64
    # listen 8080, :tcp_nopush => true

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

    timeout 30

    pid "<%= fetch(:unicorn_pid) %>"

    check_client_connection false

    working_directory "<%= current_path %>"

    user 'rails', 'admin'

    stderr_path "<%= fetch(:unicorn_log) %>"
    stdout_path "<%= fetch(:unicorn_log) %>"

    before_fork do |server, worker|

    if defined?(ActiveRecord::Base)
    ActiveRecord::Base.connection.disconnect!
    end

    old_pid = "#{server.config[:pid]}.oldbin"
    if old_pid != server.pid
    begin
    sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
    Process.kill(sig, File.read(old_pid).to_i)
    rescue Errno::ENOENT, Errno::ESRCH
    end
    end

    sleep 1
    end

    after_fork do |server, worker|
    if defined?(ActiveRecord::Base)
    ActiveRecord::Base.establish_connection
    end
    child_pid = server.config[:pid].sub(".pid", ".#{worker.nr}.pid")
    system("echo #{Process.pid} > #{child_pid}")

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