bug_bunny 2.0.1 → 2.0.2

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
  SHA256:
3
- metadata.gz: 074005f6739a5a8486230e47edbce60b9719f5e15f35d9ab3ed95d91eaf28a47
4
- data.tar.gz: 39f1d8afb65be6ff2b86e0461c16c93e0ff6ae6cbe629668ba8e6601fe898e80
3
+ metadata.gz: 8fccb3590fd16133119a7e2cea4c05002964846ac5a35e0714925f2d87115a06
4
+ data.tar.gz: 839ab64bb406eebee8e59f99c010c640a033dab15cd0f531e66f0fbc1e201c82
5
5
  SHA512:
6
- metadata.gz: 88e99f4d71b1bb78599cf7a1750218cf5fde2e9ccb010c1aa671d1ba5e1ffe011a9bc70c39fe284b150d6fb497827e94766a82085a3758308990e57eb0f0b5da
7
- data.tar.gz: 66acaa871a7f5adabf4135c3b84216f71072be0f9bc7f76c4e42247dde064709596c78b88fed32c9bd24bc47a6ba737d54bc7c3396509f4dae61f06c69d026fd
6
+ metadata.gz: 4be8016e660aec70394814ffc85ecba71759aeb94df817abac412c201685ebee89c447fdd76b9904dfb5d2bc5f73e208ed0eed2d9bbc016a307c09978254c76e
7
+ data.tar.gz: 741552a099ae0aadb5717cb46fc4ff24c77c9f31f7dae677131dc158f784a75bca1a739bb6cf65eba42bff84fba853d25a2df54ca0d5884a23f679fa72c679b0
@@ -3,6 +3,7 @@ module BugBunny
3
3
  # Ayuda a atrapar cualquier error de la gema con un solo 'rescue BugBunny::Error'.
4
4
  class Error < ::StandardError; end
5
5
  class PublishError < Error; end
6
+ class Connection < Error; end
6
7
 
7
8
  module ResponseError
8
9
  class Base < Error; end
@@ -52,7 +52,7 @@ module BugBunny
52
52
  include ActiveModel::Attributes
53
53
 
54
54
  attribute :message
55
- attribute :pool
55
+ attribute :connection
56
56
  attribute :routing_key, :string
57
57
  attribute :persistent, :boolean, default: false
58
58
  attribute :content_type, :string, default: "application/json"
@@ -66,25 +66,19 @@ module BugBunny
66
66
  attribute :expiration, :integer, default: -> { 1.day.in_milliseconds } #ms
67
67
  attribute :exchange_name, :string
68
68
  attribute :exchange_type, :string, default: 'direct'
69
- attr_accessor :type
70
-
69
+ attribute :type, :string
71
70
  attribute :action, :string
72
- attribute :arguments, default: {}
73
71
 
74
72
  def publish!
75
- pool.with do |conn|
76
- app = Rabbit.new(connection: conn)
77
- app.build_exchange(name: exchange_name, type: exchange_type)
78
- app.publish!(message, publish_opts)
79
- end
73
+ app = Rabbit.new(connection: connection)
74
+ app.build_exchange(name: exchange_name, type: exchange_type)
75
+ app.publish!(message, publish_opts)
80
76
  end
81
77
 
82
78
  def publish_and_consume!
83
- pool.with do |conn|
84
- app = Rabbit.new(connection: conn)
85
- app.build_exchange(name: exchange_name, type: exchange_type)
86
- app.publish_and_consume!(message, publish_opts)
87
- end
79
+ app = Rabbit.new(connection: connection)
80
+ app.build_exchange(name: exchange_name, type: exchange_type)
81
+ app.publish_and_consume!(message, publish_opts)
88
82
  end
89
83
 
90
84
  def publish_opts
@@ -101,14 +95,13 @@ module BugBunny
101
95
  expiration: expiration }
102
96
  end
103
97
 
104
- def type
105
- return if action.blank?
106
-
107
- self.type = format(action, arguments)
108
- end
109
-
110
98
  def initialize(attrs = {})
111
99
  super(attrs)
100
+ if attrs[:action].present?
101
+ args = attrs[:arguments] || {}
102
+
103
+ self.type = format(attrs[:action], args)
104
+ end
112
105
  self.routing_key ||= self.class::ROUTING_KEY
113
106
  end
114
107
  end
@@ -303,6 +303,10 @@ module BugBunny
303
303
  )
304
304
 
305
305
  bunny.tap(&:start)
306
+ rescue Timeout::Error, Bunny::ConnectionError => e
307
+ # Timeout::Error (para el timeout de conexión TCP) se captura separadamente.
308
+ # Bunny::ConnectionError cubre TCPConnectionFailed, AuthenticationFailure, AccessRefused, etc.
309
+ raise BugBunny::Error::Connection, e.message
306
310
  end
