class Tagrity::PidFile

Attributes

dir[R]
pid[R]

Public Class Methods

alive_pid_files(dir: nil) click to toggle source
# File lib/tagrity/pid_file.rb, line 22
def alive_pid_files(dir: nil)
  Dir.glob("#{Helper.run_dir}/*").reduce([]) do |pid_files, path|
    pid = pid_from_path(path)
    pid_file_dir = File.read(path)

    if dir.nil? || same_dirs?(pid_file_dir, dir)
      if Helper.alive?(pid)
        pid_files << PidFile.new(pid_file_dir, pid)
      else
        File.delete(path)
      end
    end

    pid_files
  end
end
delete(dir) click to toggle source
# File lib/tagrity/pid_file.rb, line 10
def delete(dir)
  pid_file_paths = Dir.glob("#{Helper.run_dir}/#{dir.split('/').last}.*.pid").select do |path|
    full_dir = File.read(path)
    File.realdirpath(full_dir) == File.realdirpath(dir)
  end

  pid_file_paths.each do |path|
    File.delete(path)
    Helper.kill(pid_from_path(path))
  end
end
new(dir, pid) click to toggle source
# File lib/tagrity/pid_file.rb, line 66
def initialize(dir, pid)
  @dir = dir
  @pid = pid
end
revive_dead_pids() click to toggle source
# File lib/tagrity/pid_file.rb, line 39
def revive_dead_pids
  Dir.glob("#{Helper.run_dir}/*").reduce([]) do |pid_files, path|
    pid = pid_from_path(path)
    dir = File.read(path)

    if not Helper.alive?(pid)
      File.delete(path)
      Dir.chdir(File.realdirpath(dir)) do
        Process.spawn('tagrity start')
      end
    end
  end
end
write(pid_file) click to toggle source
# File lib/tagrity/pid_file.rb, line 6
def write(pid_file)
  File.write("#{Helper.run_dir}/#{pid_file.name}", pid_file.dir)
end

Private Class Methods

pid_from_path(pid_file_name) click to toggle source
# File lib/tagrity/pid_file.rb, line 59
def pid_from_path(pid_file_name)
  pid_file_name.split('.')[-2].to_i
end
same_dirs?(dir1, dir2) click to toggle source
# File lib/tagrity/pid_file.rb, line 55
def same_dirs?(dir1, dir2)
  File.realdirpath(dir1) == File.realdirpath(dir2)
end

Public Instance Methods

==(other) click to toggle source
# File lib/tagrity/pid_file.rb, line 71
def ==(other)
  @dir == other.dir
  @pid == other.pid
end
delete() click to toggle source
# File lib/tagrity/pid_file.rb, line 80
def delete
  File.delete(pid_file_path)
  Helper.kill(pid.to_i)
end
name() click to toggle source
# File lib/tagrity/pid_file.rb, line 76
def name
  "#{@dir.split('/').last}.#{@pid}.pid"
end

Private Instance Methods

pid_file_path() click to toggle source
# File lib/tagrity/pid_file.rb, line 87
def pid_file_path
  "#{Helper.run_dir}/#{name}"
end