resque-rate_limited_queue 1.0.0 → 1.0.2

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: 0adf19f43b30aaaa865beda17e99e2082b5f6301
4
- data.tar.gz: 7260656ade9a2e129ccf07ac137ecd5c010aee45
3
+ metadata.gz: 10b17055e5a285f6724e2bb8b857e8258b78dfd5
4
+ data.tar.gz: 144bf7bb14973f0e3e1a317c58aff1f46965c4f6
5
5
  SHA512:
6
- metadata.gz: c978c05cc91e3c24d0a685af044e0f2791bff20b08f864a4d12722ab7a9779d94cdef347c242c6cf749eec99b45bb223204b65489afa1e9ae017d72459dc50c9
7
- data.tar.gz: 0c6b146eedb305a0522bf920b69600c8339e9e23c62e8ecaaf5d8be9019f1184d0aa60fdf2122ee797716b493fa203c181412e0ff660f8ee3f21ffed5c777d90
6
+ metadata.gz: 048a567b7ffc945ec45334e215c55f86596569f9780b1e3564b067b4fb83935ab08e5fdde8a0a79f339a2226900d15f67014c0daba1f31c636e01a03d148597a
7
+ data.tar.gz: 294537d08ab7611a9611658d4ae53fdaad4b3bbb37e73d936004cec4fb4dcdba82f6cbdf29ef05a1bf19b9e3a812d625b14ac8b52a8c38a5aa00cd4a312af475
data/README.md CHANGED
@@ -15,7 +15,7 @@ If you are using another API, then you need to write a little code that catches
15
15
  Add this line to your application's Gemfile:
16
16
 
17
17
  ```ruby
18
- gem 'resque-rate-limited-queue'
18
+ gem 'resque-rate_limited_queue'
19
19
  ```
20
20
 
21
21
  And then execute:
@@ -24,7 +24,7 @@ And then execute:
24
24
 
25
25
  Or install it yourself as:
26
26
 
27
- $ gem install resque-rate-limited-queue
27
+ $ gem install resque-rate_limited_queue
28
28
 
29
29
  ## Usage
30
30
 
@@ -80,8 +80,8 @@ Resque::Plugins::RateLimitedQueue.AngellistQueue.un_pause
80
80
  MyQueue.un_pause
81
81
  MyJob.un_pause
82
82
  ```
83
- ### A Pausable job using one of the build-in queues (Twitter, Angellist, Evernote)
84
- If you're using the [twitter gem[ (https://github.com/sferik/twitter), this is really simple. Instead of queuing using Resque.enqueue, you just use Resque::Plugins::RateLimitedQueue:TwitterQueue.enqueue.
83
+ ### A pausable job using one of the build-in queues (Twitter, Angellist, Evernote)
84
+ If you're using the [twitter gem](https://github.com/sferik/twitter), this is really simple. Instead of queuing using Resque.enqueue, you just use Resque::Plugins::RateLimitedQueue:TwitterQueue.enqueue.
85
85
 
86
86
  Make sure your code in perform doesn't catch the rate_limit exception.
87
87
 
@@ -124,7 +124,7 @@ end
124
124
  ````
125
125
 
126
126
  ### Multiple classes of pausable job using a new api
127
- If you have more than one class of job you want to queue to the api, then you can need to add another Queue class. This isn't hard
127
+ If you have more than one class of job you want to queue to the api, then you need to add another Queue class. This isn't hard
128
128
 
129
129
  ```ruby
130
130
  class MyApiQueue < Resque::Plugins::RateLimitedQueue::BaseApiQueue
@@ -148,7 +148,8 @@ All the functions are class methods
148
148
  rate_limited_enqueue(klass, *params)
149
149
  rate_limited_requeue(klass, *params)
150
150
  ````
151
- Queue the job specified to the resque queue specified by `@queue`. `rate_limited_requeue` is intended for use when you need the job to be pushed back to the queue; it just calls `rate_limited_queue`, but they are split to make testing with stubs easier.
151
+ Queue the job specified to the resque queue specified by `@queue`. `rate_limited_requeue` is intended for use when you need the job to be pushed back to the queue; there are two reasons to split this from `queue`. Firstly it makes testing easier - secondly there is a boundary condition when you need to requeue the last job in the queue.
152
+ `, but they are split to make testing with stubs easier.
152
153
 
153
154
  ```ruby
