Class: ProgramProcess

Inherits:
Object show all
Defined in:
lib/commands/process/reaper.rb

Overview

Instances of this class represent a single running process. Processes may be queried by “keyword” to find those that meet a specific set of criteria.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pid, command) ⇒ ProgramProcess

Create a new ProgramProcess instance that represents the process with the given pid, running the given command.



49
50
51
# File 'lib/commands/process/reaper.rb', line 49

def initialize(pid, command)
  @pid, @command = pid, command
end

Class Method Details

.find_by_keyword(keyword) ⇒ Object

Searches for all processes matching the given keyword:

ProgramProcess.find_by_keyword("basecamp")


33
34
35
36
37
38
39
# File 'lib/commands/process/reaper.rb', line 33

def find_by_keyword(keyword)
  process_lines_with_keyword(keyword).split("\n").collect { |line|
    next if line =~ /inq|ps axww|grep|spawn-fcgi|spawner|reaper/
    pid, *command = line.split
    new(pid, command.join(" "))
  }.compact
end

.process_keywords(action, *keywords) ⇒ Object

Searches for all processes matching the given keywords, and then invokes a specific action on each of them. This is useful for (e.g.) reloading a set of processes:

ProgramProcess.process_keywords(:reload, "basecamp")


17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/commands/process/reaper.rb', line 17

def process_keywords(action, *keywords)
  processes = keywords.collect { |keyword| find_by_keyword(keyword) }.flatten

  if processes.empty?
    puts "Couldn't find any process matching: #{keywords.join(" or ")}"
  else
    processes.each do |process|
      puts "#{action.capitalize}ing #{process}"
      process.send(action)
    end
  end      
end

Instance Method Details

#gracefulObject

Forces the (rails) application to gracefully terminate by sending a TERM signal to the process.



61
62
63
# File 'lib/commands/process/reaper.rb', line 61

def graceful
  `kill -s TERM #{@pid}`
end

#killObject

Forces the (rails) application to terminate immediately by sending a -9 signal to the process.



67
68
69
# File 'lib/commands/process/reaper.rb', line 67

def kill
  `kill -9 #{@pid}`
end

#reloadObject

Forces the (rails) application to reload by sending a HUP signal to the process.



55
56
57
# File 'lib/commands/process/reaper.rb', line 55

def reload
  `kill -s HUP #{@pid}`
end

#restartObject

Force the (rails) application to restart by sending a USR2 signal to the process.



78
79
80
# File 'lib/commands/process/reaper.rb', line 78

def restart
  `kill -s USR2 #{@pid}`
end

#to_sObject

:nodoc:



82
83
84
# File 'lib/commands/process/reaper.rb', line 82

def to_s #:nodoc:
  "[#{@pid}] #{@command}"
end

#usr1Object

Send a USR1 signal to the process.



72
73
74
# File 'lib/commands/process/reaper.rb', line 72

def usr1
  `kill -s USR1 #{@pid}`
end