Class: Blender::Lock::Flock
- Inherits:
-
Object
- Object
- Blender::Lock::Flock
- Defined in:
- lib/blender/lock/flock.rb
Instance Method Summary collapse
- #acquire ⇒ Object
-
#initialize(name, options) ⇒ Flock
constructor
A new instance of Flock.
- #release ⇒ Object
- #with_lock ⇒ Object
Constructor Details
#initialize(name, options) ⇒ Flock
Returns a new instance of Flock.
24 25 26 27 28 |
# File 'lib/blender/lock/flock.rb', line 24 def initialize(name, ) @path = ['path'] || File.join('/tmp', name) @timeout = [:timeout] || 0 @job_name = name end |
Instance Method Details
#acquire ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/blender/lock/flock.rb', line 30 def acquire @lock_fd = File.open(@path, File::CREAT|File::RDWR, 0644) @lock_fd.fcntl( Fcntl::F_SETFD, @lock_fd.fcntl(Fcntl::F_GETFD, 0) | Fcntl::FD_CLOEXEC ) if @timeout > 0 Timeout.timeout(@timeout) do @lock_fd.flock(File::LOCK_EX) end else locked = @lock_fd.flock(File::LOCK_NB | File::LOCK_EX) raise LockAcquisitionError, "Failed to lock file '#{@path}'" if locked == false end @lock_fd.write({job: @job_name, pid: Process.pid }.inspect) end |
#release ⇒ Object
44 45 46 47 48 |
# File 'lib/blender/lock/flock.rb', line 44 def release @lock_fd.flock(File::LOCK_UN) @lock_fd.close File.delete(@path) if File.exists?(@path) end |
#with_lock ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/blender/lock/flock.rb', line 50 def with_lock acquire yield if block_given? rescue Timeout::Error => e raise LockAcquisitionError, 'Timeout while waiting for lock acquisition' ensure release if @lock_fd end |