class Blender::Lock::Flock

Public Class Methods

new(name, options) click to toggle source
# File lib/blender/lock/flock.rb, line 24
def initialize(name, options)
  @path = options['path'] || File.join('/tmp', name)
  @timeout = options[:timeout] || 0
  @job_name = name
end

Public Instance Methods

acquire() click to toggle source
# 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() click to toggle source
# 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() { || ... } click to toggle source
# 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