sensu-transport 3.3.0 → 4.0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 61314ff2ef5b58121e4e081d4e7541a7371e1712
4
- data.tar.gz: 1aa88cc40ee08ad0f8ee5ca63cbed7d7beced2ec
3
+ metadata.gz: 1ce225a451ad7f61802de97848a4cc88153f3ebb
4
+ data.tar.gz: 2a206e0652fc6c6735a86aeb7be35b218ee14c0d
5
5
  SHA512:
6
- metadata.gz: 499ed08070d7d38fcf493c1b14caf5839b5c562600d7f337fc63d5595d715aac69e4ca6514d40ad40f03c9e6b7716c7eff3b7c04ed2e19698c089d3523803ed6
7
- data.tar.gz: c808abf6000bcb62b63dfa4a56c84b0fafebb770b7fca5f89f8747f0883b856be597433eb909ae29db8eb0768d7174157eb51e9168ee4f059f9e071535b2675e
6
+ metadata.gz: 24be969e850d8a38bdf898a01c61a4923e970a77cf38bc31b4ef0e2b771785fdc7fca9bbb6b848383f3dc9d467a568666b0b2a43384ba0979465919a656ce196
7
+ data.tar.gz: 52c1e08b2a6daf56195866c649e465951a3f925f0267c7e3c42101659ba8d4941c7e34f5c5c8a5312db23419269e8e42a73a46f3187085734698270f943884a9
@@ -71,10 +71,10 @@ module Sensu
71
71
  # @param options [Hash] the options to publish the message with.
72
72
  # @yield [info] passes publish info to an optional callback/block.
73
73
  # @yieldparam info [Hash] contains publish information, which
74
- # may contain an error object.
75
- def publish(type, pipe, message, options={}, &callback)
74
+ # may contain an error object (:error).
75
+ def publish(type, pipe, message, options={})
76
76
  info = {:error => nil}
77
- callback.call(info) if callback
77
+ yield(info) if block_given?
78
78
  end
79
79
 
80
80
  # Subscribe to a transport pipe and/or funnel.
@@ -89,30 +89,31 @@ module Sensu
89
89
  # the consumer callback/block.
90
90
  # @yieldparam info [Hash] contains message information.
91
91
  # @yieldparam message [String] message.
92
- def subscribe(type, pipe, funnel=nil, options={}, &callback)
92
+ def subscribe(type, pipe, funnel=nil, options={})
93
93
  info = {}
94
94
  message = ''
95
- callback.call(info, message)
95
+ yield(info, message)
96
96
  end
97
97
 
98
98
  # Unsubscribe from all transport pipes and/or funnels.
99
99
  #
100
100
  # @yield [info] passes info to an optional callback/block.
101
101
  # @yieldparam info [Hash] contains unsubscribe information.
102
- def unsubscribe(&callback)
102
+ def unsubscribe
103
103
  info = {}
104
- callback.call(info) if callback
104
+ yield(info) if block_given?
105
105
  end
106
106
 
107
107
  # Acknowledge the delivery of a message from the transport.
108
108
  #
109
109
  # @param info [Hash] message information, eg. contains its id.
110
110
  # @yield [info] passes acknowledgment info to an optional callback/block.
111
- def acknowledge(info, &callback)
112
- callback.call(info) if callback
111
+ def acknowledge(info)
112
+ yield(info) if block_given?
113
113
  end
114
114
 
115
- # Alias for acknowledge()
115
+ # Alias for acknowledge(). This should be superseded by a proper
116
+ # alias via alias_method in the transport class.
116
117
  def ack(*args, &callback)
117
118
  acknowledge(*args, &callback)
118
119
  end
@@ -121,9 +122,11 @@ module Sensu
121
122
  #
122
123
  # @param funnel [String] the transport funnel to get stats for.
123
124
  # @param options [Hash] the options to get funnel stats with.
124
- def stats(funnel, options={}, &callback)
125
+ # @yield [info] passes funnel stats a callback/block.
126
+ # @yieldparam info [Hash] contains funnel stats.
127
+ def stats(funnel, options={})
125
128
  info = {}
126
- callback.call(info)
129
+ yield(info) if block_given?
127
130
  end
128
131
 
129
132
  # Discover available transports (Subclasses)
@@ -132,6 +135,23 @@ module Sensu
132
135
  klass < self
133
136
  end
134
137
  end
