vx-consumer 0.0.2 → 0.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: 92feb250ce6331845ed2c8068a4a9980ba31366a
4
- data.tar.gz: fa5b5b57e16f73360bb70336e497d75ccbd4cc7c
3
+ metadata.gz: 98dc0b2f078519ea2ccf52a5467cdfbebce98652
4
+ data.tar.gz: 405b389c6672e9ce777c44759e2b7422aa4d1555
5
5
  SHA512:
6
- metadata.gz: fa37bf84b361bf10901ad059e21d5dc6c1c8e8d502d1a2d65c52c196a854bbaec8ad83618d28f5b20ff213415f4df0d8b0c1369c562085d532671fbc68b04587
7
- data.tar.gz: ed001b0fe9ccea0e2a8d84125baed135657f7e6ba61a02ce1ea61242e21b29d7143c2880e5fcf0d7dbc64eee7b30b7544c56e08eb3741d033b0f7bcad67fe447
6
+ metadata.gz: c880219d1bd3c939a1a0a217b36053447736ed054b7b05bb00f15c00ad23d9a88395260b407e54c7a44619b1ad7150ad0c4d4ec5231ea8517e95ba4749cb8a2c
7
+ data.tar.gz: ecff22e7ec25e566353fb6526dca1f967c6f3bae88d827d7efcb707abbfc2433db413166bb471c9dce42a9a0c6870b4a713e0bf3ae7fc6ffc19182eaea2076ae
@@ -8,8 +8,8 @@ module Vx
8
8
  properties: properties,
9
9
  multiple: multiple,
10
10
  }
11
- if channel.open?
12
- channel.ack delivery_info.delivery_tag, multiple
11
+ if _channel.open?
12
+ _channel.ack delivery_info.delivery_tag, multiple
13
13
  instrument("ack", instrumentation)
14
14
  true
15
15
  else
@@ -25,8 +25,8 @@ module Vx
25
25
  multiple: multiple,
26
26
  requeue: requeue,
27
27
  }
28
- if channel.open?
29
- channel.ack delivery_info.delivery_tag, multiple, requeue
28
+ if _channel.open?
29
+ _channel.ack delivery_info.delivery_tag, multiple, requeue
30
30
  instrument("nack", instrumentation)
31
31
  true
32
32
  else
@@ -7,20 +7,34 @@ module Vx
7
7
 
8
8
  include Instrument
9
9
 
10
- @@session_lock = Mutex.new
10
+ @@session_lock = Mutex.new
11
+
12
+ @@shutdown_lock = Mutex.new
13
+ @@shutdown = ConditionVariable.new
14
+ @@live = false
11
15
 
12
16
  attr_reader :conn
13
17
 
14
18
  def shutdown
15
- @shutdown = true
19
+ @@shutdown_lock.synchronize do
20
+ @@live = false
21
+ @@shutdown.broadcast
22
+ end
16
23
  end
17
24
 
18
- def shutdown?
19
- !!@shutdown
25
+ def live?
26
+ @@live
20
27
  end
21
28
 
22
29
  def resume
23
- @shutdown = false
30
+ @@live = true
31
+ end
32
+
33
+ def wait_shutdown(timeout = nil)
34
+ @@shutdown_lock.synchronize do
35
+ @@shutdown.wait(@@shutdown_lock, timeout)
36
+ not live?
37
+ end
24
38
  end
25
39
 
26
40
  def close
@@ -4,31 +4,44 @@ module Vx
4
4
 
5
5
  def subscribe
6
6
  ch, q = bind
7
- bunny_consumer = q.subscribe(block: false, ack: params.ack) do |delivery_info, properties, payload|
8
- payload = decode_payload properties, payload
9
7
 
10
- instrumentation = {
11
- consumer: params.consumer_name,
12
- payload: payload,
13
- properties: properties,
14
- }
8
+ subscriber = Subscriber.new(
9
+ ch,
10
+ q,
11
+ ch.generate_consumer_tag,
12
+ !params.ack
13
+ )
14
+ subscriber.vx_consumer_name = params.consumer_name
15
15
 
16
- with_middlewares :sub, instrumentation do
17
- instrument("start_processing", instrumentation)
18
- instrument("process", instrumentation) do
19
- run_instance delivery_info, properties, payload, ch
20
- end
21
- end
16
+ subscriber.on_delivery do |delivery_info, properties, payload|
17
+ handle_delivery ch, delivery_info, properties, payload
22
18
  end
23
19
 
