bunnish 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +18 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +19 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/bin/bunnish +18 -0
- data/lib/bunnish/count.rb +35 -0
- data/lib/bunnish/help.rb +9 -0
- data/lib/bunnish/publish.rb +177 -0
- data/lib/bunnish/status.rb +94 -0
- data/lib/bunnish/subscribe.rb +148 -0
- data/lib/bunnish.rb +120 -0
- data/spec/bunnish_spec.rb +4 -0
- data/spec/spec_helper.rb +12 -0
- metadata +183 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
|
9
|
+
gem "json"
|
10
|
+
gem "bunny"
|
11
|
+
|
12
|
+
group :development do
|
13
|
+
gem "rspec", "~> 2.8.0"
|
14
|
+
gem "rdoc", "~> 3.12"
|
15
|
+
gem "bundler"
|
16
|
+
gem "jeweler", "~> 1.8.4"
|
17
|
+
gem "rcov", ">= 0"
|
18
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Kenji Hara
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= bunnish
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Contributing to bunnish
|
6
|
+
|
7
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
8
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
9
|
+
* Fork the project.
|
10
|
+
* Start a feature/bugfix branch.
|
11
|
+
* Commit and push until you are happy with your contribution.
|
12
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
13
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2012 Kenji Hara. See LICENSE.txt for
|
18
|
+
further details.
|
19
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "bunnish"
|
18
|
+
gem.homepage = "http://github.com/haracane/bunnish"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Command for AMQP access to Message Queue.}
|
21
|
+
gem.description = %Q{Command for AMQP access to Message Queue.}
|
22
|
+
gem.email = "haracane@gmail.com"
|
23
|
+
gem.authors = ["Kenji Hara"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rspec/core'
|
29
|
+
require 'rspec/core/rake_task'
|
30
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
|
+
end
|
33
|
+
|
34
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
35
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
36
|
+
spec.rcov = true
|
37
|
+
end
|
38
|
+
|
39
|
+
task :default => :spec
|
40
|
+
|
41
|
+
require 'rdoc/task'
|
42
|
+
Rake::RDocTask.new do |rdoc|
|
43
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
44
|
+
|
45
|
+
rdoc.rdoc_dir = 'rdoc'
|
46
|
+
rdoc.title = "bunnish #{version}"
|
47
|
+
rdoc.rdoc_files.include('README*')
|
48
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/bin/bunnish
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bunnish"
|
4
|
+
|
5
|
+
command = ARGV.shift
|
6
|
+
|
7
|
+
case command
|
8
|
+
when "count"
|
9
|
+
Bunnish::Count.run(ARGV)
|
10
|
+
when "help"
|
11
|
+
Bunnish::Help.run(ARGV)
|
12
|
+
when "publish"
|
13
|
+
Bunnish::Publish.run(ARGV)
|
14
|
+
when "status"
|
15
|
+
Bunnish::Status.run(ARGV)
|
16
|
+
when "subscribe"
|
17
|
+
Bunnish::Subscribe.run(ARGV)
|
18
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Bunnish
|
2
|
+
module Count
|
3
|
+
def self.run(argv, input_stream=$stdin, output_stream=$stdout, error_stream=$stderr)
|
4
|
+
input_stream ||= $stdin
|
5
|
+
output_stream ||= $stdout
|
6
|
+
error_stream ||= $stderr
|
7
|
+
|
8
|
+
params = Bunnish.parse_opts(argv)
|
9
|
+
|
10
|
+
host = params[:host]
|
11
|
+
port = params[:port]
|
12
|
+
user = params[:user]
|
13
|
+
password = params[:password]
|
14
|
+
durable = params[:durable]
|
15
|
+
|
16
|
+
queue_name = argv[0]
|
17
|
+
|
18
|
+
bunny = Bunny.new(:logging => false, :spec => '09', :host=>host, :port=>port, :user=>user, :pass=>password)
|
19
|
+
|
20
|
+
# start a communication session with the amqp server
|
21
|
+
bunny.start
|
22
|
+
|
23
|
+
# create/get queue
|
24
|
+
queue = bunny.queue(queue_name, :durable=>durable)
|
25
|
+
|
26
|
+
# remain_count = queue.status[:message_count]
|
27
|
+
|
28
|
+
output_stream.puts queue.status[:message_count]
|
29
|
+
|
30
|
+
# Close client
|
31
|
+
bunny.stop
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/bunnish/help.rb
ADDED
@@ -0,0 +1,177 @@
|
|
1
|
+
module Bunnish
|
2
|
+
module Publish
|
3
|
+
def self.output_log(streams, message)
|
4
|
+
streams.each do |stream|
|
5
|
+
if stream then
|
6
|
+
stream.puts message
|
7
|
+
stream.flush
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.output_publish_log(streams, queue, count, log_label)
|
13
|
+
message_count = '?'
|
14
|
+
consumer_count = '?'
|
15
|
+
begin
|
16
|
+
message_count = queue.status[:message_count]
|
17
|
+
consumer_count = queue.status[:consumer_count]
|
18
|
+
rescue Exception=>e
|
19
|
+
end
|
20
|
+
|
21
|
+
self.output_log streams, Time.now.strftime("[%Y-%m-%d %H:%M:%S](INFO)#{log_label} published #{count} messages into #{queue.name}(#{message_count} messages, #{consumer_count} consumers)")
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.run(argv, input_stream=$stdin, output_stream=$stdout, error_stream=$stderr)
|
25
|
+
input_stream ||= $stdin
|
26
|
+
output_stream ||= $stdout
|
27
|
+
error_stream ||= $stderr
|
28
|
+
|
29
|
+
params = Bunnish.parse_opts(argv)
|
30
|
+
|
31
|
+
host = params[:host]
|
32
|
+
port = params[:port]
|
33
|
+
user = params[:user]
|
34
|
+
password = params[:password]
|
35
|
+
durable = params[:durable]
|
36
|
+
exchange_name = params[:exchange_name]
|
37
|
+
unit_size = params[:unit_size] || 100000
|
38
|
+
raise_exception_flag = params[:raise_exception_flag]
|
39
|
+
delimiter = params[:delimiter]
|
40
|
+
|
41
|
+
log_label = params[:log_label]
|
42
|
+
log_dir = params[:log_dir]
|
43
|
+
log_path = params[:log_path]
|
44
|
+
|
45
|
+
|
46
|
+
queue_name_list = argv.shift.split(/[, \r\n]/)
|
47
|
+
queue_name_list.delete('')
|
48
|
+
|
49
|
+
if delimiter
|
50
|
+
delimiter_crlf = "#{delimiter}\r\n"
|
51
|
+
delimiter_lf = "#{delimiter}\n"
|
52
|
+
end
|
53
|
+
|
54
|
+
log_streams = {}
|
55
|
+
|
56
|
+
queue_name_list.each do |queue_name|
|
57
|
+
log_path = "#{log_dir}/#{queue_name.gsub(/[\/]/, "_")}.log" if log_dir
|
58
|
+
if log_path then
|
59
|
+
log_stream = open(log_path, "a")
|
60
|
+
log_streams[queue_name] = log_stream
|
61
|
+
error_stream.puts Time.now.strftime("[%Y-%m-%d %H:%M:%S](INFO)#{log_label} output log into #{log_path}")
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
fanout_flag = (exchange_name != '' && 1 < queue_name_list.size)
|
66
|
+
|
67
|
+
bunny = nil
|
68
|
+
direct_exchange = nil
|
69
|
+
|
70
|
+
publish_flag = false
|
71
|
+
|
72
|
+
begin
|
73
|
+
direct_exchange_list = nil
|
74
|
+
|
75
|
+
# publish message to exchange
|
76
|
+
count = 0
|
77
|
+
|
78
|
+
lines = []
|
79
|
+
|
80
|
+
|
81
|
+
while line = input_stream.gets do
|
82
|
+
if bunny == nil then
|
83
|
+
bunny = Bunny.new(:logging => false, :spec => '09', :host=>host, :port=>port, :user=>user, :pass=>password)
|
84
|
+
|
85
|
+
# start a communication session with the amqp server
|
86
|
+
bunny.start
|
87
|
+
|
88
|
+
# create/get exchange
|
89
|
+
if fanout_flag then
|
90
|
+
error_stream.puts Time.now.strftime("[%Y-%m-%d %H:%M:%S]") + "(INFO)#{log_label} create fanout exchange '#{exchange_name}'"
|
91
|
+
fanout_exchange = bunny.exchange(exchange_name, :type=>:fanout, :persistent=>durable)
|
92
|
+
queue_name_list.each do |queue_name|
|
93
|
+
# create/get queue
|
94
|
+
error_stream.puts Time.now.strftime("[%Y-%m-%d %H:%M:%S]") + "(INFO)#{log_label} create queue '#{queue_name}'"
|
95
|
+
queue = bunny.queue(queue_name, :durable=>durable)
|
96
|
+
error_stream.puts Time.now.strftime("[%Y-%m-%d %H:%M:%S]") + "(INFO)#{log_label} bind queue '#{queue_name}' to fanout exchange '#{exchange_name}'"
|
97
|
+
queue.bind(fanout_exchange)
|
98
|
+
self.output_log [error_stream, log_streams[queue_name]], Time.now.strftime("[%Y-%m-%d %H:%M:%S](INFO)#{log_label} publish to #{queue_name}(#{queue.status[:message_count]} messages, #{queue.status[:consumer_count]} consumers)")
|
99
|
+
end
|
100
|
+
else
|
101
|
+
direct_exchange_list = queue_name_list.map { |queue_name|
|
102
|
+
|
103
|
+
error_stream.puts Time.now.strftime("[%Y-%m-%d %H:%M:%S]") + "(INFO)#{log_label} create direct exchange '#{queue_name}'"
|
104
|
+
direct_exchange = bunny.exchange(queue_name, :type=>:direct)
|
105
|
+
|
106
|
+
error_stream.puts Time.now.strftime("[%Y-%m-%d %H:%M:%S]") + "(INFO)#{log_label} create queue '#{queue_name}'"
|
107
|
+
queue = bunny.queue(queue_name, :durable=>durable)
|
108
|
+
|
109
|
+
error_stream.puts Time.now.strftime("[%Y-%m-%d %H:%M:%S]") + "(INFO)#{log_label} bind queue '#{queue_name}' to direct exchange '#{queue_name}'"
|
110
|
+
queue.bind(direct_exchange)
|
111
|
+
self.output_log [error_stream, log_streams[queue_name]], Time.now.strftime("[%Y-%m-%d %H:%M:%S](INFO)#{log_label} publish to #{queue_name}(#{queue.status[:message_count]} messages, #{queue.status[:consumer_count]} consumers)")
|
112
|
+
|
113
|
+
direct_exchange
|
114
|
+
}
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
if delimiter
|
119
|
+
lines.push line
|
120
|
+
next if /^#{Regexp.escape(delimiter)}\r?\n$/ !~ line
|
121
|
+
message = lines.join
|
122
|
+
lines.clear
|
123
|
+
else
|
124
|
+
message = line
|
125
|
+
end
|
126
|
+
|
127
|
+
if fanout_flag then
|
128
|
+
fanout_exchange.publish(message)
|
129
|
+
else
|
130
|
+
direct_exchange_list.each do |direct_exchange|
|
131
|
+
direct_exchange.publish(message)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
count += 1
|
135
|
+
|
136
|
+
if unit_size <= count then
|
137
|
+
publish_flag = true
|
138
|
+
queue_name_list.each do |queue_name|
|
139
|
+
queue = bunny.queue(queue_name, :durable=>durable)
|
140
|
+
log_stream = log_streams[queue_name]
|
141
|
+
self.output_publish_log [error_stream, log_stream], queue, count, log_label
|
142
|
+
end
|
143
|
+
count = 0
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
publish_flag = true if 0 < count
|
148
|
+
|
149
|
+
queue_name_list.each do |queue_name|
|
150
|
+
log_stream = log_streams[queue_name]
|
151
|
+
if bunny then
|
152
|
+
queue = bunny.queue(queue_name, :durable=>durable)
|
153
|
+
self.output_publish_log [error_stream, log_stream], queue, count, log_label if 0 < count || !publish_flag
|
154
|
+
else
|
155
|
+
self.output_log [error_stream, log_stream], Time.now.strftime("[%Y-%m-%d %H:%M:%S](INFO)#{log_label} no input for #{queue_name}")
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
bunny.stop if bunny
|
160
|
+
|
161
|
+
rescue Exception=>e
|
162
|
+
if raise_exception_flag then
|
163
|
+
bunny.stop if bunny
|
164
|
+
raise e if raise_exception_flag
|
165
|
+
else
|
166
|
+
message = Time.now.strftime("[%Y-%m-%d %H:%M:%S](EXCEPTION)#{log_label} #{e.message}(#{e.class.name}): #{e.backtrace.map{|s| " #{s}"}.join("\n")}")
|
167
|
+
self.output_log(([error_stream] + log_streams.values), message)
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
log_streams.values.each do |log_stream|
|
172
|
+
log_stream.close
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
end
|
177
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
module Bunnish
|
2
|
+
module Status
|
3
|
+
def self.run(argv, input_stream=$stdin, output_stream=$stdout, error_stream=$stderr)
|
4
|
+
input_stream ||= $stdin
|
5
|
+
output_stream ||= $stdout
|
6
|
+
error_stream ||= $stderr
|
7
|
+
|
8
|
+
params = Bunnish.parse_opts(argv)
|
9
|
+
|
10
|
+
host = params[:host]
|
11
|
+
port = params[:port]
|
12
|
+
user = params[:user]
|
13
|
+
password = params[:password]
|
14
|
+
durable = params[:durable]
|
15
|
+
empty_list_max = params[:durable]
|
16
|
+
|
17
|
+
warn_size = params[:warn_size] || 100000
|
18
|
+
error_size = params[:error_size] || 500000
|
19
|
+
|
20
|
+
warn_flag = false
|
21
|
+
error_flag = false
|
22
|
+
|
23
|
+
queue_name_list = argv.shift.split(/[, ]+/)
|
24
|
+
|
25
|
+
begin
|
26
|
+
bunny = Bunny.new(:logging => false, :spec => '09', :host=>host, :port=>port, :user=>user, :pass=>password)
|
27
|
+
|
28
|
+
# start a communication session with the amqp server
|
29
|
+
bunny.start
|
30
|
+
|
31
|
+
empty_queue_list = []
|
32
|
+
|
33
|
+
queue_name_list.each do |queue_name|
|
34
|
+
|
35
|
+
if queue_name == '' then
|
36
|
+
next
|
37
|
+
end
|
38
|
+
|
39
|
+
# create/get queue
|
40
|
+
queue = bunny.queue(queue_name, :durable=>durable)
|
41
|
+
|
42
|
+
message_count = queue.status[:message_count]
|
43
|
+
|
44
|
+
empty_queue_list.push queue_name if message_count == 0
|
45
|
+
|
46
|
+
if 0 < message_count then
|
47
|
+
if error_size < message_count then
|
48
|
+
output_stream.puts "(ERROR) #{queue_name} : #{queue.status[:message_count]} messages(> #{error_size}), #{queue.status[:consumer_count]} consumers"
|
49
|
+
error_flag = true
|
50
|
+
elsif warn_size < message_count then
|
51
|
+
output_stream.puts "(WARNING) #{queue_name} : #{queue.status[:message_count]} messages(> #{warn_size}), #{queue.status[:consumer_count]} consumers"
|
52
|
+
warn_flag = true
|
53
|
+
else
|
54
|
+
output_stream.puts "#{queue_name} : #{queue.status[:message_count]} messages, #{queue.status[:consumer_count]} consumers"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
empty_count = empty_queue_list.size
|
60
|
+
|
61
|
+
if empty_queue_list != [] then
|
62
|
+
if empty_count == 1 then
|
63
|
+
output_stream.puts "#{empty_count} queue is empty:"
|
64
|
+
else
|
65
|
+
output_stream.puts "#{empty_count} queues are empty:"
|
66
|
+
end
|
67
|
+
|
68
|
+
if empty_list_max then
|
69
|
+
empty_queue_list = empty_queue_list[0..(empty_list_max-1)]
|
70
|
+
end
|
71
|
+
|
72
|
+
empty_queue_list.each do |queue_name|
|
73
|
+
output_stream.puts " #{queue_name}"
|
74
|
+
end
|
75
|
+
rest_count = empty_count - empty_queue_list.size
|
76
|
+
output_stream.puts " ..." if 0 < rest_count
|
77
|
+
end
|
78
|
+
# Close client
|
79
|
+
bunny.stop
|
80
|
+
|
81
|
+
rescue Exception=>e
|
82
|
+
message = Time.now.strftime("[%Y-%m-%d %H:%M:%S](EXCEPTION)#{e.message}(#{e.class.name}): #{e.backtrace.map{|s| " #{s}"}.join("\n")}")
|
83
|
+
output_stream.puts message
|
84
|
+
exit 1
|
85
|
+
end
|
86
|
+
|
87
|
+
exit 1 if error_flag
|
88
|
+
exit 2 if warn_flag
|
89
|
+
exit 0
|
90
|
+
|
91
|
+
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
@@ -0,0 +1,148 @@
|
|
1
|
+
module Bunnish
|
2
|
+
module Subscribe
|
3
|
+
def self.output_log(streams, message)
|
4
|
+
streams.each do |stream|
|
5
|
+
if stream then
|
6
|
+
stream.puts message
|
7
|
+
stream.flush
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.output_subscribe_log(streams, queue, count, log_label)
|
13
|
+
message_count = '?'
|
14
|
+
consumer_count = '?'
|
15
|
+
begin
|
16
|
+
message_count = queue.status[:message_count]
|
17
|
+
consumer_count = queue.status[:consumer_count]
|
18
|
+
rescue Exception=>e
|
19
|
+
end
|
20
|
+
|
21
|
+
self.output_log streams, Time.now.strftime("[%Y-%m-%d %H:%M:%S](INFO)#{log_label} subscribed #{count} messages from #{queue.name}(#{message_count} messages, #{consumer_count} consumers)")
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.run(argv, input_stream=$stdin, output_stream=$stdout, error_stream=$stderr)
|
25
|
+
params = Bunnish.parse_opts(argv)
|
26
|
+
|
27
|
+
host = params[:host]
|
28
|
+
port = params[:port]
|
29
|
+
user = params[:user]
|
30
|
+
password = params[:password]
|
31
|
+
durable = params[:durable]
|
32
|
+
exchange_name = params[:exchange_name]
|
33
|
+
unit_size = params[:unit_size] || 10000
|
34
|
+
raise_exception_flag = params[:raise_exception_flag]
|
35
|
+
ack = params[:ack]
|
36
|
+
consumer_tag = params[:consumer_tag]
|
37
|
+
exclusive = params[:exclusive]
|
38
|
+
message_max = params[:message_max]
|
39
|
+
timeout = params[:timeout]
|
40
|
+
current_all_flag = params[:current_all_flag]
|
41
|
+
min_size = params[:min_size]
|
42
|
+
|
43
|
+
log_label = params[:log_label]
|
44
|
+
log_dir = params[:log_dir]
|
45
|
+
log_path = params[:log_path]
|
46
|
+
|
47
|
+
queue_name = argv.shift
|
48
|
+
|
49
|
+
log_stream = nil
|
50
|
+
|
51
|
+
log_path = "#{log_dir}/#{queue_name.gsub(/[\/]/, "_")}.log" if log_dir
|
52
|
+
|
53
|
+
if log_path
|
54
|
+
log_stream = open(log_path, "a")
|
55
|
+
error_stream.puts Time.now.strftime("[%Y-%m-%d %H:%M:%S](INFO)#{log_label} output log into #{log_path}")
|
56
|
+
end
|
57
|
+
|
58
|
+
exchange_name = queue_name
|
59
|
+
|
60
|
+
bunny = Bunny.new(:logging => false, :spec => '09', :host=>host, :port=>port, :user=>user, :pass=>password)
|
61
|
+
|
62
|
+
|
63
|
+
# start a communication session with the amqp server
|
64
|
+
bunny.start
|
65
|
+
|
66
|
+
# create/get queue
|
67
|
+
queue = bunny.queue(queue_name, :durable=>durable)
|
68
|
+
|
69
|
+
remain_count = queue.status[:message_count]
|
70
|
+
consumer_count = queue.status[:consumer_count]
|
71
|
+
|
72
|
+
message_max = 'current-size' if current_all_flag
|
73
|
+
|
74
|
+
if message_max == 'current-size' then
|
75
|
+
message_max = remain_count
|
76
|
+
elsif min_size
|
77
|
+
message_max = [remain_count - min_size, 0].max
|
78
|
+
else
|
79
|
+
message_max = message_max.to_i if message_max
|
80
|
+
end
|
81
|
+
|
82
|
+
if message_max
|
83
|
+
self.output_log [error_stream, log_stream], Time.now.strftime("[%Y-%m-%d %H:%M:%S](INFO)#{log_label} subscribe #{message_max} messages from #{queue_name}(#{remain_count} messages, #{consumer_count} consumers)")
|
84
|
+
|
85
|
+
if message_max <= 0
|
86
|
+
self.output_log [error_stream, log_stream], Time.now.strftime("[%Y-%m-%d %H:%M:%S](INFO)#{log_label} finished")
|
87
|
+
bunny.stop
|
88
|
+
exit 0
|
89
|
+
end
|
90
|
+
else
|
91
|
+
self.output_log [error_stream, log_stream], Time.now.strftime("[%Y-%m-%d %H:%M:%S](INFO)#{log_label} subscribe from #{queue_name}(#{remain_count} messages, #{consumer_count} consumers)")
|
92
|
+
end
|
93
|
+
|
94
|
+
if !exchange_name.nil? && exchange_name != '' then
|
95
|
+
exchange = bunny.exchange(exchange_name)
|
96
|
+
queue.bind(exchange)
|
97
|
+
end
|
98
|
+
|
99
|
+
total_count = 0
|
100
|
+
count = 0
|
101
|
+
|
102
|
+
subscribe_flag = false
|
103
|
+
|
104
|
+
if remain_count == 0 then
|
105
|
+
self.output_log [error_stream, log_stream], Time.now.strftime("[%Y-%m-%d %H:%M:%S](INFO)#{log_label} no messages in #{queue_name}(#{remain_count} messages, #{consumer_count} consumers)")
|
106
|
+
else
|
107
|
+
# subscribe to queue
|
108
|
+
begin
|
109
|
+
queue.subscribe(:ack=>ack, \
|
110
|
+
:consumer_tag=>consumer_tag, \
|
111
|
+
:exclusive=>exclusive, \
|
112
|
+
:message_max=>message_max, \
|
113
|
+
:timeout=>timeout) do |msg|
|
114
|
+
if msg && msg[:payload] then
|
115
|
+
output_stream.puts msg[:payload]
|
116
|
+
count += 1
|
117
|
+
total_count += 1
|
118
|
+
if unit_size <= count then
|
119
|
+
subscribe_flag = true
|
120
|
+
self.output_log [error_stream, log_stream], Time.now.strftime("[%Y-%m-%d %H:%M:%S](INFO)#{log_label} subscribed #{count} messages from #{queue_name}")
|
121
|
+
count = 0
|
122
|
+
break if min_size && remain_count <= total_count + min_size
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
rescue Exception=>e
|
127
|
+
if raise_exception_flag then
|
128
|
+
bunny.stop if bunny
|
129
|
+
raise e if raise_exception_flag
|
130
|
+
else
|
131
|
+
self.output_log [error_stream, log_stream], Time.now.strftime("[%Y-%m-%d %H:%M:%S](EXCEPTION)#{log_label} #{e.message}(#{e.class.name}): #{e.backtrace.map{|s| " #{s}"}.join("\n")}")
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
subscribe_flag = true if 0 < count
|
137
|
+
|
138
|
+
self.output_subscribe_log [error_stream, log_stream], queue, count, log_label # if 0 < count || subscribe_flag
|
139
|
+
|
140
|
+
if log_stream then
|
141
|
+
log_stream.close
|
142
|
+
end
|
143
|
+
|
144
|
+
# Close client
|
145
|
+
bunny.stop
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
data/lib/bunnish.rb
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "bunny"
|
3
|
+
require "bunnish/count"
|
4
|
+
require "bunnish/help"
|
5
|
+
require "bunnish/publish"
|
6
|
+
require "bunnish/status"
|
7
|
+
require "bunnish/subscribe"
|
8
|
+
|
9
|
+
|
10
|
+
module Bunnish
|
11
|
+
def self.parse_opts(argv)
|
12
|
+
host = 'localhost'
|
13
|
+
port = 5672
|
14
|
+
user = 'guest'
|
15
|
+
password = 'guest'
|
16
|
+
durable = false
|
17
|
+
ack = true
|
18
|
+
raise_exception_flag = false
|
19
|
+
|
20
|
+
exchange_name = ''
|
21
|
+
delimiter = nil
|
22
|
+
|
23
|
+
consumer_tag = nil
|
24
|
+
exclusive = false
|
25
|
+
message_max = nil
|
26
|
+
timeout = 1
|
27
|
+
unit_size = nil
|
28
|
+
min_size = nil
|
29
|
+
current_all_flag = false
|
30
|
+
|
31
|
+
warn_size = nil
|
32
|
+
error_size = nil
|
33
|
+
|
34
|
+
log_label = nil
|
35
|
+
log_dir = nil
|
36
|
+
log_path = nil
|
37
|
+
|
38
|
+
next_argv = []
|
39
|
+
|
40
|
+
while 0 < argv.size do
|
41
|
+
val = argv.shift
|
42
|
+
case val
|
43
|
+
when '-h'
|
44
|
+
host = argv.shift
|
45
|
+
when '-p'
|
46
|
+
port = argv.shift.to_i
|
47
|
+
when '-u'
|
48
|
+
user = argv.shift
|
49
|
+
when '-P'
|
50
|
+
password = argv.shift.to_i
|
51
|
+
when '--ack'
|
52
|
+
ack = (argv.shift == 't')
|
53
|
+
when '--delimiter'
|
54
|
+
delimiter = argv.shift
|
55
|
+
when '--durable'
|
56
|
+
durable = (argv.shift == 't')
|
57
|
+
when '--exchange-name'
|
58
|
+
exchange_name = argv.shift
|
59
|
+
when '--unit-size'
|
60
|
+
unit_size = argv.shift.to_i
|
61
|
+
when '--log-label'
|
62
|
+
log_label = argv.shift
|
63
|
+
log_label = "[#{log_label}]"
|
64
|
+
when '--log-dir'
|
65
|
+
log_dir = argv.shift
|
66
|
+
when '--log-file'
|
67
|
+
log_path = argv.shift
|
68
|
+
when '--raise-exception'
|
69
|
+
raise_exception_flag = true
|
70
|
+
when '--consumer-tag'
|
71
|
+
consumer_tag = argv.shift.to_i
|
72
|
+
when '--timeout'
|
73
|
+
timeout = argv.shift.to_i
|
74
|
+
when '--exclusive'
|
75
|
+
exclusive = (argv.shift == 't')
|
76
|
+
when '--message-max'
|
77
|
+
message_max = argv.shift
|
78
|
+
when '--current-all'
|
79
|
+
current_all_flag = true
|
80
|
+
when '--min-size'
|
81
|
+
min_size = argv.shift.to_i
|
82
|
+
when '--empty-list-max'
|
83
|
+
empty_list_max = argv.shift.to_i
|
84
|
+
when '--warn'
|
85
|
+
warn_size = argv.shift.to_i
|
86
|
+
when '--error'
|
87
|
+
error_size = argv.shift.to_i
|
88
|
+
else
|
89
|
+
next_argv.push val
|
90
|
+
end
|
91
|
+
end
|
92
|
+
argv.push(*next_argv)
|
93
|
+
|
94
|
+
return {
|
95
|
+
:host=>host,
|
96
|
+
:port=>port,
|
97
|
+
:user=>user,
|
98
|
+
:password=>password,
|
99
|
+
:durable=>durable,
|
100
|
+
:ack=>ack,
|
101
|
+
:exchange_name=>exchange_name,
|
102
|
+
:unit_size=>unit_size,
|
103
|
+
:raise_exception_flag=>raise_exception_flag,
|
104
|
+
:delimiter=>delimiter,
|
105
|
+
:log_label=>log_label,
|
106
|
+
:log_dir=>log_dir,
|
107
|
+
:log_path=>log_path,
|
108
|
+
:consumer_tag=>consumer_tag,
|
109
|
+
:timeout=>timeout,
|
110
|
+
:exclusive=>exclusive,
|
111
|
+
:message_max=>message_max,
|
112
|
+
:current_all_flag=>current_all_flag,
|
113
|
+
:min_size=>min_size,
|
114
|
+
:empty_list_max=>empty_list_max,
|
115
|
+
:warn_size=>warn_size,
|
116
|
+
:error_size=>error_size
|
117
|
+
}
|
118
|
+
|
119
|
+
end
|
120
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'bunnish'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,183 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bunnish
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Kenji Hara
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-12-06 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
type: :runtime
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
hash: 3
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
version_requirements: *id001
|
32
|
+
name: json
|
33
|
+
prerelease: false
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
type: :runtime
|
36
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
hash: 3
|
42
|
+
segments:
|
43
|
+
- 0
|
44
|
+
version: "0"
|
45
|
+
version_requirements: *id002
|
46
|
+
name: bunny
|
47
|
+
prerelease: false
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
type: :development
|
50
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ~>
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
hash: 47
|
56
|
+
segments:
|
57
|
+
- 2
|
58
|
+
- 8
|
59
|
+
- 0
|
60
|
+
version: 2.8.0
|
61
|
+
version_requirements: *id003
|
62
|
+
name: rspec
|
63
|
+
prerelease: false
|
64
|
+
- !ruby/object:Gem::Dependency
|
65
|
+
type: :development
|
66
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ~>
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
hash: 31
|
72
|
+
segments:
|
73
|
+
- 3
|
74
|
+
- 12
|
75
|
+
version: "3.12"
|
76
|
+
version_requirements: *id004
|
77
|
+
name: rdoc
|
78
|
+
prerelease: false
|
79
|
+
- !ruby/object:Gem::Dependency
|
80
|
+
type: :development
|
81
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
hash: 3
|
87
|
+
segments:
|
88
|
+
- 0
|
89
|
+
version: "0"
|
90
|
+
version_requirements: *id005
|
91
|
+
name: bundler
|
92
|
+
prerelease: false
|
93
|
+
- !ruby/object:Gem::Dependency
|
94
|
+
type: :development
|
95
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ~>
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
hash: 63
|
101
|
+
segments:
|
102
|
+
- 1
|
103
|
+
- 8
|
104
|
+
- 4
|
105
|
+
version: 1.8.4
|
106
|
+
version_requirements: *id006
|
107
|
+
name: jeweler
|
108
|
+
prerelease: false
|
109
|
+
- !ruby/object:Gem::Dependency
|
110
|
+
type: :development
|
111
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
112
|
+
none: false
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
hash: 3
|
117
|
+
segments:
|
118
|
+
- 0
|
119
|
+
version: "0"
|
120
|
+
version_requirements: *id007
|
121
|
+
name: rcov
|
122
|
+
prerelease: false
|
123
|
+
description: Command for AMQP access to Message Queue.
|
124
|
+
email: haracane@gmail.com
|
125
|
+
executables:
|
126
|
+
- bunnish
|
127
|
+
extensions: []
|
128
|
+
|
129
|
+
extra_rdoc_files:
|
130
|
+
- LICENSE.txt
|
131
|
+
- README.rdoc
|
132
|
+
files:
|
133
|
+
- .document
|
134
|
+
- .rspec
|
135
|
+
- Gemfile
|
136
|
+
- LICENSE.txt
|
137
|
+
- README.rdoc
|
138
|
+
- Rakefile
|
139
|
+
- VERSION
|
140
|
+
- bin/bunnish
|
141
|
+
- lib/bunnish.rb
|
142
|
+
- lib/bunnish/count.rb
|
143
|
+
- lib/bunnish/help.rb
|
144
|
+
- lib/bunnish/publish.rb
|
145
|
+
- lib/bunnish/status.rb
|
146
|
+
- lib/bunnish/subscribe.rb
|
147
|
+
- spec/bunnish_spec.rb
|
148
|
+
- spec/spec_helper.rb
|
149
|
+
homepage: http://github.com/haracane/bunnish
|
150
|
+
licenses:
|
151
|
+
- MIT
|
152
|
+
post_install_message:
|
153
|
+
rdoc_options: []
|
154
|
+
|
155
|
+
require_paths:
|
156
|
+
- lib
|
157
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
158
|
+
none: false
|
159
|
+
requirements:
|
160
|
+
- - ">="
|
161
|
+
- !ruby/object:Gem::Version
|
162
|
+
hash: 3
|
163
|
+
segments:
|
164
|
+
- 0
|
165
|
+
version: "0"
|
166
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
167
|
+
none: false
|
168
|
+
requirements:
|
169
|
+
- - ">="
|
170
|
+
- !ruby/object:Gem::Version
|
171
|
+
hash: 3
|
172
|
+
segments:
|
173
|
+
- 0
|
174
|
+
version: "0"
|
175
|
+
requirements: []
|
176
|
+
|
177
|
+
rubyforge_project:
|
178
|
+
rubygems_version: 1.8.21
|
179
|
+
signing_key:
|
180
|
+
specification_version: 3
|
181
|
+
summary: Command for AMQP access to Message Queue.
|
182
|
+
test_files: []
|
183
|
+
|