class Vagrant::Util::FileMutex
Utility to provide a simple mutex via file lock
Public Class Methods
new(mutex_path)
click to toggle source
Create a new FileMutex
instance
@param mutex_path [String] path for file
# File lib/vagrant/util/file_mutex.rb, line 8 def initialize(mutex_path) @mutex_path = mutex_path end
Public Instance Methods
lock()
click to toggle source
Attempt to acquire the lock
# File lib/vagrant/util/file_mutex.rb, line 26 def lock if lock_file.flock(File::LOCK_EX|File::LOCK_NB) === false raise Errors::VagrantLocked, lock_file_path: @mutex_path end end
unlock()
click to toggle source
Unlock the file
# File lib/vagrant/util/file_mutex.rb, line 33 def unlock lock_file.flock(File::LOCK_UN) lock_file.close File.delete(@mutex_path) if File.file?(@mutex_path) end
with_lock(&block)
click to toggle source
Execute provided block within lock and unlock when completed
# File lib/vagrant/util/file_mutex.rb, line 14 def with_lock(&block) lock begin block.call rescue => e raise e ensure unlock end end
Protected Instance Methods
lock_file()
click to toggle source
# File lib/vagrant/util/file_mutex.rb, line 41 def lock_file return @lock_file if @lock_file && !@lock_file.closed? @lock_file = File.open(@mutex_path, "w+") end