138
+
139
+ private
140
+
141
+ # Catch transport errors and call the on_error callback,
142
+ # providing it with the error object as an argument. This method
143
+ # is intended to be applied where necessary, not to be confused
144
+ # with a catch-all. Not all transports will need this.
145
+ #
146
+ # @yield [] callback/block to execute within a rescue block to
147
+ # catch transport errors.
148
+ def catch_errors
149
+ begin
150
+ yield
151
+ rescue => error
152
+ @on_error.call(error)
153
+ end
154
+ end
135
155
  end
136
156
  end
137
157
  end
@@ -1,7 +1,7 @@
1
1
  module AMQP
2
2
  class Session
3
3
  def send_heartbeat
4
- if tcp_connection_established? && !reconnecting?
4
+ if tcp_connection_established? && !reconnecting? && !closed?
5
5
  send_frame(AMQ::Protocol::HeartbeatFrame)
6
6
  if !@handling_skipped_heartbeats && @last_server_heartbeat
7
7
  if @last_server_heartbeat < (Time.now - (self.heartbeat_interval * 2))
@@ -8,6 +8,9 @@ require File.join(File.dirname(__FILE__), "patches", "amqp")
8
8
  module Sensu
9
9
  module Transport
10
10
  class RabbitMQ < Base
11
+ # RabbitMQ connection setup.
12
+ #
13
+ # @param options [Hash, String]
11
14
  def connect(options={})
12
15
  reset
13
16
  set_connection_options(options)
@@ -15,6 +18,9 @@ module Sensu
15
18
  connect_with_eligible_options
16
19
  end
17
20
 
21
+ # Reconnect to RabbitMQ.
22
+ #
23
+ # @param force [Boolean] the reconnect.
18
24
  def reconnect(force=false)
19
25
  unless @reconnecting
20
26
  @reconnecting = true
@@ -24,70 +30,134 @@ module Sensu
24
30
  end
25
31
  end
26
32
 
33
+ # Indicates if connected to RabbitMQ.
34
+ #
35
+ # @return [TrueClass, FalseClass]
27
36
  def connected?
28
37
  @connection.connected?
29
38
  end
30
39
 
40
+ # Close the RabbitMQ connection.
31
41
  def close
32
42
  callback = Proc.new { @connection.close }
33
43
  connected? ? callback.call : EM.next_tick(callback)
34
44
  end
35
45
 
36
- def publish(exchange_type, exchange_name, message, options={}, &callback)
37
- begin
38
- @channel.method(exchange_type.to_sym).call(exchange_name, options).publish(message) do
46
+ # Publish a message to RabbitMQ.
47
+ #
48
+ # @param type [Symbol] the RabbitMQ exchange type, possible
49
+ # values are: :direct and :fanout.
50
+ # @param pipe [String] the RabbitMQ exchange name.
51
+ # @param message [String] the message to be published to
52
+ # RabbitMQ.
53
+ # @param options [Hash] the options to publish the message with.
54
+ # @yield [info] passes publish info to an optional
55
+ # callback/block.
56
+ # @yieldparam info [Hash] contains publish information.
57
+ def publish(type, pipe, message, options={})
58
+ catch_errors do
59
+ @channel.method(type.to_sym).call(pipe, options).publish(message) do
39
60
  info = {}
40
- callback.call(info) if callback
61
+ yield(info) if block_given?
41
62
  end
42
- rescue => error
43
- info = {:error => error}
44
- callback.call(info) if callback
45
63
  end
46
64
  end
47
65
 
48
- def subscribe(exchange_type, exchange_name, queue_name="", options={}, &callback)
49
- previously_declared = @queues.has_key?(queue_name)
50
- @queues[queue_name] ||= @channel.queue!(queue_name, :auto_delete => true)
51
- queue = @queues[queue_name]
52
- queue.bind(@channel.method(exchange_type.to_sym).call(exchange_name))
53
- unless previously_declared
54
- queue.subscribe(options, &callback)
66
+ # Subscribe to a RabbitMQ queue.
67
+ #
68
+ # @param type [Symbol] the RabbitMQ exchange type, possible
69
+ # values are: :direct and :fanout.
70
+ # @param pipe [String] the RabbitMQ exhange name.
71
+ # @param funnel [String] the RabbitMQ queue.
72
+ # @param options [Hash] the options to consume messages with.
73
+ # @yield [info, message] passes message info and content to the
74
+ # consumer callback/block.
75
+ # @yieldparam info [Hash] contains message information.
76
+ # @yieldparam message [String] message.
77
+ def subscribe(type, pipe, funnel="", options={}, &callback)
78
+ catch_errors do
79
+ previously_declared = @queues.has_key?(funnel)
80
+ @queues[funnel] ||= @channel.queue!(funnel, :auto_delete => true)
81
+ queue = @queues[funnel]
82
+ queue.bind(@channel.method(type.to_sym).call(pipe))
83
+ unless previously_declared
84
+ queue.subscribe(options, &callback)
85
+ end
55
86
  end
