resque - 如何让 Bluepill 仅在达到安全状态后才重启 Resque worker

标签 resque bluepill

假设这是我的 worker :

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 等到它到达“安全” block 以重新启动或关闭。如何实现?

最佳答案

这样试试:

1) 使用 new_kill_child 设置 resque 以在 TERM/INT 上优雅地杀死 child 通过在启动时设置 TERM_CHILDRESQUE_TERM_TIMEOUT env 变量的方法:

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 向 child 发送 TERM 信号,等待 RESQUE_TERM_TIMEOUT,如果 child 仍在运行,则终止它。一定要

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

关于resque - 如何让 Bluepill 仅在达到安全状态后才重启 Resque worker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18424290/

相关文章:

bundler - 使用 bluepill 监控 bundle 执行 unicorn_rails

ruby-on-rails - 讨论 bluepill 使用 rbenv

ruby-on-rails - Sidekiq 不断重启 Cloud66

python - 如何获取 rq 队列中的作业数?

ruby-on-rails - 启动 Rails 服务器时未初始化的常量 Resque::Helpers

ruby-on-rails - Rails:after_create中的异常停止保存

postgresql - 多个无法从客户端接收数据 : Connection reset by peer Postgresql and Resque

ruby-on-rails - 如何使用 Resque::Failure 配置 Bugsnag?