bug_bunny 0.1.0 → 0.2.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
  SHA256:
3
- metadata.gz: d3bbeb691836f8f7b516454ed095e36fe7e719dc99f8a435fb58e2ed995e2d80
4
- data.tar.gz: 8c1b3ed5758f0a19d0ee73233a4b92e328aaa114a18c851433f2bedbb2edeabe
3
+ metadata.gz: e955f527c2414c898f4bb9fa0f5584b59d112492edde985e41aad43dd9531795
4
+ data.tar.gz: 14bd2be642714c1a275a2e2a57d7a1c96caf386162af7403b888031a76eaab4d
5
5
  SHA512:
6
- metadata.gz: 0e7cbbe33d45b497526a1a35b6d841bfd141edda37baf91975d64d218617f2cb8d8a9ec858efd4d75097d0b564cb0f96ae7326d7cb9be51c9ec926e621882b6c
7
- data.tar.gz: c59537749ecd8c3dab9cff99c4f41ad5a62af569ddbbad13223ec3b3ad2ed9f2c3acaf96d6a736718afbfe5bd40e28dcfe147c61e5e5d3861fc71007db7580b4
6
+ metadata.gz: f7e81ea70ee7b358befc49b4165a37556fb28fe18fdab973f1a8e5faaa2d253138f0424651965a54ed251a8043e4de7b85810d06cc4e1929ef80db412877f709
7
+ data.tar.gz: c55269bbf9612e22d20a39784f4d8b03f05a501c5b3833d4b7f56bf0d377c300ce833a369d7c82663d09ab2b49b9cf3572c20a2d9c8f73d57de097414bef7727
data/README.md CHANGED
@@ -1,8 +1,40 @@
1
1
  # BugBunny
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
3
+ This gem simplify use of bunny gem. You can use 2 types of comunitaction, sync and async, Only is necessary define one `Adapter` to publish messages and `Consumer` to consume messages.
4
+
5
+ # Example
6
+
7
+ ```
8
+ # Adapter Code
9
+ class TestAdapter < ::BugBunny::Adapter
10
+ def self.publish_and_consume
11
+ service_adapter = TestAdapter.new
12
+ sync_queue = service_adapter.build_queue(:queue_test, durable: true, exclusive: false, auto_delete: false)
13
+
14
+ message = ::BugBunny::Message.new(service_action: :test_action, body: { msg: 'test message' })
15
+
16
+ service_adapter.publish_and_consume!(message, sync_queue, check_consumers_count: false)
17
+
18
+ service_adapter.close_connection! # ensure the adapter is close
19
+
20
+ service_adapter.make_response
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
+
4
37
 
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/bug_bunny`. To experiment with that code, run `bin/console` for an interactive prompt.
6
38
 
7
39
  ## Installation
8
40
 
