Created
March 7, 2012 08:19
-
-
Save somebox/1991880 to your computer and use it in GitHub Desktop.
unicorn.rb file to deal with dead processes/stale pid and knows about RVM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
APP_ROOT = File.expand_path(File.dirname(File.dirname(__FILE__))) | |
if ENV['MY_RUBY_HOME'] && ENV['MY_RUBY_HOME'].include?('rvm') | |
begin | |
rvm_path = File.dirname(File.dirname(ENV['MY_RUBY_HOME'])) | |
rvm_lib_path = File.join(rvm_path, 'lib') | |
$LOAD_PATH.unshift rvm_lib_path | |
require 'rvm' | |
RVM.use_from_path! APP_ROOT | |
rescue LoadError | |
raise "RVM ruby lib is currently unavailable." | |
end | |
end | |
ENV['BUNDLE_GEMFILE'] = File.expand_path('../Gemfile', File.dirname(__FILE__)) | |
require 'bundler/setup' | |
worker_processes 4 | |
working_directory APP_ROOT | |
preload_app true | |
timeout 30 | |
listen "localhost:8080" | |
# listen APP_ROOT + "/tmp/sockets/unicorn.sock", :backlog => 64 | |
pid APP_ROOT + "/tmp/pids/unicorn.pid" | |
stderr_path APP_ROOT + "/log/unicorn.stderr.log" | |
stdout_path APP_ROOT + "/log/unicorn.stdout.log" | |
old_pid = APP_ROOT + '/tmp/pids/unicorn.pid' | |
if File.exists?(old_pid) | |
puts "Found existing unicorn PID" | |
begin | |
pid = File.read(old_pid).to_i | |
File.delete(old_pid) | |
Process.kill("QUIT", pid) | |
puts "shut down previous unicorn (#{pid})" | |
rescue Errno::ENOENT, Errno::ESRCH | |
puts "was already dead" | |
end | |
end | |
puts "unicorn started" | |
before_fork do |server, worker| | |
defined?(ActiveRecord::Base) && ActiveRecord::Base.connection.disconnect! | |
end | |
after_fork do |server, worker| | |
defined?(ActiveRecord::Base) && ActiveRecord::Base.establish_connection | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment