Module: Linecook::Locking

Included in:
AmiPacker, Baker::Docker, Packer
Defined in:
lib/linecook-gem/util/locking.rb

Instance Method Summary collapse

Instance Method Details

#clear_lock(name) ⇒ Object



17
18
19
20
# File 'lib/linecook-gem/util/locking.rb', line 17

def clear_lock(name)
  unlock(name)
  FileUtils.rm_f(lockfile(name))
end

#lock(name) ⇒ Object



5
6
7
# File 'lib/linecook-gem/util/locking.rb', line 5

def lock(name)
  lockfile(name).flock(File::LOCK_EX)
end

#lock_path(suffix) ⇒ Object



28
29
30
# File 'lib/linecook-gem/util/locking.rb', line 28

def lock_path(suffix)
  "/tmp/lock_#{suffix.gsub('/','_')}"
end

#lockfile(suffix) ⇒ Object



22
23
24
25
26
# File 'lib/linecook-gem/util/locking.rb', line 22

def lockfile(suffix)
  @locks ||= {}
  path = lock_path(suffix)
  @locks[path] = @locks[path] || File.open(path, File::RDWR|File::CREAT, 0644)
end

#unlock(name) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/linecook-gem/util/locking.rb', line 9

def unlock(name)
  return unless File.exists?(lock_path(name))
  lockfile(name).flock(File::LOCK_UN)
  lockfile(name).close
rescue IOError => e
  puts e.message
end