bunny_burrow 1.0.1 → 1.1.0

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
  SHA1:
3
- metadata.gz: 4dfcb9dc41f611dddb4036a1498092df18673cea
4
- data.tar.gz: b4f87e37e1589109a31d6de589b24d7879c5ca54
3
+ metadata.gz: f9c676f62c43bcb59cd98e4df385c514d32753ff
4
+ data.tar.gz: e744b1a510e83e6ece4753af0b1fd7a4d01abef0
5
5
  SHA512:
6
- metadata.gz: 57630b4667e989aa11bea9596ea359c49e9fe1d137699f720df706046f6ceb6a3c9e22260bbd0e4dd26483fb2381cd3aa7369483c8e463298db2660410cb01e8
7
- data.tar.gz: 44c128dc362cd389cfe6f70e5828839f3db2a53a7ff4c916c9a7302fc6c11b9e52ae3c3a7d6bb1f6f70eabf8390341ab2fc8dc05b81db941c4d066aa4cf894a2
6
+ metadata.gz: 838c97dbe6236e5e55fbbd5a5a72bda3ff27d1ab20ce2e9d54d71ad147dbf3d744ec30b77e549df57f8712f23959d1f3a9d068d9916591993d027be8360f7c86
7
+ data.tar.gz: 8d2af0701684e95d516f8d1a1468e3f9d33255d298de1546133388b242ea22d7157c6003e8c5db0d6f076c764860fdb82ca7d982de37d82279d03328e1284747
@@ -0,0 +1,75 @@
1
+ require 'bunny'
2
+ require 'json'
3
+ require 'thread'
4
+
5
+ module BunnyBurrow
6
+ STATUS_OK = 'ok'
7
+ STATUS_CLIENT_ERROR = 'client_error'
8
+ STATUS_SERVER_ERROR = 'server_error'
9
+
10
+ class Base
11
+ attr_accessor :rabbitmq_url, :rabbitmq_exchange, :logger, :log_prefix
12
+ attr_writer :timeout, :log_request, :log_response
13
+
14
+ def initialize
15
+ yield self if block_given?
16
+ end
17
+
18
+ def timeout
19
+ @timeout ||= 60
20
+ end
21
+
22
+ def log_request?
23
+ @log_request ||= false
24
+ end
25
+
26
+ def log_response?
27
+ @log_response ||= false
28
+ end
29
+
30
+ def shutdown
31
+ log 'Shutting down'
32
+ channel.close
33
+ connection.close
34
+ end
35
+
36
+ private
37
+
38
+ def connection
39
+ unless @connection
40
+ @connection = Bunny.new(rabbitmq_url)
41
+ @connection.start
42
+ end
43
+
44
+ @connection
45
+ end
46
+
47
+ def channel
48
+ @channel ||= connection.create_channel
49
+ end
50
+
51
+ def default_exchange
52
+ @default_exchange ||= channel.default_exchange
53
+ end
54
+
55
+ def topic_exchange
56
+ @topic_exchange ||= channel.topic(rabbitmq_exchange, durable: true)
57
+ end
58
+
59
+ def lock
60
+ @lock ||= Mutex.new
61
+ end
62
+
63
+ def condition
64
+ @condition ||= ConditionVariable.new
65
+ end
66
+
67
+ def log(message, level: :info)
68
+ return unless logger
69
+ prefix = log_prefix || 'BunnyBurrow'
70
+ message = "#{prefix}: #{message}"
71
+ logger.send(level, message)
72
+ end
73
+ end
74
+ end
75
+
@@ -0,0 +1,46 @@
1
+ require_relative 'base'
2
+
3
+ module BunnyBurrow
4
+ class Client < Base
5
+ def publish(payload, routing_key)
6
+ result = nil
7
+
8
+ details = {
9
+ routing_key: routing_key,
10
+ reply_to: reply_to
11
+ }
12
+ details[:request] = payload if log_request?
13
+ log "Publishing #{details}"
14
+
15
+ options = {
16
+ routing_key: routing_key,
17
+ reply_to: reply_to.name,
18
+ persistence: false
19
+ }
20
+
21
+ topic_exchange.publish(payload.to_json, options)
22
+
23
+ Timeout.timeout(timeout) do
24
+ reply_to.subscribe do |_, _, payload|
25
+ details[:response] = payload if log_response?
26
+ log "Receiving #{details}"
27
+ result = payload
28
+ lock.synchronize { condition.signal }
29
+ end
30
+
31
+ lock.synchronize { condition.wait(lock) }
32
+ end
33
+
34
+ result
35
+ end
36
+
37
+ private
38
+
39
+ def reply_to
40
+ # when creating a queue, a blank name indicates we want the AMPQ broker
41
+ # to generate a unique name for us. Also note that this queue will be on
42
+ # the default exchange
43
+ @reply_to ||= channel.queue('', exclusive: true, auto_delete: true)
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,79 @@
1
+ require_relative 'base'
2
+
3
+ module BunnyBurrow
4
+ class Server < Base
5
+ def self.create_response
6
+ {
7
+ status: STATUS_OK,
8
+ error_message: nil,
9
+ data: {}
10
+ }
11
+ end
12
+
13
+ def subscribe(routing_key, &block)
14
+ queue = channel.queue('', exclusive: true, auto_delete: true)
15
+ queue.bind(topic_exchange, routing_key: routing_key)
16
+
17
+ details = {
18
+ routing_key: routing_key,
19
+ queue: queue.name,
20
+ exchange: topic_exchange.name
21
+ }
22
+
23
+ log "Subscribing #{details}"
24
+ queue.subscribe(manual_ack: true) do |delivery_info, properties, payload|
25
+ begin
26
+ details = {
27
+ delivery_info: delivery_info,
28
+ properties: properties
29
+ }
30
+
31
+ details[:request] = payload if log_request?
32
+ log "Receiving #{details}"
33
+
34
+ response = block.call(payload)
35
+
36
+ details[:response] = response if log_response?
37
+
38
+ log "Replying #{details}"
39
+ default_exchange.publish(response.to_json, :routing_key => properties.reply_to, persistence: false)
40
+
41
+ log "Acknowledging #{details}"
42
+ channel.ack delivery_info.delivery_tag
43
+ rescue => e
44
+ log e.message, level: :error
45
+ response = {
46
+ status: STATUS_SERVER_ERROR,
47
+ error_message: e.message
48
+ }
49
+ default_exchange.publish(response.to_json, :routing_key => properties.reply_to, persistence: false)
50
+ end
51
+ end
52
+ rescue => e
53
+ log e.message, level: :error
54
+ end
55
+
56
+ def wait
57
+ process_lock.synchronize { process_condition.wait(process_lock) }
58
+ end
59
+
60
+ def stop_waiting
61
+ process_lock.synchronize { process_condition.signal }
62
+ end
63
+
64
+ def shutdown
65
+ stop_waiting
66
+ super
67
+ end
68
+
69
+ private
70
+
71
+ def process_lock
72
+ @process_lock ||= Mutex.new
73
+ end
74
+
75
+ def process_condition
76
+ @process_condition ||= ConditionVariable.new
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,3 @@
1
+ module BunnyBurrow
2
+ VERSION = '1.1.0'
3
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bunny_burrow
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vericity
@@ -129,6 +129,10 @@ extensions: []
129
129
  extra_rdoc_files: []
130
130
  files:
131
131
  - lib/bunny_burrow.rb
132
+ - lib/bunny_burrow/base.rb
133
+ - lib/bunny_burrow/client.rb
134
+ - lib/bunny_burrow/server.rb
135
+ - lib/bunny_burrow/version.rb
132
136
  homepage: https://github.com/johann-koebbe/bunny_burrow
133
137
  licenses:
134
138
  - MIT