bug_bunny 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d3bbeb691836f8f7b516454ed095e36fe7e719dc99f8a435fb58e2ed995e2d80
4
- data.tar.gz: 8c1b3ed5758f0a19d0ee73233a4b92e328aaa114a18c851433f2bedbb2edeabe
3
+ metadata.gz: 5f24c30a1e78bdb83a3f2a10eeafd1c4a4fa5f194b9edd773046ae1501c5230a
4
+ data.tar.gz: 9db7b2c82bca85a20c928c749c665ed5fc6ce9db4e0d53193bb106de2f61ccb1
5
5
  SHA512:
6
- metadata.gz: 0e7cbbe33d45b497526a1a35b6d841bfd141edda37baf91975d64d218617f2cb8d8a9ec858efd4d75097d0b564cb0f96ae7326d7cb9be51c9ec926e621882b6c
7
- data.tar.gz: c59537749ecd8c3dab9cff99c4f41ad5a62af569ddbbad13223ec3b3ad2ed9f2c3acaf96d6a736718afbfe5bd40e28dcfe147c61e5e5d3861fc71007db7580b4
6
+ metadata.gz: 8e2d31f049a5fcf696ca297e2e1b27071d3574c20093ec5b04ce8cbcbb2637817032bca33c31fd5c391bc842ac109e0f75adaf5a1ac9594e8069b9f6e0e94451
7
+ data.tar.gz: c471e33bd09d218aa9cbcf0b371211630aa6b1877285f8b6a275e69c4ffb47e4372e0fd4c91ef3caf2f34181e2a058c3ebba4fc378b39e4061d255f927b96673
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
 
@@ -89,10 +89,10 @@ module BugBunny
89
89
  # Session.correlation_id = metadata.correlation_id
90
90
  # Session.queue_name = queue.name
91
91
 
92
- unless defined?(ActiveRecord) && ActiveRecord::Base.connection_pool.with_connection(&:active?)
93
- logger.error('[PG] PG connection down')
94
- exit 7
95
- end
92
+ # unless defined?(ActiveRecord) && ActiveRecord::Base.connection_pool.with_connection(&:active?)
93
+ # logger.error('[PG] PG connection down')
94
+ # exit 7
95
+ # end
96
96
 
97
97
  begin
98
98
  message = ::BugBunny::Message.new(correlation_id: metadata.correlation_id, reply_to: metadata.reply_to, **payload)
@@ -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.1"
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,14 +1,14 @@
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.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - gabix
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-02-06 00:00:00.000000000 Z
11
+ date: 2024-07-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bunny
@@ -49,7 +49,6 @@ files:
49
49
  - Gemfile
50
50
  - README.md
51
51
  - Rakefile
52
- - bug_bunny.gemspec
53
52
  - lib/bug_bunny.rb
54
53
  - lib/bug_bunny/adapter.rb
55
54
  - lib/bug_bunny/controller.rb
@@ -58,6 +57,7 @@ files:
58
57
  - lib/bug_bunny/message.rb
59
58
  - lib/bug_bunny/queue.rb
60
59
  - lib/bug_bunny/rabbit.rb
60
+ - lib/bug_bunny/railtie.rb
61
61
  - lib/bug_bunny/response.rb
62
62
  - lib/bug_bunny/security.rb
63
63
  - lib/bug_bunny/version.rb
@@ -68,7 +68,7 @@ metadata:
68
68
  homepage_uri: https://github.com/gedera/bug_bunny
69
69
  source_code_uri: https://github.com/gedera/bug_bunny
70
70
  changelog_uri: https://github.com/gedera/bug_bunny/blob/main/CHANGELOG.md
71
- post_install_message:
71
+ post_install_message:
72
72
  rdoc_options: []
73
73
  require_paths:
74
74
  - lib
@@ -83,8 +83,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
83
  - !ruby/object:Gem::Version
84
84
  version: '0'
85
85
  requirements: []
86
- rubygems_version: 3.4.6
87
- signing_key:
86
+ rubygems_version: 3.5.11
87
+ signing_key:
88
88
  specification_version: 4
89
89
  summary: Gem for sync and async comunication via rabbit bunny.
90
90
  test_files: []
data/bug_bunny.gemspec DELETED
@@ -1,41 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "lib/bug_bunny/version"
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = "bug_bunny"
7
- spec.version = BugBunny::VERSION
8
- spec.authors = ["gabix"]
9
- spec.email = ["gab.edera@gmail.com"]
10
-
11
- spec.summary = "Gem for sync and async comunication via rabbit bunny."
12
- spec.description = "Gem for sync and async comunication via rabbit bunny."
13
- spec.homepage = "https://github.com/gedera/bug_bunny"
14
- spec.required_ruby_version = ">= 2.6.0"
15
-
16
- # spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
17
-
18
- spec.metadata["homepage_uri"] = spec.homepage
19
- spec.metadata["source_code_uri"] = "https://github.com/gedera/bug_bunny"
20
- spec.metadata["changelog_uri"] = "https://github.com/gedera/bug_bunny/blob/main/CHANGELOG.md"
21
-
22
- # Specify which files should be added to the gem when it is released.
23
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
- spec.files = Dir.chdir(__dir__) do
25
- `git ls-files -z`.split("\x0").reject do |f|
26
- (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|circleci)|appveyor)})
27
- end
28
- end
29
- spec.bindir = "exe"
30
- spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
31
- spec.require_paths = ["lib"]
32
-
33
- # Uncomment to register a new dependency of your gem
34
- # spec.add_dependency "example-gem", "~> 1.0"
35
-
36
- spec.add_dependency "bunny", "~> 2.20"
37
- spec.add_development_dependency "rubocop"
38
-
39
- # For more information and examples about making a new gem, check out our
40
- # guide at: https://bundler.io/guides/creating_gem.html
41
- end