1

我正在与上帝一起监视一个红宝石程序。当 ruby​​ 程序退出时,我想等待 10 秒,直到它再次启动。当我使用grace时,进程退出后,进程立即重新启动,但是上帝等待10秒的宽限期,直到它查看进程。现在当进程在宽限期结束之前被杀死时,上帝不会再次拿起它,并且该进程永远不会重新启动。

我希望上帝等待 10 秒钟,直到退出后运行启动命令。我该怎么做?

我在手表上试过transitionon :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)
4

2 回答 2

1

I think it should work, and the WaitBehavior class could be shorter.

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

in .god config:

# .god
w.behavior(:wait_behavior) do |b|
  b.delay = 10
end

another way

Similar to WaitBehavior, we can define a StateFileBehavior to touch a file after_stop.

require 'fileutils'

module God
  module Behaviors
    class StateFileBehavior < Behavior
      attr_accessor :file

      def after_stop
        FileUtils.touch file
      end
    end
  end
end

and in .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

Notice: In the second way, it could not work fine with w.keepalive

于 2015-02-06T04:35:41.430 回答
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
于 2015-07-10T12:40:30.250 回答