module Linecook::Locking

Public Instance Methods

clear_lock(name) click to toggle source
# File lib/linecook-gem/util/locking.rb, line 17
def clear_lock(name)
  unlock(name)
  FileUtils.rm_f(lockfile(name))
end
lock(name) click to toggle source
# File lib/linecook-gem/util/locking.rb, line 5
def lock(name)
  lockfile(name).flock(File::LOCK_EX)
end
lock_path(suffix) click to toggle source
# File lib/linecook-gem/util/locking.rb, line 28
def lock_path(suffix)
  "/tmp/lock_#{suffix.gsub('/','_')}"
end
lockfile(suffix) click to toggle source
# 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) click to toggle source
# 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