stapfen 1.2.1 → 1.3.0

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.
data/README.md CHANGED
@@ -23,7 +23,8 @@ Consider the following `myworker.rb` file:
23
23
  ]}
24
24
  end
25
25
 
26
- consume 'thequeue' do |message|
26
+ consume 'thequeue', :dead_letter_queue => '/queue/dlq',
27
+ :max_redeliveries => 0 do |message|
27
28
  data = expensive_computation(message.body)
28
29
  persist(data)
29
30
  end
@@ -40,6 +41,12 @@ It is also important to note that the `consume` block will be invoked inside an
40
41
  **instance** of `MyWorker` and will execute inside its own `Thread`, so take
41
42
  care when accessing other shared resources.
42
43
 
44
+ The consume block accepts the usual
45
+ [Stomp::Client](https://github.com/stompgem/stomp) subscription headers, as well
46
+ as :dead_letter_queue and :max\_redeliveries. If either of the latter two is
47
+ present, the consumer will unreceive any messages for which the block returns
48
+ false; after :max\_redeliveries, it will send the message to :dead_letter_queue.
49
+ `consume` blocks without these headers will fail silently rather than unreceive.
43
50
 
44
51
  ## Installation
45
52
 
@@ -1,3 +1,3 @@
1
1
  module Stapfen
2
- VERSION = '1.2.1'
2
+ VERSION = '1.3.0'
3
3
  end
@@ -85,6 +85,10 @@ module Stapfen
85
85
  debug("Running with #{@client} inside of Thread:#{Thread.current.object_id}")
86
86
 
87
87
  self.class.consumers.each do |name, headers, block|
88
+ unreceive_headers = {}
89
+ [:max_redeliveries, :dead_letter_queue].each do |sym|
90
+ unreceive_headers[sym] = headers.delete(sym) if headers.has_key? sym
91
+ end
88
92
 
89
93
  # We're taking each block and turning it into a method so that we can
90
94
  # use the instance scope instead of the blocks originally bound scope
@@ -93,7 +97,11 @@ module Stapfen
93
97
  self.class.send(:define_method, method_name, &block)
94
98
 
95
99
  client.subscribe(name, headers) do |message|
96
- self.send(method_name, message)
100
+ success = self.send(method_name, message)
101
+
102
+ if !success && !unreceive_headers.empty?
103
+ client.unreceive(message, unreceive_headers)
104
+ end
97
105
  end
98
106
  end
99
107
 
data/spec/worker_spec.rb CHANGED
@@ -43,7 +43,7 @@ describe Stapfen::Worker do
43
43
  let(:name) { 'jms.queue.lol' }
44
44
 
45
45
  it 'should add an entry for the queue name' do
46
- worker.consume(name) do
46
+ worker.consume(name) do |msg|
47
47
  nil
48
48
  end
49
49
 
@@ -52,6 +52,68 @@ describe Stapfen::Worker do
52
52
  entry.first.should eq(name)
53
53
  end
54
54
  end
55
+
56
+ context 'unreceive behavior' do
57
+ let(:client) { mock('Stomp::Client', :open? => false) }
58
+ let(:name) { '/queue/some_queue' }
59
+ before :each do
60
+ Stomp::Client.stub(:new).and_return(client)
61
+
62
+ # Get a subscription? Call the message handler block.
63
+ client.stub(:subscribe) do |name, headers, &block|
64
+ block.call('msg')
65
+ end
66
+ end
67
+
68
+ context 'with just a queue name' do
69
+ context 'on a failed message' do
70
+ it 'should not unreceive' do
71
+ client.should_receive(:unreceive).never
72
+
73
+ worker.consume(name) {|msg| false }
74
+ worker.new.run
75
+ end
76
+ end
77
+ context 'on a successful message' do
78
+ it 'should not unreceive' do
79
+ client.should_receive(:unreceive).never
80
+
81
+ worker.consume(name) {|msg| true }
82
+ worker.new.run
83
+ end
84
+ end
85
+ end
86
+
87
+ context 'with a queue name and headers for a dead_letter_queue and max_redeliveries' do
88
+ let(:unrec_headers) do
89
+ { :dead_letter_queue => '/queue/foo',
90
+ :max_redeliveries => 3 }
91
+ end
92
+ let(:raw_headers) { unrec_headers.merge(:other_header => 'foo!') }
93
+ context 'on a failed message' do
94
+ it 'should unreceive' do
95
+ client.should_receive(:unreceive).once
96
+
97
+ worker.consume(name, raw_headers) {|msg| false }
98
+ worker.new.run
99
+ end
100
+ it 'should pass :unreceive_headers through to the unreceive call' do
101
+ client.should_receive(:unreceive).with('msg', unrec_headers).once
102
+
103
+ worker.consume(name, raw_headers) {|msg| false }
104
+ worker.new.run
105
+ end
106
+ end
107
+ context 'on a successfully handled message' do
108
+ it 'should not unreceive' do
109
+ client.should_receive(:unreceive).never
110
+
111
+ worker.consume(name, raw_headers) {|msg| true }
112
+ worker.new.run
113
+ end
114
+ end
115
+ end
116
+ end
55
117
  end
56
118
  end
57
119
 
data/stapfen.gemspec CHANGED
@@ -18,5 +18,5 @@ Gem::Specification.new do |gem|
18
18
  gem.require_paths = ["lib"]
19
19
 
20
20
 
21
- gem.add_dependency('stomp', '>= 1.2.8')
21
+ gem.add_dependency('stomp', '>= 1.2.14') # 1.2.14 fixes Stomp::Client#unreceive behavior
22
22
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stapfen
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-28 00:00:00.000000000 Z
12
+ date: 2013-08-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: stomp
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: 1.2.8
21
+ version: 1.2.14
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
- version: 1.2.8
29
+ version: 1.2.14
30
30
  description: A simple gem for writing good basic STOMP workers
31
31
  email:
32
32
  - rtyler.croy@lookout.com
@@ -63,7 +63,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
63
63
  version: '0'
64
64
  segments:
65
65
  - 0
66
- hash: -3534259377773611788
66
+ hash: -2572111186363930880
67
67
  required_rubygems_version: !ruby/object:Gem::Requirement
68
68
  none: false
69
69
  requirements:
@@ -72,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
72
72
  version: '0'
73
73
  segments:
74
74
  - 0
75
- hash: -3534259377773611788
75
+ hash: -2572111186363930880
76
76
  requirements: []
77
77
  rubyforge_project:
78
78
  rubygems_version: 1.8.25