module PidLock

Constants

VERSION

Attributes

pid_path[W]

Public Class Methods

ensure_single_running(pid_name) { || ... } click to toggle source
# File lib/pid_lock.rb, line 11
def ensure_single_running(pid_name)
  return if locked?(pid_name)
  PidLock.lock(pid_name)
  begin
    yield
  ensure
    PidLock.unlock(pid_name)
  end
end
lock(pid_name) click to toggle source
# File lib/pid_lock.rb, line 21
def lock(pid_name)
  Dir.exist?(pid_path) || FileUtils.mkdir_p(pid_path)
  File.write(File.join(pid_path, "#{pid_name}.pid"), $PID)
end
locked?(pid_name) click to toggle source
# File lib/pid_lock.rb, line 26
def locked?(pid_name)
  return false unless File.exist?(File.join(pid_path, "#{pid_name}.pid"))
  begin
    Process.kill(0, pid(pid_name))
    return true
  rescue StandardError
    self.unlock(pid_name)
    return false
  end
end
pid(pid_name) click to toggle source
# File lib/pid_lock.rb, line 48
def pid(pid_name)
  File.read(File.join(pid_path, "#{pid_name}.pid")).to_i
end
pid_path() click to toggle source
# File lib/pid_lock.rb, line 7
def pid_path
  @pid_path || "tmp/pid_locks"
end
stop(pid_name) click to toggle source
# File lib/pid_lock.rb, line 37
def stop(pid_name)
  return unless File.exist?(File.join(pid_path, "#{pid_name}.pid"))
  pid = File.read(File.join(pid_path, "#{pid_name}.pid"))
  return if pid.to_s.empty?
  begin
    Process.kill("TERM", pid.to_i)
  rescue StandardError
  end
  unlock(pid_name)
end
unlock(pid_name) click to toggle source
# File lib/pid_lock.rb, line 52
def unlock(pid_name)
  if File.exist?(File.join(pid_path, "#{pid_name}.pid"))
    File.delete(File.join(pid_path, "#{pid_name}.pid"))
  end
end