307
311
  end
308
312
  end
@@ -21,16 +21,18 @@ module BugBunny
21
21
  end
22
22
 
23
23
  class ExchangeScope
24
- attr_reader :exchange_name, :klass
24
+ attr_reader :exchange_name, :routing_key, :klass
25
25
 
26
- def initialize(klass, exchange_name)
26
+ def initialize(klass, exchange_name, routing_key = nil)
27
27
  @klass = klass
28
28
  @exchange_name = exchange_name
29
+ @routing_key = routing_key
29
30
  end
30
31
 
31
32
  def method_missing(method_name, *args, **kwargs, &block)
32
33
  if @klass.respond_to?(method_name, true)
33
34
  kwargs[:exchange] = @exchange_name
35
+ kwargs[:routing_key] = @routing_key if @routing_key.present?
34
36
  @klass.execute(method_name.to_sym, *args, **kwargs, &block)
35
37
  else
36
38
  super
@@ -103,7 +105,7 @@ module BugBunny
103
105
 
104
106
  return self if persisted? && changes.empty?
105
107
 
106
- obj = self.class.publisher.send(action, exchange: current_exchange, message: changes_to_send)
108
+ obj = self.class.publisher.send(action, exchange: current_exchange, routing_key: current_routing_key, message: changes_to_send)
107
109
 
108
110
  assign_attributes(obj) # refresco el objeto
109
111
  self.persisted = true
@@ -119,7 +121,7 @@ module BugBunny
119
121
  return self unless persisted?
120
122
 
121
123
  # Llamada al PUBLISHER sin el argumento 'box'
122
- self.class.publisher.send(destroy_action.to_sym, exchange: current_exchange, id: id)
124
+ self.class.publisher.send(destroy_action.to_sym, exchange: current_exchange, routing_key: current_routing_key, id: id)
123
125
 
124
126
  self.persisted = false
125
127
  true
@@ -132,20 +134,28 @@ module BugBunny
132
134
  self.class.current_exchange
133
135
  end
134
136
 
135
- def self.for_exchange(exchange_name)
137
+ def current_routing_key
138
+ self.class.current_routing_key
139
+ end
140
+
141
+ def self.for_exchange(exchange_name, routing_key = nil)
136
142
  raise ArgumentError, 'Exchange name must be specified.' if exchange_name.blank?
137
143
 
138
- ExchangeScope.new(self, exchange_name)
144
+ ExchangeScope.new(self, exchange_name, routing_key)
139
145
  end
140
146
 
141
147
  def self.execute(name, *args, **kwargs, &block)
142
148
  original_exchange = Thread.current[:bugbunny_current_exchange]
143
149
  Thread.current[:bugbunny_current_exchange] = kwargs[:exchange]
150
+ original_routing_key = Thread.current[:bugbunny_current_routing_key]
151
+ Thread.current[:bugbunny_current_routing_key] = kwargs[:routing_key]
144
152
  begin
145
153
  kwargs.delete(:exchange)
154
+ kwargs.delete(:routing_key)
146
155
  send(name, *args, **kwargs, &block)
147
156
  ensure
148
157
  Thread.current[:bugbunny_current_exchange] = original_exchange
158
+ Thread.current[:bugbunny_current_routing_key] = original_routing_key
149
159
  end
150
160
  end
151
161
 
@@ -153,12 +163,16 @@ module BugBunny
153
163
  Thread.current[:bugbunny_current_exchange]
154
164
  end
155
165
 
166
+ def self.current_routing_key
167
+ Thread.current[:bugbunny_current_routing_key]
168
+ end
169
+
156
170
  def self.all
157
171
  where
158
172
  end
159
173
 
160
174
  def self.where(query = {})
161
- body = publisher.send(index_action.to_sym, exchange: current_exchange, message: query)
175
+ body = publisher.send(index_action.to_sym, exchange: current_exchange, routing_key: current_routing_key, message: query)
162
176
  instances = []
163
177
 
164
178
  body.each do |obj|
@@ -171,7 +185,7 @@ module BugBunny
171
185
  end
172
186
 
173
187
  def self.find(id)
174
- obj = publisher.send(show_action.to_sym, exchange: current_exchange, id: id)
188
+ obj = publisher.send(show_action.to_sym, exchange: current_exchange, routing_key: current_routing_key, id: id)
175
189
  return if obj.blank?
176
190
 
177
191
  instance = new
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BugBunny
4
- VERSION = "2.0.1"
4
+ VERSION = "2.0.2"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bug_bunny
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - gabix