【问题标题】:God - starting new process while existing process is still stopping上帝 - 在现有流程仍在停止时启动新流程
【发布时间】:2012-03-11 21:25:41
【问题描述】:

使用上帝 (godrb.com) 我正在尝试编写一个配方,在部署应用程序时,无论现有进程的状态如何,它都会启动一个新进程。现有进程需要长时间运行超时才能完成当前任务,但新进程应立即使用新部署的代码启动。

我现在拥有的设置停止时的超时时间为 300 秒,但在启动新进程之前会等待整整 300 秒。

God.watch do |w|
  w.name = "sidekiq"
  w.interval = 30.seconds
  w.start = "bash -lc 'cd /path/to/current && ./bin/sidekiq -P /path/to/shared/pids/sidekiq.pid'"
  w.stop  = "bash -lc 'kill -USR1 `cat /path/to/shared/pids/sidekiq.pid`'"
  w.stop_timeout = 300.seconds
  w.pid_file = "/path/to/shared/pids/sidekiq.pid"
  w.behavior(:clean_pid_file)
end

在这种情况下,kill -USR1 告诉 sidekiq 完成所有当前作业的处理,但不再进行任何工作。

我想保持现有工作人员的 300 秒超时,但在运行 kill 命令后立即启动新进程。

【问题讨论】:

  • 你看过独角兽是怎么做到的吗?培养新工人并让这些新工人杀死旧工人是独角兽的责任。
  • 是的,但该功能是 Unicorn 的一部分。我正在尝试用任何通用流程完成类似的事情。

标签: ruby monitoring god


【解决方案1】:

我认为你必须定义一些转换。

这是我的god.rb

# I'm using Rails
rails_env = ENV['RAILS_ENV'] || 'development'
rails_root = '/path/to/current'
pid_file = "#{rails_root}/tmp/pids/sidekiq.pid"

God.watch do |w|
  w.dir = rails_root
  w.name = "sidekiq"
  w.interval = 30.seconds
  w.env = {'RAILS_ENV' => rails_env, 'BUNDLE_GEMFILE' => "#{rails_root}/Gemfile"}
  w.uid = 'deployer'
  w.gid = 'staff'

  w.start = "cd #{rails_root}; bundle exec sidekiq -e #{rails_env} -C #{rails_root}/config/sidekiq.yml -i #{i} -P #{pid_file}&"
  w.stop = "cd #{rails_root}; bundle exec sidekiqctl stop #{pid_file} 10"
  w.restart = "#{w.stop} && #{w.start}"
  w.start_grace = 15.seconds
  w.restart_grace = 15.seconds
  w.pid_file = pid_file
  w.log = "#{rails_root}/log/sidekiq.log"

  # clean pid files before start if necessary
  w.behavior(:clean_pid_file)

  # 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
    end

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

  # start if process is not running
  w.transition(:up, :start) do |on|
    on.condition(:process_exits)
  end

  # lifecycle
  w.lifecycle do |on|
    on.condition(:flapping) do |c|
      c.to_state = [:start, :restart]
      c.times = 5
      c.within = 5.minute
      c.transition = :unmonitored
      c.retry_in = 10.minutes
      c.retry_times = 5
      c.retry_within = 2.hours
    end
  end
end

【讨论】:

    猜你喜欢
    • 2019-09-13
    • 2016-05-25
    • 2017-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-18
    • 2021-10-08
    • 1970-01-01
    相关资源
    最近更新 更多