我正在与上帝一起监视一个红宝石程序。当 ruby 程序退出时,我想等待 10 秒,直到它再次启动。当我使用grace
时,进程退出后,进程立即重新启动,但是上帝等待10秒的宽限期,直到它查看进程。现在当进程在宽限期结束之前被杀死时,上帝不会再次拿起它,并且该进程永远不会重新启动。
我希望上帝等待 10 秒钟,直到退出后运行启动命令。我该怎么做?
我在手表上试过transition
on :process_exits
,但我很难找到一种方法来将等待时间设置在正确的位置。
编辑:在查看了上帝的来源之后,我怀疑一个可能的解决方案是添加一个在其before_start
方法中等待的自定义行为。这听起来合理吗?(见下文)(完)
更多细节:
当我使用 a 中的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)