sensu-transport-bunny 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZTdiYTI3YTJjYWUxNjlkN2QxZGVjMmMyYzU3NGVkYzMyNDRjZDliMQ==
5
+ data.tar.gz: !binary |-
6
+ YzRhNjEyYTZiZTAwNTI0OWJlMTAwZGJmODQ4MjRiMTViMGI2YzMyOA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MzU4ODRhYTM0NzlkOGQ0YWZmMmIwYzkxZGU5YjU1YjEwZDRhODUyN2IwOTg1
10
+ ZTNmYWY4OGVjZWU3M2FlODkyZDQzNjU2ODVlYmM5NTdlNGM2NWMxMjljNzVl
11
+ ZWE4MTgwZjc5MjZlOGE2OWY1NjNlZmYwMDAxY2I3ZWYyYjQ2ZmI=
12
+ data.tar.gz: !binary |-
13
+ OGVkM2YyMzA1ZDliZjUzOGM0NzFiMzU3NmY5M2EyYjExODVhOTM1YTIyMDg2
14
+ NDZmMWZmM2QzZjliYmQzZTdlMmVkMmNjN2I0MzIzNDdiMjFhNjAzMDMxNmI5
15
+ M2YxYTk0NzE3YzU0NzE5YTBhZmJmMTJhYWVmMDE2ZjM2MjgyYWY=
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ vendor
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.0
6
+ - jruby
7
+ services:
8
+ - rabbitmq
9
+ notifications:
10
+ email:
11
+ - "borisov.gleb@gmail.com"
12
+ script: "rspec . --tag ~ssl"
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sensu-spawn.gemspec
4
+ gemspec
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2014 Gleb M Borisov.
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.
@@ -0,0 +1,30 @@
1
+ # Sensu::Transport::Bunny
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ gem 'sensu-transport-bunny'
8
+
9
+ And then execute:
10
+
11
+ $ bundle
12
+
13
+ ## Usage
14
+
15
+ This library provides the AMQP transport using `bunny`.
16
+
17
+ ## Contributing
18
+
19
+ Please do not submit a pull request to add an additional transport to
20
+ this library.
21
+
22
+ 1. [Fork it](https://github.com/monsterzz/sensu-transport-bunny/fork)
23
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
24
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
25
+ 4. Push to the branch (`git push origin my-new-feature`)
26
+ 5. Create a new Pull Request
27
+
28
+ ## License
29
+
30
+ Sensu-Transport-Bunny is released under the [MIT license](https://raw.github.com/monsterzz/sensu-transport-bunny/master/LICENSE.txt).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,119 @@
1
+ gem "sensu-transport"
2
+ gem "bunny", "1.3.1"
3
+
4
+ require "bunny"
5
+ require "sensu/transport/base"
6
+
7
+ module Sensu
8
+ module Transport
9
+ class Bunny < Base
10
+ def initialize
11
+ super
12
+ @queues = {}
13
+ @subscriptions = {}
14
+ end
15
+
16
+ def connect(options={})
17
+ if options.has_key?(:ssl)
18
+ options[:tls] = true
19
+ options[:tls_cert] = options[:ssl][:cert_chain_file]
20
+ options[:tls_key] = options[:ssl][:private_key_file]
21
+ end
22
+ options[:logger] = @logger
23
+
24
+ @connection = ::Bunny.new(options)
25
+ @connection.start
26
+ setup_channel(options)
27
+ rescue ::Bunny::TCPConnectionFailed => e
28
+ error = Error.new("failed to connect to rabbitmq (#{e.message}")
29
+ @on_error.call(error)
30
+ rescue ::Bunny::PossibleAuthenticationFailureError => e
31
+ error = Error.new("failed to auth to rabbitmq (#{e.message}")
32
+ @on_error.call(error)
33
+ end
34
+
35
+ def reconnect
36
+ unless @connection.connecting?
37
+ @before_reconnect.call
38
+ @connection.close
39
+ @connection.start
40
+ setup_channel
41
+ @after_reconnect.call
42
+ end
43
+ end
44
+
45
+ def connected?
46
+ @connection.connected?
47
+ end
48
+
49
+ def close
50
+ callback = Proc.new { @connection.close }
51
+ connected? ? callback.call : EM.next_tick(callback)
52
+ end
53
+
54
+ def publish(exchange_type, exchange_name, message, options={}, &callback)
55
+ begin
56
+ @channel.method(exchange_type.to_sym).call(exchange_name, options).publish(message) do
57
+ info = {}
58
+ callback.call(info) if callback
59
+ end
60
+ rescue => error
61
+ info = {:error => error}
62
+ callback.call(info) if callback
63
+ end
64
+ end
65
+
66
+ def subscribe(exchange_type, exchange_name, queue_name="", options={}, &callback)
67
+ previously_declared = @queues.has_key?(queue_name)
68
+ @queues[queue_name] ||= @channel.queue(queue_name, :auto_delete => true)
69
+ queue = @queues[queue_name]
70
+ queue.bind(@channel.method(exchange_type.to_sym).call(exchange_name))
71
+ unless previously_declared
72
+ unless @subscriptions.has_key?(queue_name)
73
+ @subscriptions[queue_name] = []
74
+ end
75
+ @subscriptions[queue_name] << queue.subscribe(options) do |delivery_info, properties, payload|
76
+ callback.call(payload)
77
+ end
78
+ end
79
+ end
80
+
81
+ def unsubscribe(&callback)
82
+ @queues.keys.each do |queue_name|
83
+ @subscriptions[queue_name].each do |consumer|
84
+ consumer.cancel
85
+ end
86
+ end
87
+ @queues = {}
88
+ @subscriptions = {}
89
+ super
90
+ end
91
+
92
+ def acknowledge(info, &callback)
93
+ info.ack
94
+ callback.call(info) if callback
95
+ end
96
+
97
+ def stats(queue_name, options={}, &callback)
98
+ options = options.merge(:auto_delete => true)
99
+ status = @channel.queue(queue_name, options).status
100
+ info = {
101
+ :messages => status[:message_count],
102
+ :consumers => status[:consumer_count]
103
+ }
104
+ callback.call(info)
105
+ end
106
+
107
+ private
108
+
109
+ def setup_channel(options={})
110
+ @channel = @connection.create_channel
111
+ prefetch = 1
112
+ if options.is_a?(Hash)
113
+ prefetch = options.fetch(:prefetch, 1)
114
+ end
115
+ @channel.prefetch(prefetch)
116
+ end
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "sensu-transport-bunny"
5
+ spec.version = "1.0.0"
6
+ spec.authors = ["Gleb M Borisov"]
7
+ spec.email = ["borisov.gleb@gmail.com"]
8
+ spec.summary = "The Sensu RabbitMQ transport (bunny)"
9
+ spec.description = "The Sensu RabbitMQ transport (bunny)"
10
+ spec.homepage = "https://github.com/monsterzz/sensu-transport-bunny"
11
+ spec.license = "MIT"
12
+
13
+ spec.files = `git ls-files -z`.split("\x0")
14
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
15
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
16
+ spec.require_paths = ["lib"]
17
+
18
+ spec.add_dependency("sensu-em")
19
+ spec.add_dependency("sensu-transport")
20
+
21
+ spec.add_dependency("bunny", "1.3.1")
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ end
@@ -0,0 +1,127 @@
1
+ require File.join(File.dirname(__FILE__), "helpers")
2
+ require "sensu/transport/bunny"
3
+ require "logger"
4
+
5
+ describe "Sensu::Transport::Bunny" do
6
+ include Helpers
7
+
8
+ before do
9
+ @transport = Sensu::Transport::Bunny.new
10
+ @transport.logger = Logger.new(STDOUT)
11
+ @transport.on_error { |e| puts e }
12
+ end
13
+
14
+ it "provides a transport API" do
15
+ expect(@transport).to respond_to(:on_error, :before_reconnect, :after_reconnect,
16
+ :connect, :reconnect, :connected?, :close,
17
+ :publish, :subscribe, :unsubscribe,
18
+ :acknowledge, :ack, :stats)
19
+ end
20
+
21
+ it "can publish and subscribe" do
22
+ async_wrapper do
23
+ @transport.connect
24
+ callback = Proc.new do |message|
25
+ expect(message).to eq("msg")
26
+ timer(0.5) do
27
+ async_done
28
+ end
29
+ end
30
+ @transport.subscribe("direct", "foo", "baz", {}, &callback)
31
+ @transport.subscribe("direct", "bar", "baz", {}, &callback)
32
+ timer(1) do
33
+ @transport.publish("direct", "foo", "msg") do |info|
34
+ expect(info).to be_kind_of(Hash)
35
+ expect(info).to be_empty
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ it "can unsubscribe from queues and close the connection" do
42
+ async_wrapper do
43
+ @transport.connect
44
+ @transport.subscribe("direct", "bar") do |info, message|
45
+ true
46
+ end
47
+ timer(1) do
48
+ @transport.unsubscribe do
49
+ @transport.close
50
+ expect(@transport.connected?).to be(false)
51
+ async_done
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ it "can open and close the connection immediately" do
58
+ async_wrapper do
59
+ @transport.connect
60
+ @transport.close
61
+ expect(@transport.connected?).to be(false)
62
+ EM.next_tick do
63
+ expect(@transport.connected?).to be(false)
64
+ timer(1) do
65
+ expect(@transport.connected?).to be(false)
66
+ async_done
67
+ end
68
+ end
69
+ end
70
+ end
71
+
72
+ it "can acknowledge the delivery of a message" do
73
+ async_wrapper do
74
+ @transport.connect
75
+ @transport.subscribe("direct", "foo", "", :ack => true) do |info, message|
76
+ @transport.acknowledge(info) do
77
+ timer(0.5) do
78
+ async_done
79
+ end
80
+ end
81
+ end
82
+ timer(1) do
83
+ @transport.publish("direct", "foo", "msg") do |info|
84
+ expect(info).to be_kind_of(Hash)
85
+ expect(info).to be_empty
86
+ end
87
+ end
88
+ end
89
+ end
90
+
91
+ it "can get queue stats, message and consumer counts" do
92
+ async_wrapper do
93
+ @transport.connect
94
+ @transport.stats("bar") do |info|
95
+ expect(info).to be_kind_of(Hash)
96
+ expect(info[:messages]).to eq(0)
97
+ expect(info[:consumers]).to eq(0)
98
+ async_done
99
+ end
100
+ end
101
+ end
102
+
103
+ it "can fail to connect" do
104
+ async_wrapper do
105
+ @transport.connect(:port => 5555)
106
+ expect(@transport.connected?).to be(false)
107
+ async_done
108
+ end
109
+ end
110
+
111
+ it "can use TLS", :ssl => true do
112
+ ssl_dir = File.join(File.dirname(__FILE__), "assets", "ssl", "client")
113
+ async_wrapper do
114
+ @transport.connect(
115
+ :port => 5671,
116
+ :ssl => {
117
+ :cert_chain_file => File.join(ssl_dir, "cert.pem"),
118
+ :private_key_file => File.join(ssl_dir, "key.pem")
119
+ }
120
+ )
121
+ timer(2) do
122
+ expect(@transport.connected?).to be(true)
123
+ async_done
124
+ end
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,24 @@
1
+ require "rspec"
2
+ require "eventmachine"
3
+
4
+ module Helpers
5
+ def timer(delay, &callback)
6
+ periodic_timer = EM::PeriodicTimer.new(delay) do
7
+ callback.call
8
+ periodic_timer.cancel
9
+ end
10
+ end
11
+
12
+ def async_wrapper(&callback)
13
+ EM.run do
14
+ timer(10) do
15
+ raise "test timed out"
16
+ end
17
+ callback.call
18
+ end
19
+ end
20
+
21
+ def async_done
22
+ EM.stop_event_loop
23
+ end
24
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sensu-transport-bunny
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Gleb M Borisov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sensu-em
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sensu-transport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bunny
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 1.3.1
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 1.3.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.6'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.6'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: The Sensu RabbitMQ transport (bunny)
98
+ email:
99
+ - borisov.gleb@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - .travis.yml
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - lib/sensu/transport/bunny.rb
111
+ - sensu-transport-bunny.gemspec
112
+ - spec/bunny_spec.rb
113
+ - spec/helpers.rb
114
+ homepage: https://github.com/monsterzz/sensu-transport-bunny
115
+ licenses:
116
+ - MIT
117
+ metadata: {}
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ! '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 2.1.11
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: The Sensu RabbitMQ transport (bunny)
138
+ test_files:
139
+ - spec/bunny_spec.rb
140
+ - spec/helpers.rb
141
+ has_rdoc: