twingly-amqp 4.2.1 → 4.3.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: 858c1c7b49f3bbd776a0aa0af9f070d1c40f31f9
4
- data.tar.gz: ee5ee54beff37137f74fe60d0d4e60dc0db4e893
3
+ metadata.gz: 4db0f7716677c33ee82f1fc7b0530aa99a9c704b
4
+ data.tar.gz: a392bce55f82c6f07c39a6df3788bc8c918af654
5
5
  SHA512:
6
- metadata.gz: ecc06c04e469767a9f9a521989f3ae8f85aec7b18da877f255bda9a12db90575e7d4b5f427624f6d5b7f3e7d94047a6e8ce30c007f14c82f0033341d00c2e12c
7
- data.tar.gz: dcf7197d39bdff607daaa44957bb817a4a4f983a697949723051376ac4345595356b0c2b48d25081ae05942948ed8b408df0ee9645e00c7354301ad87af15c18
6
+ metadata.gz: e8831829877ca9fa5ac54f2ab7cba84f91b33b3e7fe280c0c47b214dcf7365b15b80e37fc330fe5c3433de5e4ab5cf6808802ea272b79d71e54d9997fe139d21
7
+ data.tar.gz: 83cb819bd56f5a9737129f00bb406d977aa3386a10c0a620188b9f7521dcf7463e535db38b95aa0e06cd227d91264164ce55d9711484d219afcfce13a4804d9d
data/README.md CHANGED
@@ -59,6 +59,7 @@ subscription = Twingly::AMQP::Subscription.new(
59
59
  routing_key: "url.blog", # Optional, uses the default exchange if omitted
60
60
  consumer_threads: 4, # Optional
61
61
  prefetch: 20, # Optional
62
+ max_length: 10_000, # Optional
62
63
  )
63
64
 
64
65
  subscription.on_exception { |exception| puts "Oh noes! #{exception.message}" }
@@ -80,6 +81,19 @@ subscription.each_message do |message| # An instance of Twingly::AMQP::Message
80
81
  end
81
82
  ```
82
83
 
84
+ #### Non-blocking subscription
85
+
86
+ ```ruby
87
+ subscription.each_message(blocking: false) do |message|
88
+ puts "Received #{message.payload.inspect}"
89
+ message.ack
90
+ end
91
+
92
+ # Do something
93
+
94
+ subscription.cancel! # Stops the subscriber
95
+ ```
96
+
83
97
  ### Publish to a queue on the default exchange
84
98
 
85
99
  ```ruby
@@ -4,12 +4,15 @@ require "twingly/amqp/message"
4
4
  module Twingly
5
5
  module AMQP
6
6
  class Subscription
7
- def initialize(queue_name:, exchange_topic: nil, routing_key: nil, consumer_threads: 4, prefetch: 20, connection: nil)
7
+ def initialize(queue_name:, exchange_topic: nil, routing_key: nil,
8
+ consumer_threads: 4, prefetch: 20, connection: nil,
9
+ max_length: nil)
8
10
  @queue_name = queue_name
9
11
  @exchange_topic = exchange_topic
10
12
  @routing_key = routing_key
11
13
  @consumer_threads = consumer_threads
12
14
  @prefetch = prefetch
15
+ @max_length = max_length
13
16
 
14
17
  connection ||= Connection.instance
15
18
  @channel = create_channel(connection)
@@ -24,26 +27,14 @@ module Twingly
24
27
  @on_exception_callback = proc {}
25
28
  end
26
29
 
27
- def each_message(&block)
28
- setup_traps
30
+ def each_message(blocking: true, &block)
31
+ consumer = create_consumer(&block)
29
32
 
30
- consumer = @queue.subscribe(subscribe_options) do |delivery_info, metadata, payload|
31
- @before_handle_message_callback.call(payload)
32
-
33
- message = Message.new(
34
- delivery_info: delivery_info,
35
- metadata: metadata,
36
- payload: payload,
37
- channel: @channel,
38
- )
33
+ if blocking
34
+ sleep 0.01 until cancel?
39
35
 
40
- block.call(message)
36
+ consumer.cancel
41
37
  end
42
-
43
- # The consumer isn't blocking, so we wait here
44
- sleep 0.5 until cancel?
45
-
46
- consumer.cancel
47
38
  end
48
39
 
49
40
  def before_handle_message(&block)
@@ -54,6 +45,14 @@ module Twingly
54
45
  @on_exception_callback = block
55
46
  end
56
47
 
48
+ def message_count
49
+ @queue.status.fetch(:message_count)
50
+ end
51
+
52
+ def raw_queue
53
+ @queue
54
+ end
55
+
57
56
  def cancel?
58
57
  @cancel
59
58
  end
@@ -64,6 +63,21 @@ module Twingly
64
63
 
65
64
  private
66
65
 
66
+ def create_consumer(&block)
67
+ @queue.subscribe(subscribe_options) do |delivery_info, metadata, payload|
68
+ @before_handle_message_callback.call(payload)
69
+
70
+ message = Message.new(
71
+ delivery_info: delivery_info,
72
+ metadata: metadata,
73
+ payload: payload,
74
+ channel: @channel,
75
+ )
76
+
77
+ block.call(message)
78
+ end
79
+ end
80
+
67
81
  def create_channel(connection)
68
82
  channel = connection.create_channel(nil, @consumer_threads)
69
83
  channel.prefetch(@prefetch)
@@ -77,9 +91,16 @@ module Twingly
77
91
  def queue_options
78
92
  {
79
93
  durable: true,
94
+ arguments: queue_arguments,
80
95
  }
81
96
  end
82
97
 
98
+ def queue_arguments
99
+ {}.tap do |arguments|
100
+ arguments["x-max-length"] = @max_length if @max_length
101
+ end
102
+ end
103
+
83
104
  def subscribe_options
84
105
  {
85
106
  manual_ack: true,
@@ -1,5 +1,5 @@
1
1
  module Twingly
2
2
  module Amqp
3
- VERSION = "4.2.1".freeze
3
+ VERSION = "4.3.0".freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twingly-amqp
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.2.1
4
+ version: 4.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Twingly AB
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-17 00:00:00.000000000 Z
11
+ date: 2016-12-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bunny
@@ -104,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
104
  version: '0'
105
105
  requirements: []
106
106
  rubyforge_project:
107
- rubygems_version: 2.4.5.1
107
+ rubygems_version: 2.5.2
108
108
  signing_key:
109
109
  specification_version: 4
110
110
  summary: Ruby library for talking to RabbitMQ