Class: Cfruby::Processes::ProcessManager

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

Overview

The ProcessManager class serves mostly as an in-code description of the ProcessManager interface. It implements a handful of useful of helper functions, but is generally implemented in a very generic manner, and should be overridden on a case by case basis specific to a particular operating system

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object

Calls has_process? after converting the symbol to a Regexp



125
126
127
128
129
130
131
132
# File 'lib/libcfruby/processes.rb', line 125

def method_missing(symbol, *args)
  if(args == nil or args.length == 0)
    regex = Regexp.new(Regexp.escape(symbol.id2name))
    return has_process?(regex)
  end

  super.method(symbol).call(args)
end

Instance Method Details

#[](name) ⇒ Object

Calls has_process? after converting name to a Regexp



115
116
117
118
119
120
121
# File 'lib/libcfruby/processes.rb', line 115

def [](name)
  regex = name
  if(!regex.kind_of?(Regexp))
    regex = Regexp.new(Regexp.escape(name))
  end
  return has_process?(regex)       
end

#has_process?(processname) ⇒ Boolean

Returns the first ProcessInfo object of a process matching processname (either a String or Regex), or false

Returns:

  • (Boolean)


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/libcfruby/processes.rb', line 97

def has_process?(processname)
  if(!processname.kind_of?(Regexp))
    processname = Regexp.new(Regexp.escape(processname))
  end
  
  processes().each_value() { |runningprocess|
    if(processname.match(runningprocess.program))
      Cfruby.controller.inform('debug', "\"#{processname.source}\" is running")
      return(runningprocess)
    end
  }
  
  Cfruby.controller.inform('debug', "\"#{processname.source}\" is not running")
  return(false)
end

#kill(process) ⇒ Object

Kills the given process (expects a ProcessInfo object)



57
58
59
60
61
62
63
# File 'lib/libcfruby/processes.rb', line 57

def kill(process)
  `kill #{process.pid}`
  sleep(3)
  if(processes()[process.pid])
    `kill -9 #{process.pid}`
  end
end

#processesObject

Returns a ProcessList object that contains key value pairs for every process that is currently running.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/libcfruby/processes.rb', line 68

def processes()
  processes = ProcessList.new()
  processlist = `ps auxww`
  processregex = /^(\S+)\s+([0-9]+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.*)$/
  processlist.each() { |line|
    line.strip!()
    match = processregex.match(line)
    if(match != nil)
      info = ProcessInfo.new()
      info.username = match[1]
      info.pid = match[2].to_i()
      info.flags = match[8]
      info.starttime = match[9]
      info.runtime = match[10]
      info.program = match[11]
      info.cpu = match[3].to_f()
      info.mem = match[4].to_f()
      if(processes.has_key?(info.pid))
        raise(DuplicateProcessError, "Process #{info.pid} appeared twice in the process listing")
      end
      processes[info.pid] = info
    end
  }
  
  return(processes)
end