2010-02-10 07:08:23 +00:00
|
|
|
module Vagrant
|
2010-02-09 06:38:32 +00:00
|
|
|
def self.busy?
|
|
|
|
Busy.busy?
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.busy(&block)
|
2010-02-09 06:54:21 +00:00
|
|
|
Busy.busy(&block)
|
2010-02-09 06:38:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
class Busy
|
2010-02-16 01:56:48 +00:00
|
|
|
extend Vagrant::Util
|
|
|
|
|
2010-02-09 06:38:32 +00:00
|
|
|
@@busy = false
|
2010-02-09 06:54:21 +00:00
|
|
|
@@mutex = Mutex.new
|
2010-02-16 02:37:20 +00:00
|
|
|
@@trap_thread = nil
|
2010-02-09 07:32:04 +00:00
|
|
|
|
2010-02-09 06:38:32 +00:00
|
|
|
class << self
|
|
|
|
def busy?
|
|
|
|
@@busy
|
|
|
|
end
|
|
|
|
|
|
|
|
def busy=(val)
|
|
|
|
@@busy = val
|
|
|
|
end
|
2010-02-09 06:54:21 +00:00
|
|
|
|
|
|
|
def busy(&block)
|
|
|
|
@@mutex.synchronize do
|
2010-02-09 07:32:04 +00:00
|
|
|
begin
|
2010-02-16 01:56:48 +00:00
|
|
|
Signal.trap("INT") { wait_for_not_busy }
|
2010-02-09 07:32:04 +00:00
|
|
|
Busy.busy = true
|
2010-04-24 10:26:44 +00:00
|
|
|
block.call
|
2010-02-09 07:32:04 +00:00
|
|
|
ensure
|
|
|
|
# In the case an exception is thrown, make sure we restore
|
|
|
|
# busy back to some sane state.
|
|
|
|
Busy.busy = false
|
2010-02-16 01:56:48 +00:00
|
|
|
|
2010-02-16 02:37:20 +00:00
|
|
|
# Make sure that the trap thread completes, if it is running
|
|
|
|
trap_thread.join if trap_thread
|
|
|
|
|
2010-02-16 01:56:48 +00:00
|
|
|
# And restore the INT trap to the default
|
|
|
|
Signal.trap("INT", "DEFAULT")
|
2010-02-09 07:32:04 +00:00
|
|
|
end
|
2010-02-09 06:54:21 +00:00
|
|
|
end
|
|
|
|
end
|
2010-02-16 01:56:48 +00:00
|
|
|
|
|
|
|
def wait_for_not_busy(sleeptime=5)
|
2010-04-15 05:30:46 +00:00
|
|
|
if @@trap_thread
|
2010-05-21 03:54:34 +00:00
|
|
|
# logger.info "Exiting vagrant immediately!"
|
2010-04-15 05:30:46 +00:00
|
|
|
Thread.kill(@@trap_thread)
|
|
|
|
abort
|
|
|
|
return # for tests
|
|
|
|
end
|
|
|
|
|
2010-02-16 02:37:20 +00:00
|
|
|
@@trap_thread ||= Thread.new do
|
2010-02-16 01:56:48 +00:00
|
|
|
# Wait while the app is busy
|
|
|
|
loop do
|
|
|
|
break unless busy?
|
2010-05-21 03:54:34 +00:00
|
|
|
# logger.info "Waiting for vagrant to clean itself up..."
|
2010-02-16 01:56:48 +00:00
|
|
|
sleep sleeptime
|
|
|
|
end
|
|
|
|
|
|
|
|
# Exit out of the entire script
|
2010-05-21 03:54:34 +00:00
|
|
|
# logger.info "Exiting vagrant..."
|
2010-02-16 01:56:48 +00:00
|
|
|
exit
|
|
|
|
end
|
|
|
|
end
|
2010-02-16 02:37:20 +00:00
|
|
|
|
|
|
|
# Used for testing
|
|
|
|
def reset_trap_thread!
|
|
|
|
@@trap_thread = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the trap thread
|
|
|
|
def trap_thread
|
|
|
|
@@trap_thread
|
|
|
|
end
|
2010-02-09 06:38:32 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|