@@ -0,0 +1,124 @@
1
+ module BugBunny
2
+ class Railtie < Rails::Railtie
3
+ rake_tasks do
4
+ namespace :bug_bunny do |args|
5
+ ARGV.shift
6
+
7
+ desc 'Bug Bunny consumer'
8
+ # environment is required to have access to Rails models
9
+ task consumer: :environment do
10
+ options = {}
11
+
12
+ o = OptionParser.new
13
+
14
+ o.banner = "Usage: rake bug_bunny:consumer [options]"
15
+ o.on("-a ADAPTER", "--adapter ADAPTER") { |adapter| options[:adapter] = adapter }
16
+ o.on("-s", "--session") { options[:session] = true }
17
+ o.on("-m MODE", "--mode MODE", "Sync or Async mode. values [:sync, :async]") { |mode| options[:mode] = mode }
18
+ o.on("-h", "--help", 'HELP ME!!! HELPE ME!!!') { puts o }
19
+
20
+ args = o.order!(ARGV) {}
21
+
22
+ o.parse!(args)
23
+
24
+ if defined?(Rails)
25
+ Rails.logger.info("Initializing #{options}")
26
+ else
27
+ puts "Initializing #{options}"
28
+ end
29
+
30
+ adapter_class = options[:adapter].constantize
31
+
32
+ mode = options[:mode].to_sym || :sync
33
+
34
+ queue_name = { sync: adapter_class::ROUTING_KEY_SYNC_REQUEST, async: adapter_class::ROUTING_KEY_ASYNC_REQUEST }[mode]
35
+
36
+ loop do
37
+ begin
38
+ adapter = adapter_class.new
39
+
40
+ queue = adapter.build_queue(queue_name, adapter_class::QUEUES_PROPS[queue_name])
41
+
42
+ if mode == :async
43
+ adapter.build_queue(
44
+ adapter_class::ROUTING_KEY_ASYNC_RESPONSE,
45
+ adapter_class::QUEUES_PROPS[adapter_class::ROUTING_KEY_ASYNC_RESPONSE]
46
+ )
47
+ end
48
+ rescue ::BugBunny::Exception::ComunicationRabbitError => e
49
+ if defined?(Rails)
50
+ Rails.logger.error(e)
51
+ else
52
+ puts e.message
53
+ end
54
+
55
+ (adapter ||= nil).try(:close_connection!) # ensure the adapter is close
56
+ sleep 5
57
+ retry
58
+ end
59
+
60
+ adapter.consume!(queue) do |message|
61
+ begin
62
+ if options[:session]
63
+ ::Session.request_id = message.correlation_id rescue nil
64
+ ::Session.tags_context ||= {}
65
+
66
+ ::Session.extra_context ||= {}
67
+ ::Session.extra_context[:message] = message.body
68
+ end
69
+
70
+ response = nil
71
+
72
+ Timeout.timeout(5.minutes) do
73
+ if defined?(Rails)
74
+ Rails.logger.debug("Msg received: action: #{message.service_action}, msg: #{message.body}")
75
+ else
76
+ puts "Msg received: action: #{message.service_action}, msg: #{message.body}"
77
+ end
78
+ response = "#{options[:adapter]}Controller".constantize.exec_action(message)
79
+ end
80
+ rescue Timeout::Error => e
81
+ puts("[OLT_VSOL_SERVICE][TIMEOUT][CONSUMER] #{e.to_s})")
82
+ @exception = e
83
+ rescue StandardError => e
84
+ adapter.check_pg_exception!(e)
85
+ if defined?(Rails)
86
+ Rails.logger.error(e)
87
+ else
88
+ puts e.message
89
+ end
90
+ @exception = e
91
+ end
92
+
93
+ if message.reply_to
94
+ Session.reply_to_queue = message.reply_to if options[:session]
95
+ begin
96
+ msg = message.build_message
97
+ if @exception
98
+ msg.body = (response&.key?(:body)) ? response[:body] : { id: message.body[:id] }
99
+ msg.exception = @exception
100
+ @exception = nil
101
+ else
102
+ msg.body = response[:body]
103
+ msg.status = response[:status] if response.key?(:status)
104
+ end
105
+
106
+ queue = adapter.build_queue(message.reply_to, initialize: false)
107
+ adapter.publish!(msg, queue)
108
+ rescue => e
109
+ if defined?(Rails)
110
+ Rails.logger.error(e)
111
+ else
112
+ puts e.message
113
+ end
114
+ end
115
+ end
116
+ Session.clean! if options[:session]
117
+ end
118
+ sleep 5
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BugBunny
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/bug_bunny.rb CHANGED
@@ -12,5 +12,10 @@ require_relative "bug_bunny/response"
12
12
  require_relative "bug_bunny/security"
13
13
  require_relative "bug_bunny/helpers"
14
14
 
15
+ if defined? ::Rails::Railtie
16
+ ## Rails only files
17
+ require 'bug_bunny/railtie'
18
+ end
19
+
15
20
  module BugBunny
16
21
  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: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - gabix
@@ -58,6 +58,7 @@ files:
58
58
  - lib/bug_bunny/message.rb
59
59
  - lib/bug_bunny/queue.rb
60
60
  - lib/bug_bunny/rabbit.rb
61
+ - lib/bug_bunny/railtie.rb
61
62
  - lib/bug_bunny/response.rb
62
63
  - lib/bug_bunny/security.rb
63
64
  - lib/bug_bunny/version.rb