Class: WithAdvisoryLock::Flock

Inherits:
Base
  • Object
show all
Defined in:
lib/with_advisory_lock/flock.rb

Instance Attribute Summary

Attributes inherited from Base

#connection, #disable_query_cache, #lock_name, #shared, #timeout_seconds, #transaction

Instance Method Summary collapse

Methods inherited from Base

#already_locked?, #initialize, #lock_and_yield, lock_stack, #lock_stack_item, #lock_str, #stable_hashcode, #unique_column_name, #with_advisory_lock_if_needed, #yield_with_lock, #yield_with_lock_and_timeout

Constructor Details

This class inherits a constructor from WithAdvisoryLock::Base

Instance Method Details

#file_ioObject



16
17
18
19
20
21
# File 'lib/with_advisory_lock/flock.rb', line 16

def file_io
  @file_io ||= begin
    FileUtils.touch(filename)
    File.open(filename, 'r+')
  end
end

#filenameObject



7
8
9
10
11
12
13
14
# File 'lib/with_advisory_lock/flock.rb', line 7

def filename
  @filename ||= begin
    safe = lock_str.to_s.gsub(/[^a-z0-9]/i, '')
    fn = ".lock-#{safe}-#{stable_hashcode(lock_str)}"
    # Let the user specify a directory besides CWD.
    ENV['FLOCK_DIR'] ? File.expand_path(fn, ENV['FLOCK_DIR']) : fn
  end
end

#release_lockObject



29
30
31
# File 'lib/with_advisory_lock/flock.rb', line 29

def release_lock
  0 == file_io.flock(File::LOCK_UN)
end

#try_lockObject

Raises:

  • (ArgumentError)


23
24
25
26
27
# File 'lib/with_advisory_lock/flock.rb', line 23

def try_lock
  raise ArgumentError, 'transaction level locks are not supported on SQLite' if transaction

  0 == file_io.flock((shared ? File::LOCK_SH : File::LOCK_EX) | File::LOCK_NB)
end