class ProcessLock::NixLocker

Constants

EXISTS

Public Instance Methods

ensure_only_process(name) { || ... } click to toggle source
# File lib/ProcessLock/nix_locker.rb, line 8
def ensure_only_process(name)
  path = File.join(tmpdir, "#{name}.pid")
  result = run(path) { yield }
  raise LockedError if result == EXISTS
  result
end

Private Instance Methods

run(path) { || ... } click to toggle source
# File lib/ProcessLock/nix_locker.rb, line 21
def run(path)
  thrown = false
  catch(EXISTS) do
    begin
      begin
        File.open(path, File::WRONLY|File::CREAT|File::EXCL) do |file|
          file << Process.pid
        end
      rescue
        thrown = true
        throw(EXISTS, EXISTS)
      end

      yield

    ensure
      File.delete(path) unless thrown
    end
  end
end
tmpdir() click to toggle source
# File lib/ProcessLock/nix_locker.rb, line 17
def tmpdir
  '/tmp'
end