24
- Subscriber.new(bunny_consumer)
20
+ q.subscribe_with(subscriber)
21
+ end
22
+
23
+ def handle_delivery(channel, delivery_info, properties, payload)
24
+ payload = decode_payload properties, payload
25
+
26
+ instrumentation = {
27
+ consumer: params.consumer_name,
28
+ payload: payload,
29
+ properties: properties,
30
+ }
31
+
32
+ with_middlewares :sub, instrumentation do
33
+ instrument("start_processing", instrumentation)
34
+ instrument("process", instrumentation) do
35
+ run_instance delivery_info, properties, payload, channel
36
+ end
37
+ end
25
38
  end
26
39
 
27
40
  def run_instance(delivery_info, properties, payload, channel)
28
41
  new.tap do |inst|
29
42
  inst.properties = properties
30
43
  inst.delivery_info = delivery_info
31
- inst.channel = channel
44
+ inst._channel = channel
32
45
  end.perform payload
33
46
  end
34
47
 
@@ -1,24 +1,49 @@
1
+ require 'thread'
2
+ require 'bunny/consumer'
3
+
1
4
  module Vx
2
5
  module Consumer
3
- Subscriber = Struct.new(:consumer) do
6
+ class Subscriber < Bunny::Consumer
7
+
8
+ include Instrument
9
+
10
+ attr_accessor :vx_consumer_name
11
+
12
+ def initialize(*args)
13
+ super(*args)
14
+ @lock = Mutex.new
15
+ end
16
+
17
+ def graceful_shutdown
18
+ instrument('graceful_shutdown_consumer', consumer: vx_consumer_name)
19
+ in_progress { cancel }
20
+ end
21
+
22
+ def in_progress
23
+ @lock.synchronize do
24
+ yield
25
+ end
26
+ end
27
+
28
+ def call(*args)
29
+ in_progress do
30
+ @on_delivery.call(*args) if @on_delivery
31
+ end
32
+ end
4
33
 
5
34
  def cancel
6
- consumer.cancel
7
- consumer.channel.close unless consumer.channel.closed?
35
+ instrument('cancel_consumer', consumer: vx_consumer_name)
36
+ super
37
+ channel.close unless channel.closed?
8
38
  end
9
39
 
10
40
  def join
11
- consumer.channel.work_pool.join
41
+ channel.work_pool.join
12
42
  end
13
43
 
14
- def wait
15
- loop do
16
- if Consumer.shutdown?
17
- cancel
18
- break
19
- end
20
- sleep Consumer.configuration.pool_timeout
21
- end
44
+ def wait_shutdown
45
+ Consumer.wait_shutdown
46
+ cancel
22
47
  end
23
48
 
24
49
  end
@@ -1,5 +1,5 @@
1
1
  module Vx
2
2
  module Consumer
3
- VERSION = "0.0.2"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
data/lib/vx/consumer.rb CHANGED
@@ -19,7 +19,7 @@ module Vx
19
19
 
20
20
  attr_accessor :properties
21
21
  attr_accessor :delivery_info
22
- attr_accessor :channel
22
+ attr_accessor :_channel
23
23
 
24
24
  def self.included(base)
25
25
  base.extend ClassMethods
@@ -95,8 +95,12 @@ module Vx
95
95
  session.shutdown
96
96
  end
97
97
 
98
- def shutdown?
99
- session.shutdown?
98
+ def live?
99
+ session.live?
100
+ end
101
+
102
+ def wait_shutdown(timeout = nil)
103
+ session.wait_shutdown(timeout)
100
104
  end
101
105
 
102
106
  def configure
@@ -93,9 +93,9 @@ describe Vx::Consumer do
93
93
  Bob.publish a: 1
94
94
 
95
95
  th = Thread.new {
96
- consumer.wait
96
+ consumer.wait_shutdown
97
97
  }
98
- sleep 1
98
+ sleep 0.2
99
99
  Vx::Consumer.shutdown
100
100
 
101
101
  Timeout.timeout(1) do
@@ -105,6 +105,21 @@ describe Vx::Consumer do
105
105
  expect(Bob._collected).to eq(["a" => 1])
106
106
  end
107
107
 
108
+ it "should work with graceful shutdown" do
109
+ consumer = Bob.subscribe
110
+ 10.times do |n|
111
+ Bob.publish a: n
112
+ end
113
+
114
+ sleep 0.2
115
+ Timeout.timeout(1) do
116
+ consumer.graceful_shutdown
117
+ end
118
+
119
+ expect(Bob._collected).to have_at_least(2).item
120
+ expect(Bob._collected).to have_at_most(8).item
121
+ end
122
+
108
123
  def handle_errors
109
124
  begin
110
125
  yield
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vx-consumer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Galinsky
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-04 00:00:00.000000000 Z
11
+ date: 2014-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bunny