在后台运行resque

Mar*_*kus 22 daemon background ruby-on-rails resque ruby-on-rails-3

我有一个工作轨道应用程序与resque队列系统工作得很好.但是,我缺乏一种实际上妖魔化resque工人的好方法.

我可以通过rake resque来启动它们:work QUEUE ="*"但我想这不是你应该让你的工人在前台运行的重点.出于某种原因,没有人似乎在解决这个问题.在官方resque github页面上声明你可以做这样的事情:

PIDFILE=./resque.pid BACKGROUND=yes QUEUE="*" rake resque:work
Run Code Online (Sandbox Code Playgroud)

好吧 - 它至少没有在这里进入背景.

And*_*ggs 15

resque-pool的+1 - 真的很棒.我们将它与上帝结合使用,以确保它始终可用.

# Resque
God.watch do |w|

  w.dir = RAILS_ROOT

  w.name = "resque-pool"
  w.interval = 30.seconds
  w.start = "cd #{RAILS_ROOT} && sudo -u www-data sh -c 'umask 002 && resque-pool -d -E #{RAILS_ENV}'"
  w.start_grace = 20.seconds
  w.pid_file = "#{RAILS_ROOT}/tmp/pids/resque-pool.pid"

  w.behavior(:clean_pid_file)

  # restart if memory gets too high
  #w.transition(:up, :restart) do |on|
  #  on.condition(:memory_usage) do |c|
  #    c.above = 350.megabytes
  #    c.times = 2
  #  end
  #end

  # determine the state on startup
  w.transition(:init, { true => :up, false => :start }) do |on|
    on.condition(:process_running) do |c|
      c.running = true
    end
  end

  # determine when process has finished starting
  w.transition([:start, :restart], :up) do |on|
    on.condition(:process_running) do |c|
      c.running = true
      c.interval = 5.seconds
    end

    # failsafe
    on.condition(:tries) do |c|
      c.times = 5
      c.transition = :start
      c.interval = 5.seconds
    end
  end

  # start if process is not running
  w.transition(:up, :start) do |on|
    on.condition(:process_running) do |c|
      c.running = false
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

这为您提供了一种非常优雅的方式来重新加载工作中的代码而不会中断作业 - 只需kill -2在部署时使用resque-pool.空闲工人会立即死亡,繁忙的工人将在他们完成当前工作时死亡,上帝将使用您的新代码重新启动resque-pool.

这些是我们对Capistrano的Resque任务:

namespace :resque do

  desc "Starts resque-pool daemon."
  task :start, :roles => :app, :only => { :jobs => true } do
    run "cd #{current_path};resque_pool -d -e #{rails_env} start"
  end

  desc "Sends INT to resque-pool daemon to close master, letting workers finish their jobs."
  task :stop, :roles => :app, :only => { :jobs => true } do
    pid = "#{current_path}/tmp/pids/resque-pool.pid"
    sudo "kill -2 `cat #{pid}`"
  end

  desc "Restart resque workers - actually uses resque.stop and lets God restart in due course."
  task :restart, :roles => :app, :only => { :jobs => true } do
    stop # let God restart.
  end

  desc "List all resque processes."
  task :ps, :roles => :app, :only => { :jobs => true } do
    run 'ps -ef f | grep -E "[r]esque-(pool|[0-9])"'
  end

  desc "List all resque pool processes."
  task :psm, :roles => :app, :only => { :jobs => true } do
    run 'ps -ef f | grep -E "[r]esque-pool"'
  end

end
Run Code Online (Sandbox Code Playgroud)

当resque-pool分支工作者时,您可能需要重新连接任何数据库连接 - 检查文档.


mbs*_*ikh 13

我有同样的问题,以下工作对我来说.

PIDFILE=./resque.pid BACKGROUND=yes QUEUE="*" rake resque:work >>  worker1.log &
Run Code Online (Sandbox Code Playgroud)

您还可以将STDERR重定向到同一日志文件.


spa*_*ovv 5

妖魔化一个过程你可以使用nohup:

nohup cmd &
Run Code Online (Sandbox Code Playgroud)

在resque的github上有一个monit的配置,它显示了如何使用nohup,它看起来像这样:

nohup bundle exec rake resque:work QUEUE=queue_name PIDFILE=tmp/pids/resque_worker_QUEUE.pid & >> log/resque_worker_QUEUE.log 2>&1
Run Code Online (Sandbox Code Playgroud)