qwrapper 0.0.6 → 0.0.7
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/lib/qwrapper.rb +8 -0
- data/lib/qwrapper/queues/base.rb +10 -4
- data/lib/qwrapper/queues/rabbitmq.rb +53 -36
- data/lib/qwrapper/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 428aa9d04e2124f91d54aca54c8bce80c0397b93
|
4
|
+
data.tar.gz: 0e868856d631ce54a0bcfd9f8d827d947c52e604
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3a6bf19733bcb80047613a4ef6925a017b3cea783f9eb03bcdfcf6f44a3b3c697ea06e6f5376c793653ad2ffbb87402e80cf393751d82450c6ea0a05f9216f82
|
7
|
+
data.tar.gz: 05f84c3512b945ebd91d02be3aec16ddd1f4d619205ba8d5412aa6234dc8907a4cb09b8a43208bfc9fedfa744bf2c7ec9f5e8c72e83a3f367c275c2a991aa09f
|
data/lib/qwrapper.rb
CHANGED
data/lib/qwrapper/queues/base.rb
CHANGED
@@ -7,21 +7,27 @@ module Qwrapper
|
|
7
7
|
include Loggable
|
8
8
|
|
9
9
|
attr_accessor :config
|
10
|
-
attr_reader :
|
10
|
+
attr_reader :requeue_errors, :requeue_lambda
|
11
11
|
|
12
12
|
def initialize(options)
|
13
13
|
@config = options
|
14
14
|
@logger = options[:logger] if options.has_key?(:logger)
|
15
15
|
end
|
16
16
|
|
17
|
-
def
|
18
|
-
@
|
19
|
-
requeueable_exceptions = config[:
|
17
|
+
def requeue_errors
|
18
|
+
@requeue_errors ||= begin
|
19
|
+
requeueable_exceptions = config[:requeue_errors] || []
|
20
20
|
requeueable_exceptions << Qwrapper::RequeueError
|
21
21
|
requeueable_exceptions.uniq
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
+
def requeue_lambda
|
26
|
+
@requeue_lambda ||= begin
|
27
|
+
config[:requeue_lambda]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
25
31
|
end
|
26
32
|
|
27
33
|
end
|
@@ -1,49 +1,89 @@
|
|
1
1
|
require 'bunny'
|
2
2
|
require 'qwrapper/queues/base'
|
3
3
|
|
4
|
+
|
4
5
|
module Qwrapper
|
5
6
|
|
6
7
|
module Queues
|
7
8
|
|
8
9
|
class RabbitMQ < Base
|
9
10
|
|
11
|
+
attr_reader :connection
|
12
|
+
|
10
13
|
def subscribe(queue_name, options={}, &block)
|
14
|
+
ch = nil
|
11
15
|
begin
|
12
|
-
logger.info "
|
13
|
-
|
16
|
+
logger.info "Subscribing to '#{queue_name}'"
|
17
|
+
ch = connection.create_channel
|
18
|
+
ch.prefetch(options[:prefetch] || 1)
|
19
|
+
queue = ch.queue(queue_name, options.merge(durable: true))
|
14
20
|
queue.subscribe(ack: true, block: true) do |delivery_info, metadata, payload|
|
15
|
-
# TODO find unique way to identify a message and list that as part
|
16
|
-
# of the wrap message
|
17
21
|
begin
|
18
22
|
if logger.respond_to?(:wrap)
|
19
23
|
logger_wrapped_block_execution(payload, &block)
|
20
24
|
else
|
21
25
|
block_execution(payload, &block)
|
22
26
|
end
|
23
|
-
rescue *
|
24
|
-
|
25
|
-
|
27
|
+
rescue *requeue_errors => ex
|
28
|
+
if requeue_lambda
|
29
|
+
requeue_lambda.call(queue_name, payload, ex)
|
30
|
+
else
|
31
|
+
logger.error "No requeue_lambda provided"
|
32
|
+
raise ex
|
33
|
+
end
|
26
34
|
end
|
27
35
|
queue.channel.ack(delivery_info.delivery_tag)
|
28
36
|
end
|
29
|
-
connection.close if connection
|
30
37
|
rescue Exception => ex
|
31
|
-
logger.error "TODO: What logic goes here?"
|
32
38
|
logger.error ex
|
39
|
+
ensure
|
40
|
+
ch.close if ch
|
33
41
|
end
|
34
42
|
end
|
35
43
|
|
36
44
|
def publish(queue_name, messages, options={})
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
45
|
+
ch = nil
|
46
|
+
begin
|
47
|
+
logger.info "Publishing to '#{queue_name}'"
|
48
|
+
messages = [messages] unless messages.is_a?(Array)
|
49
|
+
messages.flatten!
|
50
|
+
if messages.count > 0
|
51
|
+
ch = connection.create_channel
|
52
|
+
ch.prefetch(options[:prefetch] || 1)
|
53
|
+
queue = ch.queue(queue_name, options.merge(durable: true))
|
54
|
+
messages.each do |message|
|
55
|
+
queue.publish(message.to_s, :persistent => true)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
rescue Exception => ex
|
59
|
+
logger.error ex
|
60
|
+
ensure
|
61
|
+
ch.close if ch
|
41
62
|
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def connect!
|
66
|
+
connection.start if connection
|
67
|
+
end
|
68
|
+
|
69
|
+
def disconnect!
|
42
70
|
connection.close if connection
|
43
71
|
end
|
44
72
|
|
45
73
|
private
|
46
74
|
|
75
|
+
def connection
|
76
|
+
@conn ||= begin
|
77
|
+
c = Bunny.new({
|
78
|
+
host: config[:host] || "localhost",
|
79
|
+
port: config[:port] || 5672,
|
80
|
+
logger: dup_logger,
|
81
|
+
keepalive: config[:keepalive] || true
|
82
|
+
})
|
83
|
+
c.start
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
47
87
|
def logger_wrapped_block_execution(payload, &block)
|
48
88
|
logger.wrap("SubscribedMessage") do |nested_logger|
|
49
89
|
block.call(payload, nested_logger)
|
@@ -54,29 +94,6 @@ module Qwrapper
|
|
54
94
|
block.call(payload, logger)
|
55
95
|
end
|
56
96
|
|
57
|
-
def get_queue(queue_name, options={})
|
58
|
-
ch = connection.create_channel
|
59
|
-
ch.prefetch(config[:prefetch] || 1)
|
60
|
-
ch.queue(queue_name, options.merge(durable: true))
|
61
|
-
end
|
62
|
-
|
63
|
-
def connection
|
64
|
-
#@conn ||= begin
|
65
|
-
Bunny.new(connection_details).tap do |c|
|
66
|
-
c.start
|
67
|
-
end
|
68
|
-
#end
|
69
|
-
end
|
70
|
-
|
71
|
-
def connection_details
|
72
|
-
{
|
73
|
-
host: config[:host] || "localhost",
|
74
|
-
port: config[:port] || 5672,
|
75
|
-
logger: dup_logger,
|
76
|
-
keepalive: config[:keepalive] || true
|
77
|
-
}
|
78
|
-
end
|
79
|
-
|
80
97
|
def dup_logger
|
81
98
|
logger.respond_to?(:duplicate) ? logger.duplicate("bunny") : logger
|
82
99
|
end
|
data/lib/qwrapper/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: qwrapper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nirmit Patel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-11-
|
11
|
+
date: 2014-11-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|