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



148
149
150
151
152
153
154
155
# File 'lib/libcfruby/processes.rb', line 148

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



138
139
140
141
142
143
144
# File 'lib/libcfruby/processes.rb', line 138

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)


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/libcfruby/processes.rb', line 120

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)



80
81
82
83
84
85
86
# File 'lib/libcfruby/processes.rb', line 80

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.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/libcfruby/processes.rb', line 91

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