56
87
  end
57
88
 
58
- def unsubscribe(&callback)
59
- @queues.values.each do |queue|
60
- if connected?
61
- queue.unsubscribe
62
- else
63
- queue.before_recovery do
89
+ # Unsubscribe from all RabbitMQ queues.
90
+ #
91
+ # @yield [info] passes info to an optional callback/block.
92
+ # @yieldparam info [Hash] contains unsubscribe information.
93
+ def unsubscribe
94
+ catch_errors do
95
+ @queues.values.each do |queue|
96
+ if connected?
64
97
  queue.unsubscribe
98
+ else
99
+ queue.before_recovery do
100
+ queue.unsubscribe
101
+ end
65
102
  end
66
103
  end
104
+ @queues = {}
105
+ @channel.recover if connected?
67
106
  end
68
- @queues = {}
69
- @channel.recover if connected?
70
107
  super
71
108
  end
72
109
 
73
- def acknowledge(info, &callback)
74
- info.ack
75
- callback.call(info) if callback
110
+ # Acknowledge the delivery of a message from RabbitMQ.
111
+ #
112
+ # @param info [Hash] message info containing its delivery tag.
113
+ # @yield [info] passes acknowledgment info to an optional
114
+ # callback/block.
115
+ def acknowledge(info)
116
+ catch_errors do
117
+ info.ack
118
+ end
119
+ super
76
120
  end
77
121
 
78
- def stats(queue_name, options={}, &callback)
79
- options = options.merge(:auto_delete => true)
80
- @channel.queue(queue_name, options).status do |messages, consumers|
81
- info = {
82
- :messages => messages,
83
- :consumers => consumers
84
- }
85
- callback.call(info)
122
+ # A proper alias for acknowledge().
123
+ alias_method :ack, :acknowledge
124
+
125
+ # RabbitMQ queue stats, including message and consumer counts.
126
+ #
127
+ # @param funnel [String] the RabbitMQ queue to get stats for.
128
+ # @param options [Hash] the options to get queue stats with.
129
+ # @yield [info] passes queue stats to the callback/block.
130
+ # @yieldparam info [Hash] contains queue stats.
131
+ def stats(funnel, options={})
132
+ catch_errors do
133
+ options = options.merge(:auto_delete => true)
134
+ @channel.queue(funnel, options).status do |messages, consumers|
135
+ info = {
136
+ :messages => messages,
137
+ :consumers => consumers
138
+ }
139
+ yield(info)
140
+ end
86
141
  end
87
142
  end
88
143
 
89
144
  private
90
145
 
146
+ # Catch RabbitMQ errors and call the on_error callback,
147
+ # providing it with the error object as an argument. This method
148
+ # is intended to be applied where necessary, not to be confused
149
+ # with a catch-all.
150
+ #
151
+ # @yield [] callback/block to execute within a rescue block to
152
+ # catch RabbitMQ errors.
153
+ def catch_errors
154
+ begin
155
+ yield
156
+ rescue AMQP::Error => error
157
+ @on_error.call(error)
158
+ end
159
+ end
160
+
91
161
  def reset
92
162
  @queues = {}
93
163
  @connection_timeout.cancel if @connection_timeout
@@ -111,11 +181,8 @@ module Sensu
111
181
  @eligible_options.shift
112
182
  end
113
183
 
