【发布时间】:2014-11-15 09:30:12
【问题描述】:
这是我第一次使用God gem。当我运行命令god terminate 时,它不会关闭现有任务。所以下次我再次运行上帝脚本时,它只会翻倍。
我看到了 Resque 用于终止 Rogue 生成的示例脚本,但不完全了解如何修改我的脚本。
我运行的命令
god -c config/resque.god
god -c config/stale.god
我的 resque.god 文件:
RAILS_ROOT = File.dirname(File.dirname(__FILE__))
God.watch do |w|
w.name = "android_msg"
w.group = "resque-group"
w.interval = 30.seconds
w.start = "cd #{RAILS_ROOT} && rake resque:work QUEUE='android_message'"
w.start_grace = 10.seconds
# 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
God.watch do |w|
w.name = "apn_sender"
w.group = "resque-group"
w.interval = 30.seconds
w.start = "cd #{RAILS_ROOT} && rake apn:sender"
w.start_grace = 10.seconds
# 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
陈旧的神
# This will ride alongside god and kill any rogue stale worker
# processes. Their sacrifice is for the greater good.
WORKER_TIMEOUT = 60 * 10 # 10 minutes
Thread.new do
loop do
begin
`ps -e -o pid,command | grep [r]esque`.split("\n").each do |line|
parts = line.split(' ')
next if parts[-2] != "at"
started = parts[-1].to_i
elapsed = Time.now - Time.at(started)
if elapsed >= WORKER_TIMEOUT
::Process.kill('USR1', parts[0].to_i)
end
end
rescue
# don't die because of stupid exceptions
nil
end
sleep 30
end
end
【问题讨论】: