Class: SpiderGazelle::Management::Pid

Inherits:
Object
  • Object
show all
Defined in:
lib/spider-gazelle/management/process.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Pid

Returns a new instance of Pid.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/spider-gazelle/management/process.rb', line 15

def initialize(path)
  @pid_file = path

  remove_stale_pid_file
  write_pid_file

  # At application exit remove the file
  #  unless of course we do not own the file
  cur = ::Process.pid
  at_exit do
    if cur == current
      remove_pid_file
    end
  end
end

Instance Method Details

#currentObject



31
32
33
# File 'lib/spider-gazelle/management/process.rb', line 31

def current
  File.exist?(@pid_file) ? open(@pid_file).read.to_i : nil
end

#running?(pid) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/spider-gazelle/management/process.rb', line 35

def running?(pid)
  if ::FFI::Platform.windows?
    begin
      # Returns exit code or nil if still running
      if ::Process.respond_to? :get_exitcode
        return ::Process.get_exitcode(pid).nil?
      else
        # win32/process not loaded
        return false
      end
    rescue
      # PID probably doesn't exist
      false
    end
  else
    begin
      ::Process.getpgid(pid) != -1
    rescue ::Errno::EPERM
      # Operation not permitted
      true
    rescue ::Errno::ESRCH
      # No such process
      false
    end
  end
end