Class: Tenderloin::Busy

Inherits:
Object
  • Object
show all
Extended by:
Util
Defined in:
lib/tenderloin/busy.rb

Constant Summary collapse

@@busy =
false
@@mutex =
Mutex.new
@@trap_thread =
nil

Class Method Summary collapse

Methods included from Util

error_and_exit, included, logger, wrap_output

Class Method Details

.busy(&block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/tenderloin/busy.rb', line 26

def busy(&block)
  @@mutex.synchronize do
    begin
      Signal.trap("INT") { wait_for_not_busy }
      Busy.busy = true
      runner = Thread.new(block) { block.call }
      runner.join
    ensure
      # In the case an exception is thrown, make sure we restore
      # busy back to some sane state.
      Busy.busy = false

      # Make sure that the trap thread completes, if it is running
      trap_thread.join if trap_thread

      # And restore the INT trap to the default
      Signal.trap("INT", "DEFAULT")
    end
  end
end

.busy=(val) ⇒ Object



22
23
24
# File 'lib/tenderloin/busy.rb', line 22

def busy=(val)
  @@busy = val
end

.busy?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/tenderloin/busy.rb', line 18

def busy?
  @@busy
end

.reset_trap_thread!Object

Used for testing



63
64
65
# File 'lib/tenderloin/busy.rb', line 63

def reset_trap_thread!
  @@trap_thread = nil
end

.trap_threadObject

Returns the trap thread



68
69
70
# File 'lib/tenderloin/busy.rb', line 68

def trap_thread
  @@trap_thread
end

.wait_for_not_busy(sleeptime = 5) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/tenderloin/busy.rb', line 47

def wait_for_not_busy(sleeptime=5)
  @@trap_thread ||= Thread.new do
    # Wait while the app is busy
    loop do
      break unless busy?
      logger.info "Waiting for tenderloin to clean itself up..."
      sleep sleeptime
    end

    # Exit out of the entire script
    logger.info "Exiting tenderloin..."
    exit
  end
end