Sha256: a5fa4274f01a05309da1d19d360b93d2a749ae4ebd633e5adc7619a1d0cd746e

Contents?: true

Size: 1.38 KB

Versions: 6

Compression:

Stored size: 1.38 KB

Contents

module ProconBypassMan
  class Forever
    # 動作確認方法
    # - 10秒ごとにrefreshするのでタイムアウトは起きない
    #   - ProconBypassMan::Forever.run { |watchdog| loop { puts(:hi); sleep(10); watchdog.active! } }
    # - タイムアウトが起きること
    #   - ProconBypassMan::Forever.run { |watchdog| loop { puts(:hi); sleep(10); } }
    def self.run(&block)
      loop do
        new.run(&block)
      end
    end

    # @return [void]
    def run(&block)
      raise(ArgumentError, "need a block") unless block_given?

      thread, watchdog = work_one(callable: block)
      wait_and_kill_if_outdated(thread, watchdog)
    end

    # @param [Proc] callable
    # @return [Array<Thread, ProconBypassMan::Watchdog>]
    def work_one(callable: )
      watchdog = ProconBypassMan::Watchdog.new
      thread = Thread.start do
        callable.call(watchdog)
      rescue => e
        ProconBypassMan.logger.error("[Forever] #{e.full_message}")
      end

      return [thread, watchdog]
    end

    # @param [ProconBypassMan::Watchdog] watchdog
    # @param [Thread] thread
    # @return [void]
    def wait_and_kill_if_outdated(thread, watchdog)
      loop do
        if watchdog.outdated?
          watchdog.active!
          ProconBypassMan.logger.error("watchdog timeout!!")
          thread.kill
          return
        end

        sleep(10)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
procon_bypass_man-0.3.12 lib/procon_bypass_man/support/forever.rb
procon_bypass_man-0.3.11 lib/procon_bypass_man/support/forever.rb
procon_bypass_man-0.3.10 lib/procon_bypass_man/support/forever.rb
procon_bypass_man-0.3.9 lib/procon_bypass_man/support/forever.rb
procon_bypass_man-0.3.8.1 lib/procon_bypass_man/support/forever.rb
procon_bypass_man-0.3.8 lib/procon_bypass_man/support/forever.rb