Skip to content

Instantly share code, notes, and snippets.

@steverob
Created April 7, 2016 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steverob/b83e41bb49d78f9aa32f79136df5af5f to your computer and use it in GitHub Desktop.
Save steverob/b83e41bb49d78f9aa32f79136df5af5f to your computer and use it in GitHub Desktop.
My App unicorn.rb
app_path = "/u/apps/app_production"
# Set unicorn options
worker_processes 8
preload_app true
timeout 30
listen "#{app_path}/shared/tmp/sockets/unicorn.sock", :backlog => 2048
# Spawn unicorn master worker for user apps (group: apps)
user 'deploy', 'deploy'
# Fill path to your app
working_directory "#{app_path}/current"
# Should be 'production' by default, otherwise use other env
rails_env = ENV['RAILS_ENV'] || 'production'
# Log everything to one file
stderr_path "log/unicorn.log"
stdout_path "log/unicorn.log"
# Set master PID location
pid "#{app_path}/shared/tmp/pids/unicorn.pid"
before_exec do |server|
Dotenv.overload
ENV["BUNDLE_GEMFILE"] = "#{app_path}/current/Gemfile"
end
before_fork do |server, worker|
ActiveRecord::Base.connection.disconnect! if defined? ActiveRecord::Base
sleep 10
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
after_fork do |server, worker|
ActiveRecord::Base.establish_connection if defined? ActiveRecord::Base
if defined?(EventMachine)
unless EventMachine.reactor_running? && EventMachine.reactor_thread.alive?
if EventMachine.reactor_running?
EventMachine.stop_event_loop
EventMachine.release_machine
EventMachine.instance_variable_set("@reactor_running",false)
end
Thread.new { EventMachine.run }
end
end
Signal.trap("INT") { EventMachine.stop }
Signal.trap("TERM") { EventMachine.stop }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment