【问题标题】:How to make Bluepill restart Resque workers only after reaching a safe status如何让 Bluepill 仅在达到安全状态后重启 Resque 工作人员
【发布时间】:2013-08-27 18:31:27
【问题描述】:

假设这是我的工人:

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 等到它到达“安全”块以重新启动或关闭。如何做到这一点?

【问题讨论】:

  • 您的长操作是否可以放入数据库事务中,这样如果被杀死,它们会使系统保持干净状态?不是您要寻找的答案,但也许这是另一种方法?
  • 你找到解决办法了吗?

标签: resque bluepill


【解决方案1】:

试试这个方法:

1) 通过在启动时设置 TERM_CHILDRESQUE_TERM_TIMEOUT 环境变量,使用 new_kill_child 方法在 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 seconds

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

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

b) 将 process.stop_signals 中的 Bluepill TERM 超时配置为比 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2012-01-25
    • 2015-06-14
    • 1970-01-01
    相关资源
    最近更新 更多