worker_processes <%= fetch(:unicorn_worker_processes) %>
timeout <%= fetch(:unicorn_timeout) %>
app_path = "<%= fetch(:deploy_to) %>"
listen "<%= fetch(:unicorn_sock_path) %>"
pid "<%= fetch(:unicorn_pid_path) %>"

stderr_path "<%= fetch(:unicorn_stderr_path) %>"
stdout_path "<%= fetch(:unicorn_stdout_path) %>"

preload_app true

# use correct Gemfile on restarts
before_exec do |server|
  ENV['BUNDLE_GEMFILE'] = "#{app_path}/current/Gemfile"
end

preload_app true

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
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.connection.disconnect!
  end

  # Before forking, kill the master process that belongs to the .oldbin PID.
  # This enables 0 downtime deploys.
  old_pid = "#{server.config[: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