Class: TimedExit

Inherits:
Object
  • Object
show all
Defined in:
lib/workety/timed_exit.rb

Instance Method Summary collapse

Constructor Details

#initialize(timeout = 60, message = "Timeout reached") ⇒ TimedExit

Returns a new instance of TimedExit.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/workety/timed_exit.rb', line 18

def initialize(timeout = 60, message = "Timeout reached")
  @mutex = Mutex.new

  @must_cancel = false
  @timeout_reached = false
  
  Thread.rescue_exit do
    sleep timeout
    
    @mutex.synchronize do
      unless @must_cancel
        @timeout_reached = true
        Rails.logger.info message
        Process.exit(false)
      end
    end
  end
  
  if block_given?
    yield
    cancel
  end
end

Instance Method Details

#cancelObject



42
43
44
45
46
47
# File 'lib/workety/timed_exit.rb', line 42

def cancel
  @mutex.synchronize do
    Process.exit(false) if @timeout_reached # See comment below
    @must_cancel = true
  end
end