154
155
  pause
@@ -1,3 +1,3 @@
1
1
  module RateLimitedQueue
2
- VERSION = '1.0.0'
2
+ VERSION = '1.0.2'
3
3
  end
@@ -25,8 +25,14 @@ module Resque
25
25
  end
26
26
 
27
27
  def rate_limited_requeue(klass, *params)
28
- # split from above to make it easy to stub for testing
29
- rate_limited_enqueue(klass, *params)
28
+ # if the queue is empty, this was the last job - so queue to the paused queue
29
+ with_lock do
30
+ if paused?(true)
31
+ Resque.enqueue_to(paused_queue_name, klass, *params)
32
+ else
33
+ Resque.enqueue_to(@queue, klass, *params)
34
+ end
35
+ end
30
36
  end
31
37
 
32
38
  def pause_until(timestamp)
@@ -49,8 +55,15 @@ module Resque
49
55
  false
50
56
  end
51
57
 
52
- def paused?
53
- Resque.redis.exists(RESQUE_PREFIX + paused_queue_name)
58
+ def paused?(unknown = false)
59
+ # parameter is what to return if the queue is empty, and so the state is unknown
60
+ if Resque.redis.exists(RESQUE_PREFIX + @queue.to_s)
61
+ false
62
+ elsif Resque.redis.exists(RESQUE_PREFIX + paused_queue_name)
63
+ true
64
+ else
65
+ unknown
66
+ end
54
67
  end
55
68
 
56
69
  def paused_queue_name
@@ -10,11 +10,11 @@ class RateLimitedTestQueue
10
10
  rate_limited_requeue(self, succeed) unless succeed
11
11
  end
12
12
 
13
- def self.queue_name
13
+ def self.queue_name_private
14
14
  @queue.to_s
15
15
  end
16
16
 
17
- def self.queue
17
+ def self.queue_private
18
18
  @queue
19
19
  end
20
20
  end
@@ -26,7 +26,7 @@ describe Resque::Plugins::RateLimitedQueue do
26
26
 
27
27
  shared_examples_for 'queue' do |queue_suffix|
28
28
  it 'should queue to the correct queue' do
29
- queue_param = queue_suffix.empty? ? RateLimitedTestQueue.queue : "#{RateLimitedTestQueue.queue_name}#{queue_suffix}"
29
+ queue_param = queue_suffix.empty? ? RateLimitedTestQueue.queue_private : "#{RateLimitedTestQueue.queue_name_private}#{queue_suffix}"
30
30
  Resque.should_receive(:enqueue_to).with(queue_param, nil, nil)
31
31
  RateLimitedTestQueue.rate_limited_enqueue(nil, nil)
32
32
  end
@@ -55,19 +55,18 @@ describe Resque::Plugins::RateLimitedQueue do
55
55
  Resque.should_not_receive(:enqueue_to)
56
56
  RateLimitedTestQueue.perform(true)
57
57
  end
58
-
59
58
  end
60
59
 
61
60
  describe 'pause' do
62
61
  it 'should rename the queue to paused' do
63
- Resque.redis.should_receive(:renamenx).with("queue:#{RateLimitedTestQueue.queue_name}", "queue:#{RateLimitedTestQueue.queue_name}_paused")
62
+ Resque.redis.should_receive(:renamenx).with("queue:#{RateLimitedTestQueue.queue_name_private}", "queue:#{RateLimitedTestQueue.queue_name_private}_paused")
64
63
  RateLimitedTestQueue.pause
65
64
  end
66
65
  end
67
66
 
68
67
  describe 'un_pause' do
69
68
  it 'should not unpause the queue' do
70
- Resque.redis.should_not_receive(:renamenx).with("queue:#{RateLimitedTestQueue.queue_name}", "queue:#{RateLimitedTestQueue.queue_name}_paused")
69
+ Resque.redis.should_not_receive(:renamenx).with("queue:#{RateLimitedTestQueue.queue_name_private}", "queue:#{RateLimitedTestQueue.queue_name_private}_paused")
71
70
  RateLimitedTestQueue.un_pause
72
71
  end
73
72
  end
@@ -105,7 +104,7 @@ describe Resque::Plugins::RateLimitedQueue do
105
104
 
