我正在监视上帝的红宝石计划。当ruby程序退出时,我想等待10秒直到它再次启动。当我使用grace
时,在进程退出后,进程立即再次启动,但是上帝等待10秒的宽限期,直到它查看进程。当这个过程在恩典结束之前被杀死时,上帝不会再次捡起它并且这个过程永远不会重新启动。
我希望上帝等待10秒,直到退出后运行启动命令。我该怎么做?
我在transition
的{{1}}手表中尝试了:process_exits
,但我很难找到一种方法将等待时间设置在正确的位置。
编辑:在浏览了神的来源之后,我怀疑,一个可能的解决方案是添加一个自定义行为,它会在before_start
方法中等待。这听起来合理吗? (见下文)(完)
更多详情:
当我使用grace
中的watch
功能时,我会遇到这种情况:
INFO: Loading simple.god
INFO: Syslog enabled.
INFO: Using pid file directory: /Users/fsc/.god/pids
INFO: Started on drbunix:///tmp/god.17165.sock
INFO: simple_god move 'unmonitored' to 'init'
DEBUG: driver schedule #<God::Conditions::ProcessRunning:0x007fe134dee140> in 0 seconds
INFO: simple_god moved 'unmonitored' to 'init'
INFO: simple_god [trigger] process is not running (ProcessRunning)
DEBUG: simple_god ProcessRunning [false] {true=>:up, false=>:start}
INFO: simple_god move 'init' to 'start'
INFO: simple_god start: ruby .../simple.rb
DEBUG: driver schedule #<God::Conditions::ProcessRunning:0x007fe134dedb00> in 0 seconds
INFO: simple_god moved 'init' to 'start'
INFO: simple_god [trigger] process is running (ProcessRunning)
DEBUG: simple_god ProcessRunning [true] {true=>:up}
INFO: simple_god move 'start' to 'up'
INFO: simple_god registered 'proc_exit' event for pid 42498
INFO: simple_god moved 'start' to 'up'
我杀了这个过程。
INFO: simple_god [trigger] process 42498 exited (ProcessExits)
DEBUG: simple_god ProcessExits [true] {true=>:start}
INFO: simple_god move 'up' to 'start'
INFO: simple_god deregistered 'proc_exit' event for pid 42498
INFO: simple_god start: ruby .../simple.rb
这里宽限期开始。此时此过程已经开始。然而,神观察等待宽限期,直到它看着这个过程。
下一个日志行在上一个最后一个日志行之后10秒(宽限期)发生:
DEBUG: driver schedule #<God::Conditions::ProcessRunning:0x007fe134dedb00> in 0 seconds
INFO: simple_god moved 'up' to 'start'
INFO: simple_god [trigger] process is running (ProcessRunning)
DEBUG: simple_god ProcessRunning [true] {true=>:up}
INFO: simple_god move 'start' to 'up'
INFO: simple_god registered 'proc_exit' event for pid 42501
INFO: simple_god moved 'start' to 'up'
修改
自定义行为:
module God
module Behaviors
class WaitBehavior < Behavior
attr_accessor :delay
def initialize
super
self.delay = 10
end
def valid?
valid = true
valid
end
def before_start
if delay>0 then
sleep delay
end
end
def test
true
end
end
end
end
使用.god配置中的行为:
w.behavior(:wait_behavior)
答案 0 :(得分:1)
我认为它应该有用,WaitBehavior
类可以更短。
module God
module Behaviors
class WaitBehavior < Behavior
attr_accessor :delay
def before_start
sleep delay.to_i if delay.to_i > 0
end
end
end
end
<。>在.god配置中:
# .god
w.behavior(:wait_behavior) do |b|
b.delay = 10
end
与WaitBehavior
类似,我们可以定义StateFileBehavior
来触摸文件after_stop。
require 'fileutils'
module God
module Behaviors
class StateFileBehavior < Behavior
attr_accessor :file
def after_stop
FileUtils.touch file
end
end
end
end
并在.god
config
# .god
stop_timestamp_file = '/path/to/file'
w.behavior(:state_file_behavior) do |b|
b.file = stop_timestamp_file
end
w.start_if do |on|
on.condition(:file_mtime) do |c|
c.interval = 2
c.path = stop_timestamp_file
c.max_age = 10
end
end
注意:第二种方式,w.keepalive
无法正常使用
答案 1 :(得分:0)
我遇到了同样的问题,两种解决方案都有效但你也可以通过使用lambda条件来修改上帝。我刚刚创建了一个新的:down
状态,当进程退出时,我将转换到:down
状态,然后使用lambda延迟启动一段时间。
要创建新状态,只需将其添加到valid_states
即可这就是我的神文件的样子
God.watch do |w|
w.name = "brain"
w.start = "ruby yourthing.rb"
w.stop_grace = 30
w.valid_states = [:init, :up, :start, :restart, :down]
...
# down if process not running
w.transition(:up, :down) do |on|
on.condition(:process_exits)
end
# delay when down and move to start
w.transition(:down, :start) do |on|
on.condition(:lambda) do |c|
c.lambda = lambda do
puts "process exists, sleep 30 seconds"
sleep 30
true
end
end
end