ruby on rails在部署到服务器时停止resque worker

时间:2013-07-28 18:13:23

标签: ruby capistrano resque

为什么在将代码部署到服务器时需要停止resque worker?

这是我的部署文件的一部分。我发现代码中可能存在错误:

namespace :resque do
  desc "Start resque workers"
  task :start do
    # Start two workers with separate run commands, so we can store their PIDs
    # Hacky, but works
    run "if [ ! -e #{deploy_to}/shared/pids/resque_production_1.pid ]; then cd #{deploy_to}/current && RAILS_ENV=production QUEUE=* PIDFILE=#{deploy_to}/shared/pids/resque_production_1.pid BACKGROUND=yes VERBOSE=1 bundle exec rake environment resque:work; fi;"
    run "if [ ! -e #{deploy_to}/shared/pids/resque_production_2.pid ]; then cd #{deploy_to}/current && RAILS_ENV=production QUEUE=* PIDFILE=#{deploy_to}/shared/pids/resque_production_2.pid BACKGROUND=yes VERBOSE=1 bundle exec rake environment resque:work; fi;"
  end

  desc "Stop resque workers"
  task :stop do
    run "if [ -e #{deploy_to}/shared/pids/resque_production_2.pid ]; then echo \"Killing Worker #1\"; kill -s QUIT `cat #{deploy_to}/shared/pids/resque_production_2.pid`; rm -f #{deploy_to}/shared/pids/resque_production_2.pid; echo \"Done\"; fi;"
    run "if [ -e #{deploy_to}/shared/pids/resque_production_2.pid ]; then echo \"Killing Worker #2\"; kill -s QUIT `cat #{deploy_to}/shared/pids/resque_production_2.pid`; rm -f #{deploy_to}/shared/pids/resque_production_2.pid; echo \"Done\"; fi;"
  end

似乎有一些错误:

# stop resque worker

/resque_production_2.pid

他们两个都杀了/resque_production_2.pid ......这意味着一个工人在部署期间没有被杀死......你认为这会导致任何问题......

因为我最近发现我的一个resque作业无法排入生产服务器的队列中。并且没有在列表中显示失败。可能由此造成的吗?。但它在登台服务器上工作正常。生产服务器中的其他resque作业也可以正常工作。这很奇怪。

1 个答案:

答案 0 :(得分:0)

在部署期间重新启动Resque worker,以确保在工作程序中运行最新版本的代码(通常是模型和工作程序)。无法重新启动worker意味着该worker正在运行上次启动的代码修订版;如果您对功能进行了重大更改,那么您可能会运行会破坏或以其他方式引入错误的代码,因为您当前的代码可能与其运行的代码版本不兼容。

顺便说一下,类似下面这样的表格将有助于防止将来出现这样的错误(并且只是通过增加上限来让你添加额外的工作者):

WORKER_COUNT = 2

task :start do
  # Start n workers with separate run commands, so we can store their PIDs
  1.upto(WORKER_COUNT) do |i|
    run "if [ ! -e #{deploy_to}/shared/pids/resque_production_#{i}.pid ]; then cd #{deploy_to}/current && RAILS_ENV=production QUEUE=* PIDFILE=#{deploy_to}/shared/pids/resque_production_#{i}.pid BACKGROUND=yes VERBOSE=1 bundle exec rake environment resque:work; fi;"
  end
end

desc "Stop resque workers"
task :stop do
  1.upto(WORKER_COUNT) do |i|
    run "if [ -e #{deploy_to}/shared/pids/resque_production_#{i}.pid ]; then echo \"Killing Worker #1\"; kill -s QUIT `cat #{deploy_to}/shared/pids/resque_production_#{i}.pid`; rm -f #{deploy_to}/shared/pids/resque_production_#{i}.pid; echo \"Done\"; fi;"
  end
end