Class: WithAdvisoryLock::Flock
- Inherits:
-
Base
- Object
- Base
- WithAdvisoryLock::Flock
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
Instance Method Details
#file_io ⇒ Object
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
|
#filename ⇒ Object
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)}"
ENV['FLOCK_DIR'] ? File.expand_path(fn, ENV['FLOCK_DIR']) : fn
end
end
|
#release_lock ⇒ Object
29
30
31
|
# File 'lib/with_advisory_lock/flock.rb', line 29
def release_lock
0 == file_io.flock(File::LOCK_UN)
end
|
#try_lock ⇒ Object
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
|