Class: ProconBypassMan::Forever

Inherits:
Object
  • Object
show all
Defined in:
lib/procon_bypass_man/support/forever.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run(&block) ⇒ Object

動作確認方法

  • 10秒ごとにrefreshするのでタイムアウトは起きない

    • ProconBypassMan::Forever.run { |watchdog| loop { puts(:hi); sleep(10); watchdog.active! } }

  • タイムアウトが起きること

    • ProconBypassMan::Forever.run { |watchdog| loop { puts(:hi); sleep(10); } }



8
9
10
11
12
# File 'lib/procon_bypass_man/support/forever.rb', line 8

def self.run(&block)
  loop do
    new.run(&block)
  end
end

Instance Method Details

#run(&block) ⇒ void

This method returns an undefined value.

Raises:

  • (ArgumentError)


15
16
17
18
19
20
# File 'lib/procon_bypass_man/support/forever.rb', line 15

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

#wait_and_kill_if_outdated(thread, watchdog) ⇒ void

This method returns an undefined value.

Parameters:



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/procon_bypass_man/support/forever.rb', line 38

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

#work_one(callable:) ⇒ Array<Thread, ProconBypassMan::Watchdog>

Parameters:

  • callable (Proc)

Returns:



24
25
26
27
28
29
30
31
32
33
# File 'lib/procon_bypass_man/support/forever.rb', line 24

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