106
105
  describe 'perform' do
107
106
  it 'should not execute the block' do
108
- Resque.should_receive(:enqueue_to).with("#{RateLimitedTestQueue.queue_name}_paused", RateLimitedTestQueue, true)
107
+ Resque.should_receive(:enqueue_to).with("#{RateLimitedTestQueue.queue_name_private}_paused", RateLimitedTestQueue, true)
109
108
  RateLimitedTestQueue.should_not_receive(:perform)
110
109
  RateLimitedTestQueue.around_perform_with_check_and_requeue(true)
111
110
  end
@@ -113,7 +112,7 @@ describe Resque::Plugins::RateLimitedQueue do
113
112
 
114
113
  describe 'un_pause' do
115
114
  it 'should rename the queue to live' do
116
- Resque.redis.should_receive(:renamenx).with("queue:#{RateLimitedTestQueue.queue_name}_paused", "queue:#{RateLimitedTestQueue.queue_name}")
115
+ Resque.redis.should_receive(:renamenx).with("queue:#{RateLimitedTestQueue.queue_name_private}_paused", "queue:#{RateLimitedTestQueue.queue_name_private}")
117
116
  RateLimitedTestQueue.un_pause
118
117
  end
119
118
  end
@@ -153,7 +152,6 @@ describe Resque::Plugins::RateLimitedQueue do
153
152
  expect { RateLimitedTestQueue.un_pause }.to_not raise_error
154
153
  end
155
154
  end
156
-
157
155
  end
158
156
 
159
157
  context 'with other errror' do
@@ -178,7 +176,8 @@ describe Resque::Plugins::RateLimitedQueue do
178
176
  describe 'paused?' do
179
177
  context 'with paused queue' do
180
178
  before do
181
- Resque.redis.stub(:exists).and_return(true)
179
+ Resque.redis.stub(:exists).with("queue:#{RateLimitedTestQueue.queue_name_private}_paused").and_return(true)
180
+ Resque.redis.stub(:exists).with("queue:#{RateLimitedTestQueue.queue_name_private}").and_return(false)
182
181
  end
183
182
 
184
183
  it 'should return the true if the paused queue exists' do
@@ -188,12 +187,25 @@ describe Resque::Plugins::RateLimitedQueue do
188
187
 
189
188
  context 'with un paused queue' do
190
189
  before do
191
- Resque.redis.stub(:exists).and_return(false)
190
+ Resque.redis.stub(:exists).with("queue:#{RateLimitedTestQueue.queue_name_private}_paused").and_return(false)
191
+ Resque.redis.stub(:exists).with("queue:#{RateLimitedTestQueue.queue_name_private}").and_return(true)
192
192
  end
193
193
 
194
- it 'should return the false if the paused queue does not exist' do
194
+ it 'should return the false if the main queue exists exist' do
195
195
  expect(RateLimitedTestQueue.paused?).to eq(false)
196
196
  end
197
197
  end
198
+
199
+ context 'with unknown queue state' do
200
+ before do
201
+ Resque.redis.stub(:exists).with("queue:#{RateLimitedTestQueue.queue_name_private}_paused").and_return(false)
202
+ Resque.redis.stub(:exists).with("queue:#{RateLimitedTestQueue.queue_name_private}").and_return(false)
203
+ end
204
+
205
+ it 'should return the default' do
206
+ expect(RateLimitedTestQueue.paused?(true)).to eq(true)
207
+ expect(RateLimitedTestQueue.paused?(false)).to eq(false)
208
+ end
209
+ end
198
210
  end
199
211
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resque-rate_limited_queue
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Greg Dowling
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-30 00:00:00.000000000 Z
11
+ date: 2015-01-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: resque
@@ -219,7 +219,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
219
219
  version: '0'
220
220
  requirements: []
221
221
  rubyforge_project:
222
- rubygems_version: 2.4.2
222
+ rubygems_version: 2.4.3
223
223
  signing_key:
224
224
  specification_version: 4
225
225
  summary: A Resque plugin to help manage jobs that use rate limited apis, pausing when
@@ -231,3 +231,4 @@ test_files:
231
231
  - spec/rate_limited_queue_spec.rb
232
232
  - spec/rate_limited_un_pause_spec.rb
233
233
  - spec/spec_helper.rb
234
+ has_rdoc: