Module: Bunnish::Command::Status

Defined in:
lib/bunnish/command/status.rb

Class Method Summary collapse

Class Method Details

.run(argv, input_stream = $stdin, output_stream = $stdout) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
# File 'lib/bunnish/command/status.rb', line 3

def self.run(argv, input_stream=$stdin, output_stream=$stdout)
  
  params = Bunnish.parse_opts(argv)
  
  host = params[:host]
  port = params[:port]
  user = params[:user]
  password = params[:password]
  durable = params[:durable]
  empty_list_max = params[:durable]
  
  warn_size = params[:warn_size] || 100000
  error_size = params[:error_size] || 500000
  
  warn_flag = false
  error_flag = false
  
  queue_name_list = argv.shift

  if queue_name_list.nil?
    Bunnish.logger.error("queue-name is not set")
    return 0
  end
  
  queue_name_list = queue_name_list.split(/[, \r\n]/)
  queue_name_list.delete('')
  
  begin
    bunny = Bunny.new(:logging => false, :spec => '09', :host=>host, :port=>port, :user=>user, :pass=>password)
  
    # start a communication session with the amqp server
    bunny.start
    
    empty_queue_list = []
    
    queue_name_list.each do |queue_name|
    
      if queue_name == '' then
        next
      end
      
      # create/get queue
      queue = bunny.queue(queue_name, :durable=>durable)
      
      message_count = queue.status[:message_count]
      
      empty_queue_list.push queue_name if message_count == 0
      
      if 0 < message_count then
        if error_size < message_count then
          output_stream.puts "(ERROR) #{queue_name} : #{queue.status[:message_count]} messages(> #{error_size}), #{queue.status[:consumer_count]} consumers"
          error_flag = true
        elsif warn_size < message_count then
          output_stream.puts "(WARNING) #{queue_name} : #{queue.status[:message_count]} messages(> #{warn_size}), #{queue.status[:consumer_count]} consumers"
          warn_flag = true
        else
          output_stream.puts "#{queue_name} : #{queue.status[:message_count]} messages, #{queue.status[:consumer_count]} consumers"
        end
      end
    end
    
    empty_count = empty_queue_list.size
    
    if empty_queue_list != [] then
      if empty_count == 1 then
        output_stream.puts "#{empty_count} queue is empty:"
      else
        output_stream.puts "#{empty_count} queues are empty:"
      end
      
      if empty_list_max then
        empty_queue_list = empty_queue_list[0..(empty_list_max-1)]
      end
      
      empty_queue_list.each do |queue_name|
        output_stream.puts "  #{queue_name}"
      end
      rest_count = empty_count - empty_queue_list.size
      output_stream.puts "  ..." if 0 < rest_count
    end
    # Close client
    bunny.stop
  
  rescue Exception=>e
    message = Time.now.strftime("[%Y-%m-%d %H:%M:%S](EXCEPTION)#{e.message}(#{e.class.name}): #{e.backtrace.map{|s| "  #{s}"}.join("\n")}")
    output_stream.puts message
    return 1
  end
  
  return 1 if error_flag
  return 2 if warn_flag
  return 0
end