root=File.expand_path(File.dirname(__FILE__) + '/..')
#shared_dir="#{root}/shared"
working_directory root
pid "#{root}/pids/unicorn.nemo.pid"
stderr_path  "#{root}/log/unicorn.stderr.log"
stdout_path  "#{root}/log/unicorn.stdout.log"
listen "#{root}/sockets/unicorn.nemo.sock", :backlog => 64
listen(8081, backlog: 64) if ENV['RAILS_ENV'] == 'production'
worker_processes 2
preload_app true
timeout 30


before_fork do |server, worker|
  # the following is highly recomended for Rails + "preload_app true"
  # as there's no need for the master process to hold a connection
  defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!

  ##
  # When sent a USR2, Unicorn will suffix its pidfile with .oldbin and
  # immediately start loading up a new version of itself (loaded with a new
  # version of our app). When this new Unicorn is completely loaded
  # it will begin spawning workers. The first worker spawned will check to
  # see if an .oldbin pidfile exists. If so, this means we've just booted up
  # a new Unicorn and need to tell the old one that it can now die. To do so
  # we send it a QUIT.
  #
  # Using this method we get 0 downtime deploys.

  old_pid = "#{root}/pids/unicorn.nemo.pid.oldbin"
  if File.exists?(old_pid) && server.pid != old_pid
    begin
      Process.kill("QUIT", File.read(old_pid).to_i)
    rescue Errno::ENOENT, Errno::ESRCH
      # someone else did our job for us
    end
  end
end

