通过Capistrano任务启动多个具有/特定队列的延迟作业工作者

时间:2012-03-01 21:12:56

标签: ruby-on-rails ruby ruby-on-rails-3 delayed-job

我正在研究使用delayed_job的队列。我发现this page概述了各种启动工人的方法,但是我想保留目前的Capistrano方法:

set :delayed_job_args, "-n 2 -p ecv2.production"
after "deploy:start",  "delayed_job:start"
...

我想知道如何修改delayed_job_args来处理具有特定队列的产生1工作者,以及为每个其他工作处理1个工作者。到目前为止,我所有的工作都是如此:

namespace :delayed_job do
  task :restart, :roles => :app do
    run "cd #{current_path}; RAILS_ENV=#{rails_env} script/delayed_job -p ecv2.production --queue=export restart"
    run "cd #{current_path}; RAILS_ENV=#{rails_env} script/delayed_job -p ecv2.production restart"
  end
end

......但那没什么好玩的。有什么建议吗?

2 个答案:

答案 0 :(得分:5)

我将我的作业拆分为两个队列,其中一个工作人员与deploy.rb文件中的此设置隔离到每个队列:

namespace :delayed_job do
  task :start, roles: :app do
    run "cd #{current_path}; #{rails_env} bundle exec script/delayed_job -i queue_one --queue=one start"
    run "cd #{current_path}; #{rails_env} bundle exec script/delayed_job -i queue_two --queue=two start"
  end

  task :stop, roles: :app do
    run "cd #{current_path}; #{rails_env} bundle exec script/delayed_job -i queue_one --queue=one stop"
    run "cd #{current_path}; #{rails_env} bundle exec script/delayed_job -i queue_two --queue=two stop"
  end

  task :restart, roles: :app do
    run "cd #{current_path}; #{rails_env} bundle exec script/delayed_job -i queue_one --queue=one restart"
    run "cd #{current_path}; #{rails_env} bundle exec script/delayed_job -i queue_two --queue=two restart"
  end
end

命令的-i name部分非常重要。这是允许多个delayed_job实例运行的部分。

如果你想将工作人员添加到特定队列,那么你可以像这样扩展它们(我在队列1中只有两个工人,而在队列2上只有一个工人):

namespace :delayed_job do
  task :start, roles: :app do
    run "cd #{current_path}; #{rails_env} bundle exec script/delayed_job -i one1 --queue=one start"
    run "cd #{current_path}; #{rails_env} bundle exec script/delayed_job -i one2 --queue=one start"
    run "cd #{current_path}; #{rails_env} bundle exec script/delayed_job -i two --queue=two start"
  end

  task :stop, roles: :app do
    run "cd #{current_path}; #{rails_env} bundle exec script/delayed_job -i one1 stop"
    run "cd #{current_path}; #{rails_env} bundle exec script/delayed_job -i one2 stop"
    run "cd #{current_path}; #{rails_env} bundle exec script/delayed_job -i two stop"
  end

  task :restart, roles: :app do
    run "cd #{current_path}; #{rails_env} bundle exec script/delayed_job -i one1 --queue=one restart"
    run "cd #{current_path}; #{rails_env} bundle exec script/delayed_job -i one2 --queue=one restart"
    run "cd #{current_path}; #{rails_env} bundle exec script/delayed_job -i two --queue=two restart"
  end
end

答案 1 :(得分:4)

经过一番乱搞,我发现的技巧是恢复'set:delayed_job_args'并使用--queues =(复数)而不是--queue =(单数)。希望这可以帮助遇到同样问题的其他人。

set :delayed_job_args, "-n 2 -p ecv2.production --queues=cache,export"

更新:我现在正在使用的内容......

after "deploy:stop",    "delayed_job:stop"
after "deploy:start",   "delayed_job:start"
after "deploy:restart", "delayed_job:restart"

namespace :delayed_job do
  # See 'man nice' for details, default priority is 10 and 15 is a bit lower
  task :start, :roles => :app do
    run "cd #{current_path}; #{rails_env} nice -n 15 ruby script/delayed_job -n 1 -p yourapp.#{application} start"
  end

  task :restart, :roles => :app do
    stop
    start
  end
end