Class: Resque::Top::CLI
- Inherits:
-
Object
- Object
- Resque::Top::CLI
- Defined in:
- lib/resque-top/cli.rb
Instance Method Summary collapse
- #clear ⇒ Object
- #command_exists?(command) ⇒ Boolean
- #detect_terminal_size ⇒ Object
- #display ⇒ Object
-
#initialize(options) ⇒ CLI
constructor
A new instance of CLI.
- #justify(longest, string) ⇒ Object
- #right_float(target, str) ⇒ Object
- #run ⇒ Object
- #tabular(rows, indent = 2, separator = "|") ⇒ Object
Constructor Details
Instance Method Details
#clear ⇒ Object
100 101 102 |
# File 'lib/resque-top/cli.rb', line 100 def clear print "\033[2J" end |
#command_exists?(command) ⇒ Boolean
104 105 106 |
# File 'lib/resque-top/cli.rb', line 104 def command_exists?(command) ENV['PATH'].split(File::PATH_SEPARATOR).any? { |d| File.exists? File.join(d, command) } end |
#detect_terminal_size ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/resque-top/cli.rb', line 109 def detect_terminal_size if (ENV['COLUMNS'] =~ /^\d+$/) && (ENV['LINES'] =~ /^\d+$/) [ENV['COLUMNS'].to_i, ENV['LINES'].to_i] elsif (RUBY_PLATFORM =~ /java/ || (!STDIN.tty? && ENV['TERM'])) && command_exists?('tput') [`tput cols`.to_i, `tput lines`.to_i] elsif STDIN.tty? && command_exists?('stty') `stty size`.scan(/\d+/).map { |s| s.to_i }.reverse else nil end rescue Exception => e nil end |
#display ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 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 94 95 96 97 98 |
# File 'lib/resque-top/cli.rb', line 55 def display out = [] out << "Resque connected to: #{Resque.redis_id} (#{Resque.redis.namespace})" out << "" out << "Queues: " rows = [] Resque.queues.each do |queue| rows << [ queue, Resque.size(queue) ] end rows << [ "failed", Resque::Failure.count ] out.push *(tabular(rows)) out << "" out << "Workers:" workers = Resque.workers.sort_by { |w| w.to_s } workers.each do |worker| line = '' host, pid, queues = worker.to_s.split(':') cols = [] cols << [ "#{host}:#{pid}", queues ] data = worker.processing || {} if data['queue'] cols[-1].push "#{data['payload']['class']} (#{data['run_at']})" else cols[-1].push "Waiting for a job...." end out.push *(tabular(cols)) end if workers.empty? out << " (There are no registered workers)" end (@height - out.size - 2).times do out << "" end out << "(Ctrl-c to quit.)" right_float(out[0], Time.now.strftime('%H:%M:%S')) clear puts out.join("\n") end |
#justify(longest, string) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/resque-top/cli.rb', line 47 def justify(longest, string) if string.is_a?(Integer) string.to_s.rjust(longest) else string.ljust(longest) end end |
#right_float(target, str) ⇒ Object
24 25 26 |
# File 'lib/resque-top/cli.rb', line 24 def right_float(target, str) target << (' ' * (@width - target.length - str.length)) + str end |
#run ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/resque-top/cli.rb', line 16 def run loop do display sleep 1 end rescue Interrupt end |
#tabular(rows, indent = 2, separator = "|") ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/resque-top/cli.rb', line 28 def tabular(rows, indent=2, separator="|") if rows.empty? nil else longest = [0] * rows[0].count rows.each do |cols| cols.each_with_index do |value, i| longest[i] = [ longest[i], value.to_s.length ].max end end rows.collect do |row| longest.each_with_index do |value, i| row[i] = justify(value, row[i]) end (" " * indent) + row.join(" | ") end end end |