Class: AliveCheck

Inherits:
Object
  • Object
show all
Defined in:
lib/piggy-core/alive_check.rb

Overview

This class works similar to a timeout. Run it like this:

x = AliveCheck(10)
begin
  x.check { ... x.alive! ... }
rescue Timeout::Error => msg
  ...
end

If x doesn’t get an alive! notification for 10 to 20 seconds, it will raise a Timeout::Error

Instance Method Summary collapse

Constructor Details

#initialize(check_interval_sec, ex_msg = 'execution expired') ⇒ AliveCheck

Returns a new instance of AliveCheck.



18
19
20
21
22
# File 'lib/piggy-core/alive_check.rb', line 18

def initialize(check_interval_sec, ex_msg = 'execution expired')
  @sec = check_interval_sec
  @alive = false
  @ex_msg = ex_msg
end

Instance Method Details

#alive!Object



42
43
44
# File 'lib/piggy-core/alive_check.rb', line 42

def alive!
  @alive = true
end

#checkObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/piggy-core/alive_check.rb', line 24

def check
  checked_thread = Thread.current
  @alive = true
  alive_checker = Thread.new {
    while(@alive) do
      @alive = false
      sleep(@sec)
    end
    checked_thread.raise(Timeout::Error, @ex_msg) if checked_thread.alive?
  }
  begin 
    yield
  ensure
    @alive = false
    alive_checker.kill if alive_checker and alive_checker.alive?
  end
end