Class: Spitball::FileLock

Inherits:
Object
  • Object
show all
Defined in:
lib/spitball/file_lock.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#pathObject (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_lockObject



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_lockObject

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