2012-12-15 124 views
0

我剛剛切換到在獨角獸上部署我的Rails項目。在我Capistrano的配方deploy:restart爲什麼unicorn.pid被更新,但進程不會重新啓動?

task :restart, :except => { :no_release => true } do 
    run "cd #{shared_path}/pids && kill -s USR2 `cat unicorn.pid`" 
end 

但是從我不時發現,有時候,部署過程成功完成,不過雖然unicorn.pid更新,麒麟過程保持靜止。

例如,我部署在12月15日,並且監測到unicorn.pid已更新,但是如果我運行ps -ef | grep unicorn,我可以看到獨角獸進程仍在我上次部署時啓動,所以它指的是最後一個發佈文件夾造成麻煩。

這是爲什麼?

以下是我的unicorn.rb文件:

env = ENV["RAILS_ENV"] 

case env 
when 'pre', 'production' 
    @app_path = '/home/deployer/deploy/myproject' 
end 

if env == 'pre' || env == 'production' 
    user 'deployer', 'staff' 
    shared_path = "#{@app_path}/shared" 
    stderr_path "#{shared_path}/log/unicorn.stderr.log" 
    stdout_path "#{shared_path}/log/unicorn.stdout.log" 

    if env == 'production' 
    worker_processes 6 
    else 
    worker_processes 2 
    end 

    working_directory "#{@app_path}/current" # available in 0.94.0+ 

    listen "/tmp/myproject.sock", :backlog => 64 


    timeout 30 

    pid "#{shared_path}/pids/unicorn.pid" 


    preload_app true 
    GC.respond_to?(:copy_on_write_friendly=) and 
    GC.copy_on_write_friendly = true 

    before_fork do |server, worker| 

    defined?(ActiveRecord::Base) and 
    ActiveRecord::Base.connection.disconnect! 

    old_pid = "#{shared_path}/pids/unicorn.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| 
    defined?(ActiveRecord::Base) and 
    ActiveRecord::Base.establish_connection 
    end 
end 

回答

0

我不知道爲什麼是這樣的情況,但個人通過Capistrano的部署時,我不殺麒麟PID。

相反,我通過god gem啓動獨角獸,並配置god文件,以便在文件被觸摸時重新啓動獨角獸。

當我通過Capistrano的部署,我再輕觸文件中的每個部署後,迫使麒麟進程重新啓動

namespace :deploy do 
    desc "Touch restart.txt, which will restart all God processes" 
    task :restart do 
    run "touch #{ current_path }/tmp/restart.txt" 
end 

您需要將此模塊添加到文件。上帝太:

module God 
    module Conditions 
    class RestartFileTouched < PollCondition 
     attr_accessor :restart_file 
     def initialize 
     super 
     end 

     def process_start_time 
     Time.parse(`ps -o lstart -p #{self.watch.pid}`) 
     end 

     def restart_file_modification_time 
     File.mtime(self.restart_file) 
     end 

     def valid? 
     valid = true 
     valid &= complain("Attribute 'restart_file' must be specified", self) if self.restart_file.nil? 
     valid 
     end 

     def test 
     process_start_time < restart_file_modification_time 
     end 
    end 
    end 
end 
+0

感謝Hisoka,所以這樣上帝照顧所有人,我仍然得到獨角獸pre_load應用程序的好處? – larryzhao

+0

是的,基本上就是這樣。 –

相關問題