aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2006-04-28 03:15:31 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2006-04-28 03:15:31 +0000
commit4a218de694c8f77a396f1dc55c828b4a3f693d23 (patch)
treefc13bd2e744299b90abc68f40ee760b568a699c3 /railties
parent0dc53a8f6f024c5ca7afba7ada498a26ab888c0f (diff)
downloadrails-4a218de694c8f77a396f1dc55c828b4a3f693d23.tar.gz
rails-4a218de694c8f77a396f1dc55c828b4a3f693d23.tar.bz2
rails-4a218de694c8f77a396f1dc55c828b4a3f693d23.zip
Added a simple process inspector
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4298 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'railties')
-rw-r--r--railties/lib/commands/process/inspector.rb68
1 files changed, 68 insertions, 0 deletions
diff --git a/railties/lib/commands/process/inspector.rb b/railties/lib/commands/process/inspector.rb
new file mode 100644
index 0000000000..cc17db6ac1
--- /dev/null
+++ b/railties/lib/commands/process/inspector.rb
@@ -0,0 +1,68 @@
+require 'optparse'
+
+if RUBY_PLATFORM =~ /mswin32/ then abort("Reaper is only for Unix") end
+
+OPTIONS = {
+ :pid_path => File.expand_path(RAILS_ROOT + '/tmp/pids'),
+ :pattern => "dispatch.*.pid",
+ :ps => "ps -o user,start,etime,pcpu,vsz,majflt,command -p %s"
+}
+
+class Inspector
+ def self.inspect(pid_path, pattern)
+ new(pid_path, pattern).inspect
+ end
+
+ def initialize(pid_path, pattern)
+ @pid_path, @pattern = pid_path, pattern
+ end
+
+ def inspect
+ for process in find_processes
+ puts "#{File.basename(process[:pid_file])}:"
+ puts(`#{OPTIONS[:ps] % process[:pid]}`)
+ puts
+ end
+ end
+
+ private
+ def find_processes
+ pid_files.inject([]) do |pids, pid_file|
+ pids << { :pid_file => pid_file, :pid => File.read(pid_file).to_i }
+ end
+ end
+
+ def pid_files
+ Dir.glob(@pid_path + "/" + @pattern)
+ end
+end
+
+
+ARGV.options do |opts|
+ opts.banner = "Usage: inspector [options]"
+
+ opts.separator ""
+
+ opts.on <<-EOF
+ Description:
+ Get the lowdown on processes.
+
+ Examples:
+ inspector
+ inspector -s 'ps -o user,start,majflt,pcpu,vsz -p %s'
+ EOF
+
+ opts.on(" Options:")
+
+ opts.on("-s", "--ps=command", "default: #{OPTIONS[:ps]}", String) { |OPTIONS[:ps]| }
+ opts.on("-p", "--pidpath=path", "default: #{OPTIONS[:pid_path]}", String) { |OPTIONS[:pid_path]| }
+ opts.on("-r", "--pattern=pattern", "default: #{OPTIONS[:pattern]}", String) { |OPTIONS[:pattern]| }
+
+ opts.separator ""
+
+ opts.on("-h", "--help", "Show this help message.") { puts opts; exit }
+
+ opts.parse!
+end
+
+Inspector.inspect(OPTIONS[:pid_path], OPTIONS[:pattern]) \ No newline at end of file