我不知道你爲什麼把所有的工作對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
我使用該設置大部分時間和我的應用程序重新加載沒有任何問題,你可能已經擁有所有這些,因爲它大部分是默認配置,但是如果你錯過了一些東西,請試試看);
是的,'preload_app'設置爲true。偉大的信息!我唯一的問題是,識別舊的獨角獸進程ID是什麼是最好的方法? –
.oldbin或(old)會被附加到舊進程的名稱中,所以你可以將'ps'管道傳輸到'grep'來獲得獨角獸和舊版本 –
我使用更新編輯了OP。感謝您迄今爲止提供的幫助。 –