bugs_bunny 0.0.9 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.9
1
+ 0.1.0
data/bin/bbunny CHANGED
@@ -1,8 +1,9 @@
1
1
  #!/usr/bin/env ruby
2
- # $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
3
  require 'rubygems'
4
4
  require 'optparse'
5
5
  require 'bugs_bunny'
6
+ include BugsBunny
6
7
 
7
8
  opt = OptionParser.new do |o|
8
9
  o.banner = <<BANNER
@@ -33,6 +34,7 @@ Options:
33
34
  BANNER
34
35
  o.on('-c CONFIG', "Config file path") { |path| Opt[:config] = path }
35
36
  o.on('-p PATH', "Output path for config file") { |path| Opt[:out] = path }
37
+ o.on('-m MODE', "marshal, json or plain") { |m| Opt[:mode] = m }
36
38
  o.on('-v', "--verbose") { |b| Opt[:verbose] = true }
37
39
  o.on('-f', "--force") { |b| Opt[:force] = true }
38
40
  o.on('-h') { puts o; exit }
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{bugs_bunny}
8
- s.version = "0.0.9"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Marcos Piccinini"]
12
- s.date = %q{2010-03-03}
12
+ s.date = %q{2010-03-05}
13
13
  s.default_executable = %q{bbunny}
14
14
  s.description = %q{Play/Manage RabbitMQ. Vhosts, queues.}
15
15
  s.email = %q{x@nofxx.com}
@@ -35,9 +35,10 @@ Gem::Specification.new do |s|
35
35
  "features/support/env.rb",
36
36
  "lib/bugs_bunny.rb",
37
37
  "lib/bugs_bunny/cli.rb",
38
+ "lib/bugs_bunny/helper.rb",
39
+ "lib/bugs_bunny/queue.rb",
38
40
  "lib/bugs_bunny/rabbit.rb",
39
41
  "lib/bugs_bunny/rabbitmq.yml",
40
- "lib/bugs_bunny/record.rb",
41
42
  "spec/bugs_bunny/cli_spec.rb",
42
43
  "spec/bugs_bunny/rabbit_spec.rb",
43
44
  "spec/bugs_bunny_spec.rb",
@@ -8,7 +8,7 @@ Feature: Managing queues
8
8
  Then I should have 1 queues
9
9
  When I run "queues"
10
10
  Then I should see "todo"
11
- And I should see "0 messages"
11
+ And I should see "0"
12
12
 
13
13
  Scenario: Show queues
14
14
  Given a queue "crucification" with 3 messages
@@ -19,10 +19,14 @@ Feature: Managing queues
19
19
  Scenario: List queues
20
20
  Given I run "queues"
21
21
  Then I should see "Queues"
22
+ And I should see "Name"
22
23
  And I should see "todo"
24
+ And I should see "(V)"
23
25
  And I should see "crucification"
24
- And I should see "0 messages"
25
- And I should see "3 messages"
26
+ And I should see "Msgs"
27
+ And I should see "3"
28
+ And I should see "Users"
29
+ And I should see "0"
26
30
 
27
31
  Scenario: Add to queue
28
32
  Given a queue "works"
@@ -75,3 +79,10 @@ Feature: Managing queues
75
79
  And I should see "0 messages"
76
80
  When I run "queue crucification"
77
81
  Then I should see "0 messages"
