bug_bunny 1.0.1 → 2.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 +4 -4
- data/README.md +18 -61
- data/Rakefile +5 -1
- data/lib/bug_bunny/config.rb +2 -2
- data/lib/bug_bunny/controller.rb +80 -7
- data/lib/bug_bunny/exception.rb +14 -66
- data/lib/bug_bunny/publisher.rb +115 -0
- data/lib/bug_bunny/rabbit.rb +283 -83
- data/lib/bug_bunny/resource.rb +226 -0
- data/lib/bug_bunny/version.rb +1 -1
- data/lib/bug_bunny.rb +7 -16
- metadata +8 -13
- data/Gemfile +0 -8
- data/lib/bug_bunny/adapter.rb +0 -347
- data/lib/bug_bunny/helpers.rb +0 -36
- data/lib/bug_bunny/message.rb +0 -161
- data/lib/bug_bunny/queue.rb +0 -23
- data/lib/bug_bunny/railtie.rb +0 -124
- data/lib/bug_bunny/response.rb +0 -15
- data/lib/bug_bunny/security.rb +0 -19
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 619fcbcb8024b1952fb21a4764cc9ae3734df38d7e359ec32f5b41fc3009eccd
|
|
4
|
+
data.tar.gz: 5c950d6cc2ef61351e937c6f2a7a32e6dc8dee06f4f93dfdae1bed350ad9702a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5ca317931674edfef2bc57eff4ab9df4faea01edfb833ce8978cd74bcd1bf9bb0c5823efe9540039b16ef5dfca8d3800d7fa6a76720e4949e13d3138baeb7f8f
|
|
7
|
+
data.tar.gz: cfe9ee5d4d01ae4a786eac02028b623939228da9d695bfbdd0139621600ff4e8ad825ccc5c74178c8d88deb38f47ea05eec2137e58ccad2eb8c7d202bfed6168
|
data/README.md
CHANGED
|
@@ -1,63 +1,20 @@
|
|
|
1
1
|
# BugBunny
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
# Controller Code
|
|
25
|
-
class TestController < ::BugBunny::Controller
|
|
26
|
-
##############################################################################
|
|
27
|
-
# SYNC SERVICE ACTIONS
|
|
28
|
-
##############################################################################
|
|
29
|
-
def self.test_action(message)
|
|
30
|
-
puts 'sleeping 5 seconds...'
|
|
31
|
-
sleep 5
|
|
32
|
-
{ status: :success, body: message.body }
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
## Installation
|
|
40
|
-
|
|
41
|
-
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
|
42
|
-
|
|
43
|
-
Install the gem and add to the application's Gemfile by executing:
|
|
44
|
-
|
|
45
|
-
$ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
|
|
46
|
-
|
|
47
|
-
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
48
|
-
|
|
49
|
-
$ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
|
|
50
|
-
|
|
51
|
-
## Usage
|
|
52
|
-
|
|
53
|
-
TODO: Write usage instructions here
|
|
54
|
-
|
|
55
|
-
## Development
|
|
56
|
-
|
|
57
|
-
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
58
|
-
|
|
59
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
60
|
-
|
|
61
|
-
## Contributing
|
|
62
|
-
|
|
63
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/bug_bunny.
|
|
3
|
+
## Publish
|
|
4
|
+
|
|
5
|
+
## Consumer
|
|
6
|
+
|
|
7
|
+
## Exceptions
|
|
8
|
+
- Error General:
|
|
9
|
+
- `BugBunny::Error` hereda de `::StandardError` (Captura cualquier error de la gema.)
|
|
10
|
+
- Error de Publicadores:
|
|
11
|
+
- `BugBunny::PublishError` hereda de `BugBunny::Error` (Para fallos de envío o conexión.)
|
|
12
|
+
- Error de Respuestas:
|
|
13
|
+
- `BugBunny::ResponseError::Base` hereda de `BugBunny::Error` (Captura todos los errores de respuesta).
|
|
14
|
+
- Errores Específicos de Respuesta:
|
|
15
|
+
- `BugBunny::ResponseError::BadRequest`
|
|
16
|
+
- `BugBunny::ResponseError::NotFound`
|
|
17
|
+
- `BugBunny::ResponseError::NotAcceptable`
|
|
18
|
+
- `BugBunny::ResponseError::RequestTimeout`
|
|
19
|
+
- `BugBunny::ResponseError::UnprocessableEntity`
|
|
20
|
+
- `BugBunny::ResponseError::InternalServerError`
|
data/Rakefile
CHANGED
data/lib/bug_bunny/config.rb
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
module BugBunny
|
|
2
2
|
class Config
|
|
3
3
|
# getter y setter para cada propiedad.
|
|
4
|
-
attr_accessor :
|
|
4
|
+
attr_accessor :host, :username, :password, :vhost, :logger, :automatically_recover, :network_recovery_interval, :connection_timeout, :read_timeout, :write_timeout, :heartbeat, :continuation_timeout
|
|
5
5
|
|
|
6
6
|
# Método para generar la URL de conexión
|
|
7
7
|
def url
|
|
8
|
-
"amqp://#{
|
|
8
|
+
"amqp://#{username}:#{password}@#{host}/#{vhost}"
|
|
9
9
|
end
|
|
10
10
|
end
|
|
11
11
|
end
|
data/lib/bug_bunny/controller.rb
CHANGED
|
@@ -1,16 +1,89 @@
|
|
|
1
1
|
module BugBunny
|
|
2
2
|
class Controller
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
include ActiveModel::Model
|
|
4
|
+
include ActiveModel::Attributes
|
|
5
|
+
|
|
6
|
+
attribute :headers
|
|
7
|
+
attribute :params
|
|
8
|
+
attribute :raw_string
|
|
9
|
+
|
|
10
|
+
attr_reader :rendered_response
|
|
11
|
+
|
|
12
|
+
def self.before_actions
|
|
13
|
+
# Nota el uso de '@' en lugar de '@@'
|
|
14
|
+
@before_actions ||= Hash.new { |hash, key| hash[key] = [] }
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.before_action(method_name, **options)
|
|
18
|
+
actions = options.delete(:only) || []
|
|
19
|
+
|
|
20
|
+
if actions.empty?
|
|
21
|
+
before_actions[:_all_actions] << method_name
|
|
22
|
+
else
|
|
23
|
+
Array(actions).each do |action|
|
|
24
|
+
before_actions[action.to_sym] << method_name
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def _run_before_actions
|
|
30
|
+
current_action = headers[:action].to_sym
|
|
31
|
+
|
|
32
|
+
callbacks = self.class.before_actions[:_all_actions] + self.class.before_actions[current_action]
|
|
33
|
+
|
|
34
|
+
callbacks.each do |method_name|
|
|
35
|
+
send(method_name) if respond_to?(method_name, true)
|
|
36
|
+
return false if @rendered_response
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
true
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def render(status:, json: nil)
|
|
43
|
+
@rendered_response = self.class.render(status: status, json: json)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def safe_parse_body(body)
|
|
47
|
+
self.params ||= {}
|
|
48
|
+
|
|
49
|
+
return if body.blank?
|
|
50
|
+
|
|
51
|
+
case headers[:content_type]
|
|
52
|
+
when 'application/json', 'application/x-www-form-urlencoded'
|
|
53
|
+
if body.instance_of?(Hash)
|
|
54
|
+
params.merge!(body.deep_symbolize_keys)
|
|
55
|
+
else # es string
|
|
56
|
+
params.merge!(ActiveSupport::JSON.decode(body).deep_symbolize_keys)
|
|
57
|
+
end
|
|
58
|
+
when 'text/plain'
|
|
59
|
+
self.raw_string = body
|
|
60
|
+
end
|
|
5
61
|
end
|
|
6
62
|
|
|
7
|
-
def self.
|
|
8
|
-
|
|
63
|
+
def self.status_code_number(code)
|
|
64
|
+
codes = Rack::Utils::SYMBOL_TO_STATUS_CODE
|
|
65
|
+
codes[:unprocessable_entity] = 422
|
|
66
|
+
code = codes[code.to_sym] if codes.key?(code.to_sym)
|
|
67
|
+
code
|
|
9
68
|
end
|
|
10
69
|
|
|
11
|
-
def self.
|
|
12
|
-
|
|
13
|
-
|
|
70
|
+
def self.render(status:, json: nil)
|
|
71
|
+
status_number = status_code_number(status)
|
|
72
|
+
{ status: status_number, body: json }
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def self.call(headers:, body: {})
|
|
76
|
+
controller = new(headers: headers)
|
|
77
|
+
controller.safe_parse_body(body)
|
|
78
|
+
controller.params[:id] = headers[:id] if headers.key?(:id)
|
|
79
|
+
controller.params.with_indifferent_access
|
|
80
|
+
return controller.rendered_response unless controller._run_before_actions
|
|
81
|
+
|
|
82
|
+
controller.send(controller.headers[:action])
|
|
83
|
+
rescue NoMethodError => e # action controller no exist
|
|
84
|
+
raise e
|
|
85
|
+
rescue StandardError => e
|
|
86
|
+
render status: :internal_server_error, json: e.message
|
|
14
87
|
end
|
|
15
88
|
end
|
|
16
89
|
end
|
data/lib/bug_bunny/exception.rb
CHANGED
|
@@ -1,69 +1,17 @@
|
|
|
1
1
|
module BugBunny
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
class
|
|
16
|
-
def to_s
|
|
17
|
-
:invalid_signature
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
class GatewayError < StandardError
|
|
22
|
-
def to_s
|
|
23
|
-
:gateway_error
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
class UltraCriticError < StandardError
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
class ComunicationRabbitError < StandardError
|
|
31
|
-
attr_accessor :backtrace
|
|
32
|
-
|
|
33
|
-
def initialize(msg, backtrace)
|
|
34
|
-
@backtrace = backtrace
|
|
35
|
-
super(msg)
|
|
36
|
-
end
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
class WithOutConsumer < StandardError
|
|
40
|
-
attr_accessor :backtrace
|
|
41
|
-
|
|
42
|
-
def initialize(msg, backtrace)
|
|
43
|
-
@backtrace = backtrace
|
|
44
|
-
super(msg)
|
|
45
|
-
end
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
class RetryWithoutError < StandardError
|
|
49
|
-
def to_s
|
|
50
|
-
"retry_sidekiq_without_error"
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
def backtrace
|
|
54
|
-
[]
|
|
55
|
-
end
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
ServiceClasses = [
|
|
59
|
-
Exception::NeedSignature,
|
|
60
|
-
Exception::InvalidSignature,
|
|
61
|
-
Exception::ServiceError,
|
|
62
|
-
Exception::GatewayError,
|
|
63
|
-
Exception::RetryWithoutError
|
|
64
|
-
]
|
|
65
|
-
|
|
66
|
-
# Exceptions from ActiveRecord::StatementInvalid
|
|
67
|
-
PG_EXCEPTIONS_TO_EXIT = %w[PG::ConnectionBad PG::UnableToSend].freeze
|
|
2
|
+
# Clase base para TODOS los errores de la gema BugBunny.
|
|
3
|
+
# Ayuda a atrapar cualquier error de la gema con un solo 'rescue BugBunny::Error'.
|
|
4
|
+
class Error < ::StandardError; end
|
|
5
|
+
class PublishError < Error; end
|
|
6
|
+
|
|
7
|
+
module ResponseError
|
|
8
|
+
class Base < Error; end
|
|
9
|
+
|
|
10
|
+
class BadRequest < Base; end # HTTP 400
|
|
11
|
+
class NotFound < Base; end # HTTP 404
|
|
12
|
+
class NotAcceptable < Base; end # HTTP 406
|
|
13
|
+
class RequestTimeout < Base; end # HTTP 408
|
|
14
|
+
class UnprocessableEntity < Base; end # HTTP 422
|
|
15
|
+
class InternalServerError < Base; end # HTTP 500
|
|
68
16
|
end
|
|
69
17
|
end
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# content_type:
|
|
2
|
+
# Propósito: Indica el formato de codificación del cuerpo del mensaje (ej. application/json, text/plain, application/xml).
|
|
3
|
+
# Uso Recomendado: dice a tu código qué lógica de deserialización aplicar. Si es application/json, usas JSON.parse.
|
|
4
|
+
|
|
5
|
+
# content_encoding:
|
|
6
|
+
# Propósito: Indica cómo se comprimió o codificó el cuerpo del mensaje (ej. gzip, utf-8).
|
|
7
|
+
# Uso Recomendado: Si envías cuerpos grandes, puedes comprimirlos (ej. con Gzip) para ahorrar ancho de banda y usar este campo para que el consumidor sepa cómo descomprimirlos antes de usar el content_type.
|
|
8
|
+
|
|
9
|
+
# correlation_id:
|
|
10
|
+
# Propósito: Un identificador único que se usa para correlacionar una respuesta con una petición previa.
|
|
11
|
+
# Uso Recomendado: Es indispensable en el patrón Remote Procedure Call (RPC). Si un productor envía una petición, copia este ID a la respuesta. Cuando el productor recibe la respuesta, usa este ID para saber a qué petición original corresponde.
|
|
12
|
+
|
|
13
|
+
# reply_to:
|
|
14
|
+
# Propósito: Especifica el nombre de la cola a la que el consumidor debe enviar la respuesta.
|
|
15
|
+
# Uso Recomendado: También clave en RPC. El productor especifica aquí su cola de respuesta temporal o exclusiva. El consumidor toma el mensaje, procesa, y publica el resultado en la cola indicada en reply_to.
|
|
16
|
+
|
|
17
|
+
# message_id:
|
|
18
|
+
# Propósito: Un identificador único para el mensaje en sí.
|
|
19
|
+
# Uso Recomendado: Ayuda a prevenir el procesamiento duplicado si un sistema de consumo cae y se recupera. El consumidor puede almacenar los message_id ya procesados.
|
|
20
|
+
|
|
21
|
+
# timestamp:
|
|
22
|
+
# Propósito: Indica la hora y fecha en que el mensaje fue publicado por el productor.
|
|
23
|
+
# Uso Recomendado: Útil para auditoría, diagnóstico y seguimiento de la latencia del sistema.
|
|
24
|
+
|
|
25
|
+
# priority:
|
|
26
|
+
# Propósito: Un valor entero que indica la prioridad relativa del mensaje (de 0 a 9, siendo 9 la más alta).
|
|
27
|
+
# Uso Recomendado: Solo funciona si la cola receptora está configurada como una Cola de Prioridades. Si lo está, RabbitMQ dará preferencia a los mensajes con mayor prioridad.
|
|
28
|
+
|
|
29
|
+
# expiration:
|
|
30
|
+
# Propósito: Especifica el tiempo de vida (TTL - Time To Live) del mensaje en la cola, en milisegundos.
|
|
31
|
+
# Uso Recomendado: Si el mensaje caduca antes de ser consumido, RabbitMQ lo descartará o lo moverá a la Dead Letter Queue (DLQ). Es vital para mensajes sensibles al tiempo (ej. tokens o alertas).
|
|
32
|
+
|
|
33
|
+
# user_id y app_id:
|
|
34
|
+
# Propósito: Identificadores que especifican qué usuario y qué aplicación generaron el mensaje.
|
|
35
|
+
# Uso Recomendado: Auditoría y seguridad. El broker (RabbitMQ) puede verificar que el user_id coincida con el usuario de la conexión AMQP utilizada para publicar.
|
|
36
|
+
|
|
37
|
+
# type:
|
|
38
|
+
# Propósito: Un identificador de aplicación para describir el "tipo" o "clase" de la carga útil del mensaje.
|
|
39
|
+
# Uso Recomendado: Usado a menudo para el enrutamiento interno dentro de una aplicación consumidora, similar al header Action que usas. Por ejemplo, en lugar de usar headers[:action], podrías usar properties[:type].
|
|
40
|
+
|
|
41
|
+
# cluster_id:
|
|
42
|
+
# Propósito: Obsoleto en AMQP 0-9-1 y no debe ser utilizado.
|
|
43
|
+
|
|
44
|
+
# persistent:
|
|
45
|
+
# Un valor booleano (true o false). Cuando es true, le dice a RabbitMQ que el mensaje debe persistir en el disco. Si el servidor de RabbitMQ se reinicia, el mensaje no se perderá.
|
|
46
|
+
|
|
47
|
+
# expiration:
|
|
48
|
+
# El tiempo de vida del mensaje en milisegundos. Después de este tiempo, RabbitMQ lo descartará automáticamente si no ha sido consumido.
|
|
49
|
+
module BugBunny
|
|
50
|
+
class Publisher
|
|
51
|
+
include ActiveModel::Model
|
|
52
|
+
include ActiveModel::Attributes
|
|
53
|
+
|
|
54
|
+
attribute :message
|
|
55
|
+
attribute :pool
|
|
56
|
+
attribute :routing_key, :string
|
|
57
|
+
attribute :persistent, :boolean, default: false
|
|
58
|
+
attribute :content_type, :string, default: "application/json"
|
|
59
|
+
attribute :content_encoding, :string, default: "utf-8"
|
|
60
|
+
attribute :correlation_id, :string
|
|
61
|
+
attribute :reply_to, :string
|
|
62
|
+
attribute :app_id, :string
|
|
63
|
+
attribute :headers, default: {}
|
|
64
|
+
attribute :message_id, :string, default: -> { SecureRandom.uuid }
|
|
65
|
+
attribute :timestamp, :datetime, default: -> { Time.zone.now.utc.to_i }
|
|
66
|
+
attribute :expiration, :integer, default: -> { 1.day.in_milliseconds } #ms
|
|
67
|
+
attribute :exchange_name, :string
|
|
68
|
+
attribute :exchange_type, :string, default: 'direct'
|
|
69
|
+
attr_accessor :type
|
|
70
|
+
|
|
71
|
+
attribute :action, :string
|
|
72
|
+
attribute :arguments, default: {}
|
|
73
|
+
|
|
74
|
+
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
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
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
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def publish_opts
|
|
91
|
+
{ routing_key: routing_key,
|
|
92
|
+
type: type,
|
|
93
|
+
persistent: persistent,
|
|
94
|
+
content_type: content_type,
|
|
95
|
+
content_encoding: content_encoding,
|
|
96
|
+
correlation_id: correlation_id,
|
|
97
|
+
reply_to: reply_to,
|
|
98
|
+
app_id: app_id,
|
|
99
|
+
headers: headers,
|
|
100
|
+
timestamp: timestamp,
|
|
101
|
+
expiration: expiration }
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def type
|
|
105
|
+
return if action.blank?
|
|
106
|
+
|
|
107
|
+
self.type = format(action, arguments)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def initialize(attrs = {})
|
|
111
|
+
super(attrs)
|
|
112
|
+
self.routing_key ||= self.class::ROUTING_KEY
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|