qwrapper 0.0.6 → 0.0.7

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: 067dd75b70804a46400c8f52f517ac39348e4c88
4
- data.tar.gz: 4f2f4aebcfdd7f8476867d191ec9b8df26f25965
3
+ metadata.gz: 428aa9d04e2124f91d54aca54c8bce80c0397b93
4
+ data.tar.gz: 0e868856d631ce54a0bcfd9f8d827d947c52e604
5
5
  SHA512:
6
- metadata.gz: ffafd53ee24d682913b451c6f331d3768e175874894327046ac47c62c34ec6115a7de0b25a2b1e0f47d270f5a96a3fcda105a146e2026d81a8f4d41bd14b600c
7
- data.tar.gz: dcdd6edd2bc7a4dcad953591eb32c1ad449082a18e3ddb1afc01dd8f0c1a591fbb6aef7eb0571d0f7d7b4b73a26fc56ae74b868024e4c8b355141688b7523a3e
6
+ metadata.gz: 3a6bf19733bcb80047613a4ef6925a017b3cea783f9eb03bcdfcf6f44a3b3c697ea06e6f5376c793653ad2ffbb87402e80cf393751d82450c6ea0a05f9216f82
7
+ data.tar.gz: 05f84c3512b945ebd91d02be3aec16ddd1f4d619205ba8d5412aa6234dc8907a4cb09b8a43208bfc9fedfa744bf2c7ec9f5e8c72e83a3f367c275c2a991aa09f
@@ -26,6 +26,14 @@ module Qwrapper
26
26
  @config ||= {}
27
27
  end
28
28
 
29
+ def connect!
30
+ queue.connect! if queue
31
+ end
32
+
33
+ def disconnect!
34
+ queue.disconnect! if queue
35
+ end
36
+
29
37
  def queue
30
38
  @queue ||= begin
31
39
  base = Qwrapper::Queues::Base
@@ -7,21 +7,27 @@ module Qwrapper
7
7
  include Loggable
8
8
 
9
9
  attr_accessor :config
10
- attr_reader :requeue_exceptions
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 requeue_exceptions
18
- @requeue_exceptions ||= begin
19
- requeueable_exceptions = config[:requeue_exceptions] || []
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 "Subscribed to '#{queue_name}'"
13
- queue = get_queue(queue_name, options)
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 *requeue_exceptions => ex
24
- logger.error "Requeue logic"
25
- logger.error ex
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
- messages = [messages] unless messages.is_a?(Array)
38
- queue = get_queue(queue_name, options={})
39
- messages.each do |message|
40
- queue.publish(message.to_s, :persistent => true)
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
@@ -1,3 +1,3 @@
1
1
  module Qwrapper
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
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.6
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-17 00:00:00.000000000 Z
11
+ date: 2014-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler