Class: Rush::ProcessSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rush/process_set.rb

Overview

A container for processes that behaves like an array, and adds process-specific operations on the entire set, like kill.

Example:

processes.filter(:cmdline => /mongrel_rails/).kill

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(processes) ⇒ ProcessSet

Returns a new instance of ProcessSet.



11
12
13
# File 'lib/rush/process_set.rb', line 11

def initialize(processes)
	@processes = processes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object

All other messages (like size or first) are passed through to the array.



59
60
61
# File 'lib/rush/process_set.rb', line 59

def method_missing(meth, *args)
	processes.send(meth, *args)
end

Instance Attribute Details

#processesObject (readonly)

Returns the value of attribute processes.



9
10
11
# File 'lib/rush/process_set.rb', line 9

def processes
  @processes
end

Instance Method Details

#==(other) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/rush/process_set.rb', line 50

def ==(other)
	if other.class == self.class
		other.processes == processes
	else
		to_a == other
	end
end

#alive?Boolean

Check status of all processes in the set, returns an array of booleans.

Returns:

  • (Boolean)


40
41
42
# File 'lib/rush/process_set.rb', line 40

def alive?
	processes.map { |p| p.alive? }
end

#eachObject



46
47
48
# File 'lib/rush/process_set.rb', line 46

def each
	processes.each { |p| yield p }
end

#filter(conditions) ⇒ Object

Filter by any field that the process responds to. Specify an exact value, or a regular expression. All conditions are put together as a boolean AND, so these two statements are equivalent:

processes.filter(:uid => 501).filter(:cmdline => /ruby/)
processes.filter(:uid => 501, :cmdline => /ruby/)


22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rush/process_set.rb', line 22

def filter(conditions)
	Rush::ProcessSet.new(
		processes.select do |p|
			conditions.all? do |key, value|
				value.class == Regexp ?
					value.match(p.send(key)) :
					p.send(key) == value
			end
		end
	)
end

#killObject

Kill all processes in the set.



35
36
37
# File 'lib/rush/process_set.rb', line 35

def kill
	processes.each { |p| p.kill }
end