Class: Tagrity::PidFile

Inherits:
Object
  • Object
show all
Defined in:
lib/tagrity/pid_file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir, pid) ⇒ PidFile

Returns a new instance of PidFile.



66
67
68
69
# File 'lib/tagrity/pid_file.rb', line 66

def initialize(dir, pid)
  @dir = dir
  @pid = pid
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



64
65
66
# File 'lib/tagrity/pid_file.rb', line 64

def dir
  @dir
end

#pidObject (readonly)

Returns the value of attribute pid.



64
65
66
# File 'lib/tagrity/pid_file.rb', line 64

def pid
  @pid
end

Class Method Details

.alive_pid_files(dir: nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# 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

.revive_dead_pidsObject



39
40
41
42
43
44
45
46
47
48
49
50
51
# 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) ⇒ Object



6
7
8
# File 'lib/tagrity/pid_file.rb', line 6

def write(pid_file)
  File.write("#{Helper.run_dir}/#{pid_file.name}", pid_file.dir)
end

Instance Method Details

#==(other) ⇒ Object



71
72
73
74
# File 'lib/tagrity/pid_file.rb', line 71

def ==(other)
  @dir == other.dir
  @pid == other.pid
end

#deleteObject



80
81
82
83
# File 'lib/tagrity/pid_file.rb', line 80

def delete
  File.delete(pid_file_path)
  Helper.kill(pid.to_i)
end

#nameObject



76
77
78
# File 'lib/tagrity/pid_file.rb', line 76

def name
  "#{@dir.split('/').last}.#{@pid}.pid"
end