Class: ProcessMonitor

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

Class Method Summary collapse

Class Method Details

.get_pid(process_type, process_pattern = "") ⇒ Object

This method fetches the process ids of the process matching the process_type and process_pattern



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/process_monitor.rb', line 7

def self.get_pid(process_type,process_pattern="")
  # Parse the output to fetch the process id.
  process_id_reg_exp = %r{^(.*)\s*}

  process_information_lines = `pgrep #{process_type}|xargs ps`.split("\n")
  pids = []

  process_information_lines.each_with_index do |process_line,index|
    next if index == 0
    # The name of the process will be verity-spider
    # This is to make sure we don't kill any other ruby process other than spider.
    if process_line =~ /#{process_pattern}/
      process_id_reg_exp.match(process_line)
      pid =  $1.gsub(/\s*/, "").to_i
      pids << pid unless $$ == pid
    end
  end
  if pids.length > 1
    pids
  else
    pids.first
  end
end

.get_process_status(pid = nil) ⇒ Object

Gets the process status given the process id



35
36
37
38
39
40
41
42
43
44
# File 'lib/process_monitor.rb', line 35

def self.get_process_status(pid=nil)
  unless pid.nil?
    status_regexp = %r{\s\((.*)\)\n}
    complete_status = `cat /proc/#{pid}/status|grep State`
    status_regexp.match(complete_status)
    status = $1.capitalize
  else
    p "No pid given!"
  end
end

.process_is_up?(pid) ⇒ Boolean

Returns true if the process is up else returns false.

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
# File 'lib/process_monitor.rb', line 50

def self.process_is_up?(pid)
  unless pid.nil?
    # The folder /proc/<pid>/ contains information about the process
    # The file /proc/<pid>/status gives information about the process as to if it is sleeping and so on.
    # If the folder exists it would mean the process exists.
    (pid.nil? || !File.exists?("/proc/#{pid}"))? false : true
  else
     p "No pid given!"
  end
end