114
- def reconnect_callback
115
- Proc.new { reconnect }
116
- end
117
-
118
- def setup_connection(options={}, &callback)
184
+ def setup_connection(options={})
185
+ reconnect_callback = Proc.new { reconnect }
119
186
  @connection = AMQP.connect(options, {
120
187
  :on_tcp_connection_failure => reconnect_callback,
121
188
  :on_possible_authentication_failure => reconnect_callback
@@ -123,7 +190,7 @@ module Sensu
123
190
  @connection.logger = @logger
124
191
  @connection.on_open do
125
192
  @connection_timeout.cancel
126
- callback.call if callback
193
+ yield if block_given?
127
194
  end
128
195
  @connection.on_tcp_connection_loss(&reconnect_callback)
129
196
  @connection.on_skipped_heartbeats(&reconnect_callback)
@@ -112,7 +112,7 @@ module Sensu
112
112
  #
113
113
  # @yield [info] passes info to an optional callback/block.
114
114
  # @yieldparam info [Hash] empty hash.
115
- def unsubscribe(&callback)
115
+ def unsubscribe
116
116
  @connections.each do |name, connection|
117
117
  case name
118
118
  when "pubsub"
@@ -131,13 +131,15 @@ module Sensu
131
131
  #
132
132
  # @param funnel [String] the transport funnel to get stats for.
133
133
  # @param options [Hash] IGNORED by this transport.
134
- def stats(funnel, options={}, &callback)
134
+ # @yield [info] passes list stats to the callback/block.
135
+ # @yieldparam info [Hash] contains list stats.
136
+ def stats(funnel, options={})
135
137
  redis_connection("redis").llen(funnel) do |messages|
136
138
  info = {
137
139
  :messages => messages,
138
140
  :consumers => 0
139
141
  }
140
- callback.call(info)
142
+ yield(info)
141
143
  end
142
144
  end
143
145
 
@@ -215,11 +217,11 @@ module Sensu
215
217
  # @yield [info] passes publish info to an optional callback/block.
216
218
  # @yieldparam info [Hash] contains publish information.
217
219
  # @yieldparam subscribers [String] current subscriber count.
218
- def pubsub_publish(pipe, message, &callback)
220
+ def pubsub_publish(pipe, message)
219
221
  channel = redis_key("channel", pipe)
220
222
  redis_connection("redis").publish(channel, message) do |subscribers|
221
223
  info = {:subscribers => subscribers}
222
- callback.call(info) if callback
224
+ yield(info) if block_given?
223
225
  end
224
226
  end
225
227
 
@@ -240,7 +242,7 @@ module Sensu
240
242
  # the consumer/method callback/block.
241
243
  # @yieldparam info [Hash] contains the channel name.
242
244
  # @yieldparam message [String] message content.
243
- def pubsub_subscribe(pipe, &callback)
245
+ def pubsub_subscribe(pipe)
244
246
  channel = redis_key("channel", pipe)
245
247
  redis_connection("pubsub").subscribe(channel) do |type, channel, message|
246
248
  case type
@@ -250,7 +252,7 @@ module Sensu
250
252
  @logger.debug("unsubscribed from redis channel: #{channel}") if @logger
251
253
  when "message"
252
254
  info = {:channel => channel}
253
- callback.call(info, message)
255
+ yield(info, message)
254
256
  end
255
257
  end
256
258
  end
@@ -265,11 +267,11 @@ module Sensu
265
267
  # @yield [info] passes publish info to an optional callback/block.
266
268
  # @yieldparam info [Hash] contains publish information.
267
269
  # @yieldparam queued [String] current list size.
268
- def list_publish(pipe, message, &callback)
270
+ def list_publish(pipe, message)
269
271
  list = redis_key("list", pipe)
270
272
  redis_connection("redis").rpush(list, message) do |queued|
271
273
  info = {:queued => queued}
272
- callback.call(info) if callback
274
+ yield(info) if block_given?
273
275
  end
274
276
  end
275
277
 
@@ -287,7 +289,7 @@ module Sensu
287
289
  # @yieldparam message [String] message content.
288
290
  def list_blpop(list, &callback)
289
291
  redis_connection(list).blpop(list, 0) do |_, message|
290
- EM::next_tick {list_blpop(list, &callback)}
292
+ EM::next_tick { list_blpop(list, &callback) }
291
293
  callback.call({}, message)
292
294
  end
293
295
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "sensu-transport"
5
- spec.version = "3.3.0"
5
+ spec.version = "4.0.0"
6
6
  spec.authors = ["Sean Porter"]
7
7
  spec.email = ["portertech@gmail.com"]
8
8
  spec.summary = "The Sensu transport abstraction library"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sensu-transport
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.3.0
4
+ version: 4.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Porter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-16 00:00:00.000000000 Z
11
+ date: 2016-02-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: eventmachine
@@ -172,7 +172,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
172
172
  version: '0'
173
173
  requirements: []
174
174
  rubyforge_project:
175
- rubygems_version: 2.2.2
175
+ rubygems_version: 2.4.5.1
176
176
  signing_key:
177
177
  specification_version: 4
178
178
  summary: The Sensu transport abstraction library