core: delete environment lock files

This commit is contained in:
Mitchell Hashimoto 2014-04-24 23:13:08 -07:00
parent 84f889e801
commit 0ed3c5174b
1 changed files with 44 additions and 18 deletions

View File

@ -352,6 +352,8 @@ module Vagrant
# @param [String] name Name of the lock, since multiple locks can # @param [String] name Name of the lock, since multiple locks can
# be held at one time. # be held at one time.
def lock(name="global", **opts) def lock(name="global", **opts)
f = nil
# If we don't have a block, then locking is useless, so ignore it # If we don't have a block, then locking is useless, so ignore it
return if !block_given? return if !block_given?
@ -362,30 +364,54 @@ module Vagrant
lock_path = data_dir.join("lock.#{name}.lock") lock_path = data_dir.join("lock.#{name}.lock")
@logger.debug("Attempting to acquire process-lock: #{name}") @logger.debug("Attempting to acquire process-lock: #{name}")
File.open(lock_path, "w+") do |f|
# The file locking fails only if it returns "false." If it if name != "dotlock"
# succeeds it returns a 0, so we must explicitly check for lock("dotlock", no_clean: true) do
# the proper error case. f = File.open(lock_path, "w+")
if f.flock(File::LOCK_EX | File::LOCK_NB) === false
@logger.warn("Process-lock in use: #{name}")
raise Errors::EnvironmentLockedError,
name: name
end end
else
f = File.open(lock_path, "w+")
end
@logger.info("Acquired process lock: #{name}") # The file locking fails only if it returns "false." If it
# succeeds it returns a 0, so we must explicitly check for
# the proper error case.
if f.flock(File::LOCK_EX | File::LOCK_NB) === false
@logger.warn("Process-lock in use: #{name}")
raise Errors::EnvironmentLockedError,
name: name
end
begin @logger.info("Acquired process lock: #{name}")
# Mark that we have a lock
@locks[name] = true
return yield result = nil
ensure begin
# We need to make sure that no matter what this is always # Mark that we have a lock
# reset to false so we don't think we have a lock when we @locks[name] = true
# actually don't.
@locks.delete(name) result = yield
ensure
# We need to make sure that no matter what this is always
# reset to false so we don't think we have a lock when we
# actually don't.
@locks.delete(name)
end
# Clean up the lock file, this requires another lock
if !opts[:no_clean]
lock("dotlock", no_clean: true) do
f.close
File.delete(lock_path)
end end
end end
# Return the result
return result
ensure
begin
f.close if f
rescue IOError
end
end end
# This returns a machine with the proper provider for this environment. # This returns a machine with the proper provider for this environment.