Class: Spitball::FileLock
- Inherits:
-
Object
- Object
- Spitball::FileLock
- Defined in:
- lib/spitball/file_lock.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #acquire_lock ⇒ Object
-
#initialize(path) ⇒ FileLock
constructor
A new instance of FileLock.
-
#release_lock ⇒ Object
seems silly to lock to release lock.
Constructor Details
#initialize(path) ⇒ FileLock
Returns a new instance of FileLock.
7 8 9 |
# File 'lib/spitball/file_lock.rb', line 7 def initialize(path) @path = path end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
5 6 7 |
# File 'lib/spitball/file_lock.rb', line 5 def path @path end |
Instance Method Details
#acquire_lock ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/spitball/file_lock.rb', line 11 def acquire_lock File.open(pre_lock_path, 'w') {|f| f.write Process.pid } system "ln #{pre_lock_path} #{path} > /dev/null 2>&1" File.read(path).to_i == Process.pid ensure FileUtils.rm_f pre_lock_path end |
#release_lock ⇒ Object
seems silly to lock to release lock
20 21 22 |
# File 'lib/spitball/file_lock.rb', line 20 def release_lock FileUtils.rm_f path if acquire_lock end |