82
+
83
+ Scenario: JSON queues
84
+ When I run 'queue works add "{\"foo\": 2}"'
85
+ And I run "queue works list -m json"
86
+ Then I should see "QUEUE 1"
87
+ And I should see "works"
88
+ And I should see "Body"
@@ -1,4 +1,4 @@
1
- When /^I run "([^\"]*)"$/ do |comm|
1
+ When /^I run [\"|\'](.*)[\"|\']$/ do |comm|
2
2
  @out = `bin/bbunny #{comm}`
3
3
  end
4
4
 
@@ -16,5 +16,5 @@ end
16
16
 
17
17
  Then /^I should have (\d+) queues*$/ do |n|
18
18
  Given 'I run "queues"'
19
- @out.split("\n").length.should eql(n.to_i+1)
19
+ @out.split("\n").length.should eql(n.to_i+6) #table adds 6 lines
20
20
  end
@@ -9,9 +9,10 @@ rescue LoadError
9
9
  end
10
10
 
11
11
  module BugsBunny
12
- Opt = {:verbose => false, :force => false, :rabbit => {}}
12
+ Opt = {:verbose => false, :force => false, :rabbit => {}, :mode => :marshal}
13
13
  end
14
14
 
15
+ require "bugs_bunny/helper"
15
16
  require "bugs_bunny/cli"
16
17
  require "bugs_bunny/rabbit"
17
- require "bugs_bunny/record"
18
+ require "bugs_bunny/queue"
@@ -0,0 +1,55 @@
1
+ module BugsBunny
2
+ module Helper
3
+
4
+ def print_queue(h, body)
5
+ print ["-------\n", "QUEUE #{h.delivery_tag} (#{h.content_type}): ",
6
+ ("Redelivered " if h.redelivered), "Mode #{h.delivery_mode}",
7
+ "\nConsumer: #{h.consumer_tag} \nExchange: #{h.exchange}",
8
+ "\n\nBody:", read_dump(body), "\n"].reject(&:nil?).join
9
+ end
10
+
11
+ def read_dump(dump)
12
+ case BugsBunny::Opt[:mode].to_sym
13
+ when :marshal then Marshal.load(dump)
14
+ when :json then JSON.load(dump)
15
+ else dump
16
+ end
17
+ rescue
18
+ dump
19
+ end
20
+
21
+ # http://gist.github.com/72234
22
+ def print_table(title, items, *fields)
23
+ return if items.empty?
24
+ puts title
25
+ #find max length for each field; start with the field names themselves
26
+ fields = items.first.class.column_names unless fields.any?
27
+ max_len = Hash[*fields.map {|f| [f, f.to_s.length]}.flatten]
28
+ items.each do |item|
29
+ fields.each do |field|
30
+ len = item.send(field).to_s.length
31
+ max_len[field] = len if len > max_len[field]
32
+ end
33
+ end
34
+
35
+ border = '+-' + fields.map {|f| '-' * max_len[f] }.join('-+-') + '-+'
36
+ title_row = '| ' + fields.map do |f|
37
+ sprintf("%-#{max_len[f]}s", f.to_s.split("_")[0].capitalize)
38
+ end.join(' | ') + ' |'
39
+
40
+ puts border
41
+ puts title_row
42
+ puts border
43
+
44
+ items.each do |item|
45
+ row = '| ' + fields.map do |f|
46
+ sprintf("%-#{max_len[f]}s", item.send(f))
47
+ end.join(' | ') + ' |'
48
+ puts row
49
+ end
50
+
51
+ puts border
52
+ puts "#{items.length} rows in set\n"
53
+ end
54
+ end
55
+ end
@@ -1,15 +1,16 @@
1
1
  module BugsBunny
2
- class Record
3
- attr_reader :name, :msgs, :users
4
- @records = []
2
+ class Queue
3
+ include Helper
4
+ attr_reader :name, :msgs, :users, :ready, :noack, :commit, :acts, :memory
5
+ @queues = []
5
6
 
6
7
  def self.all
7
8
  # possible with amqp?
8
9
  `rabbitmqctl list_queues -p #{Opt[:rabbit][:vhost]} name durable auto_delete arguments node messages_ready messages_unacknowledged messages_uncommitted messages consumers transactions memory`.split("\n").each do |l|
9
10
  next if l =~ /Listing|\.\./
10
- @records << BugsBunny::Record.new(*l.split("\t"))
11
+ @queues << BugsBunny::Queue.new(*l.split("\t"))
11
12
  end
12
- @records
13
+ @queues
13
14
  end
14
15
 
15
16
  def self.create(name)
@@ -19,8 +20,8 @@ module BugsBunny
19
20
  end
20
21
 
21
22
  def initialize(*params)
22
- @name, d, a,_,_,ready,_,all,@users, @transactions, @mem = *params
23
- @msgs = ready.to_i + all.to_i
23
+ @name, d, a, args, @node, @ready, @noack, @commit, @msgs,
24
+ @users, @acts, @memory = *params
24
25
  @durable = eval(d) if d #ugly
25
26
  @auto_delete = eval(a) if a #more ugly
26
27
  @mq = MQ.queue(@name, :passive => true)
@@ -42,15 +43,7 @@ module BugsBunny
42
43
  def inspect
43
44
  puts ""
44
45
  @mq.bind(MQ.fanout(@name)).subscribe(:ack => true) do |h, body| #, :nowait => false
45
- puts "-------\n"
46
- print "QUEUE #{h.delivery_tag} (#{h.content_type}): "
47
- print "Redelivered " if h.redelivered
48
- puts "Mode #{h.delivery_mode}"
49
- puts "Consumer: #{h.consumer_tag} Exchange: #{h.exchange}"
50
- txt = read(body)
51
- puts "\nBody:"
52
- puts txt
53
- puts
46
+ print_queue(h, body)
54
47
  end
55
48
  end
56
49
  alias :live :inspect
@@ -83,7 +76,7 @@ module BugsBunny
83
76
  end
84
77
 
85
78
  def to_s
86
- "#{@name} #{durable}: #{@msgs} messages"
79
+ "#{@name} #{kind}: #{@msgs} messages"
87
80
  end
88
81
 
89
82
  def <=>(other)
@@ -91,18 +84,10 @@ module BugsBunny
91
84
  end
92
85
 
93
86
  private
94
-
95
- def read(dump, mode=:marshal)
96
- case mode
97
- when :marshal then Marshal.load(dump)
98
- when :json then JSON.load(dump)
99
- else dump
100
- end
101
- rescue
102
- dump
87
+ def name_kind
88
+ "(#{kind[0].chr}) #{@name}"
103
89
  end
104
-
105
- def durable
90
+ def kind
106
91
  @durable ? "Durable" : "Volatile"
107
92
  end
108
93
 
@@ -2,6 +2,7 @@
2
2
  module BugsBunny
3
3
 
4
4
  class Rabbit
5
+ include Helper
5
6
 
6
7
  def initialize(params=nil)
7
8
  end
@@ -18,12 +19,13 @@ module BugsBunny
18
19
  def queues(param=nil,name=nil)
19
20
  if param == "new"
20
21
  return halt("new <name>") unless name
21
- BugsBunny::Record.create(name)
22
+ BugsBunny::Queue.create(name)
22
23
  return halt
23
24
  end
24
- qs = BugsBunny::Record.all
25
+ qs = BugsBunny::Queue.all
25
26
  unless param
26
- print_table "Queues", qs.sort_by { |r| r.msgs }
27
+ print_table "Queues", qs.sort_by { |r| r.msgs }, :msgs, :name_kind,
28
+ :users, :ready, :noack, :commit, :acts, :memory
27
29
  else
28
30
  qs.each(&:"#{param}")
29
31
  end
@@ -31,7 +33,7 @@ module BugsBunny
31
33
  end
32
34
 
33
35
  def queue(q, action="info", *params)
34
- rec = BugsBunny::Record.new(q)
36
+ rec = BugsBunny::Queue.new(q)
35
37
  if rec.respond_to?(action)
36
38
  rec.send(action, *params)
37
39
  else
@@ -44,13 +46,6 @@ module BugsBunny
44
46
  print_table "Queues", @mq.exchanges
45
47
  end
46
48
 
47
- def print_table(title, arr)
48
- return if arr.empty?
49
- puts title
50
- arr.each do |q|
51
- puts q
52
- end
53
- end
54
49
 
55
50
  def halt(msg=nil)
56
51
  BugsBunny::Rabbit.halt(msg)
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
+ - 1
7
8
  - 0
8
- - 9
9
- version: 0.0.9
9
+ version: 0.1.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Marcos Piccinini
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-03 00:00:00 -03:00
17
+ date: 2010-03-05 00:00:00 -03:00
18
18
  default_executable: bbunny
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -83,9 +83,10 @@ files:
83
83
  - features/support/env.rb
84
84
  - lib/bugs_bunny.rb
85
85
  - lib/bugs_bunny/cli.rb
86
+ - lib/bugs_bunny/helper.rb
87
+ - lib/bugs_bunny/queue.rb
86
88
  - lib/bugs_bunny/rabbit.rb
87
89
  - lib/bugs_bunny/rabbitmq.yml
88
- - lib/bugs_bunny/record.rb
89
90
  - spec/bugs_bunny/cli_spec.rb
90
91
  - spec/bugs_bunny/rabbit_spec.rb
91
92
  - spec/bugs_bunny_spec.rb