我不确定你为什么要把所有的工作都放在 capistrano 上,通常在 capistrano 上这样的东西就足够了:
set :unicorn_pid, "unicorn.my_website.pid"
desc "Zero-downtime restart of Unicorn"
task :restart, roles: :app do
if remote_file_exists?("/tmp/#{unicorn_pid}")
puts "Killing /tmp/#{unicorn_pid}"
run "kill -s USR2 `cat /tmp/#{unicorn_pid}`"
else
run "cd #{current_path} ; RAILS_ENV=#{rails_env} bundle exec unicorn_rails -c #{unicorn_config} -D"
end
end
然后真正的杀死独角兽老进程的代码其实是在独角兽配置上
# config/unicorn.rb
# Set environment to development unless something else is specified
env = ENV["RAILS_ENV"] || "production"
# See http://unicorn.bogomips.org/Unicorn/Configurator.html for complete documentation.
worker_processes 2 # amount of unicorn workers to spin up
APP_PATH = "/u/apps/my_website/current"
listen "/tmp/my_website.socket"
preload_app true
timeout 30 # restarts workers that hang for 30 seconds
pid "/tmp/unicorn.my_website.pid"
# By default, the Unicorn logger will write to stderr.
# Additionally, ome applications/frameworks log to stderr or stdout,
# so prevent them from going to /dev/null when daemonized here:
stderr_path APP_PATH + "/log/unicorn.stderr.log"
stdout_path APP_PATH + "/log/unicorn.stdout.log"
if env == "production"
# Help ensure your application will always spawn in the symlinked
# "current" directory that Capistrano sets up.
working_directory APP_PATH
# feel free to point this anywhere accessible on the filesystem
user 'deploy', 'deploy' # 'user', 'group'
shared_path = "/u/apps/my_website/shared"
stderr_path "#{shared_path}/log/unicorn.stderr.log"
stdout_path "#{shared_path}/log/unicorn.stdout.log"
end
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
# 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.
#
# This enables 0 downtime deploys.
old_pid = "/tmp/unicorn.my_website.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|
# Unicorn master loads the app then forks off workers - because of the way
# Unix forking works, we need to make sure we aren't using any of the parent's
# sockets, e.g. db connection (since "preload_app true")
if defined?(ActiveRecord::Base)
ActiveRecord::Base.establish_connection
end
# if preload_app is true, then you may also want to check and
# restart any other shared sockets/descriptors such as Memcached,
# and Redis. TokyoCabinet file handles are safe to reuse
# between any number of forked children (assuming your kernel
# correctly implements pread()/pwrite() system calls)
end
我大部分时间都使用该设置,并且我的应用程序重新加载没有任何问题,您可能已经拥有所有这些,因为它主要是默认配置,但是如果您缺少某些东西,请尝试一下;)