正确的方法来停止Ruby服务

时间:2015-02-16 16:37:09

标签: ruby service

我正在使用AirBnb Nerve服务。它的服务代码如下所示:


require 'logger'
require 'json'
require 'timeout'

require 'nerve/version'
require 'nerve/utils'
require 'nerve/log'
require 'nerve/ring_buffer'
require 'nerve/reporter'
require 'nerve/service_watcher'

module Nerve
  class Nerve

    include Logging

    def initialize(opts={})
      log.info 'nerve: starting up!'

      # set global variable for exit signal
      $EXIT = false

      ...some code...

      # Any exceptions in the watcher threads should wake the main thread so
      # that we can fail fast.
      Thread.abort_on_exception = true

      log.debug 'nerve: completed init'
    end

    def run
      log.info 'nerve: starting run'

      @services.each do |name, config|
        launch_watcher(name, config)
      end

      begin
        sleep
      rescue StandardError => e
        log.error "nerve: encountered unexpected exception #{e.inspect} in main thread"
        raise e
      ensure
        $EXIT = true
        log.warn 'nerve: reaping all watchers'
        @watchers.each do |name, watcher_thread|
          reap_watcher(name)
        end
      end

      log.info 'nerve: exiting'
    ensure
      $EXIT = true
    end

    def launch_watcher(name, config)
... some code ...
    end

    def reap_watcher(name)
... some code ...
    end
  end
end

我没有看到任何停止方法。什么是停止这种服务的正确方法?我正在使用JRuby并打算为此服务编写JSVC适配器。

1 个答案:

答案 0 :(得分:0)

没有办法通过当前的API执行此操作,除非向其发送信号。

如果发送信号不起作用且您想明确处理停止,看起来您需要更改以下内容:

  • #stop方法添加到设置Nerve。{/ li>的$EXIT = true
  • 修改#run,使其醒来并检查sleep,而不是永远沉睡($EXIT)。