3

假设这是我的工人:

class FooWorker
  @queue = :foo

  def self.perform
    User.all.each do |u|
      ...
      Do Long Operations (Unsafe to kill)
      ...

      # Here it's safe to break the worker and restart
    end
  end
end

我正在使用 Resque Scheduler 对此进行排队,这是我的 Bluepill conf:

...
app.process(process_name) do |process|
  process.group         = "resque"
  process.start_command = "rake environment resque:work QUEUE=foo RAILS_ENV=production"
  ...
  process.stop_signals  = [:quit, 5.seconds, :term, 1.minute, :kill]
  process.daemonize     = true

  process.start_grace_time = 30.seconds
  process.stop_grace_time  = 80.seconds

  process.monitor_children do |child_process|
    child_process.stop_command = "kill -QUIT {{PID}}"

    child_process.checks :mem_usage, :every => 30.seconds, :below => 500.megabytes, :times => [3,4], :fires => :stop
  end
end
....

我想让 Bluepill 或 Resque 等到它到达“安全”块才能重新启动或关闭。如何做到这一点?

4

1 回答 1

0

试试这种方式:

new_kill_child1)通过在启动时设置TERM_CHILDRESQUE_TERM_TIMEOUTenv变量来设置resque以在TERM / INT上优雅地杀死孩子:

process.start_command = "rake environment resque:work QUEUE=foo RAILS_ENV=production TERM_CHILD=1 RESQUE_TERM_TIMEOUT=20.0"

RESQUE_TERM_TIMEOUT默认值为4 秒

这将使 resque 向孩子发送 TERM 信号,等待RESQUE_TERM_TIMEOUT,如果孩子仍在运行,则将其杀死。务必

a)将此超时设置为足够大以使您的关键部分结束,

b) 将 Bluepill TERM timeout in 配置process.stop_signals为比RESQUE_TERM_TIMEOUT在等待子进程结束临界区时不杀死 worker 稍大一些。

2)处理子进程中的TERM信号以优雅地停止:

class FooWorker
  class << self
    attr_accessor :stop
  end

  @queue = :foo
  def self.perform
    User.all.each do |u|
      ...
      Do Long Operations (Unsafe to kill)
      ...

      # Here it's safe to break the worker and restart
      return if FooWorker.stop
    end
  end
end

trap('TERM') do
  FooWorker.stop = true
end
于 2013-09-22T11:13:12.007 回答