resque-throttler 0.1.4 → 0.1.5

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: 09888d1679af9193b4323ac267b2d1488d911d8a
4
- data.tar.gz: 287f898deb3c12a723527fe024645dd63f7821de
3
+ metadata.gz: 2aec65024025fe55d1b9c7b387f9cb748e424c0e
4
+ data.tar.gz: 452e1de58c438865919553cb0677b14684a5c99a
5
5
  SHA512:
6
- metadata.gz: b3150786f6c903bb11d1a4386ac943c681411f434a6f4bab42ff9d659bc20754ba762273d06d9de80c8b64a323fdb49333a6029dc7c188e4dc421fd9cbe4bde5
7
- data.tar.gz: ecd025b6e2946ea1777af977ee399cc97259269820b2c6280dbed93bbec561544dff2a90ee4bca0678120392a47cd8a2c68ed1db2b81e6054ab819dba776d02a
6
+ metadata.gz: 0bbb257494f39dcf211b35cd06f303a76178a6ddf9d33a2cb612cceb9173e573ea2454ef8613544ffca1ceb9416edc12db841272afea85861be3ebe67652dca2
7
+ data.tar.gz: cdd423838bb1ea520f85104fc574ce2dd9f957b78e96eb2648f1e5292e225ac0cd999d78c8a8da2b7ba4892d2b875be0283922ff83b136f574e339b99c527389
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- resque-throttler (0.1.0)
4
+ resque-throttler (0.1.4)
5
5
  resque (~> 1.25)
6
6
 
7
7
  GEM
@@ -33,7 +33,7 @@ GEM
33
33
  rack
34
34
  rake (10.3.2)
35
35
  redis (3.0.7)
36
- redis-namespace (1.5.0)
36
+ redis-namespace (1.5.1)
37
37
  redis (~> 3.0, >= 3.0.4)
38
38
  resque (1.25.2)
39
39
  mono_logger (~> 1.0)
@@ -31,7 +31,7 @@ module Resque::Plugins
31
31
  end
32
32
 
33
33
  def queue_rate_limited?(queue)
34
- @rate_limits[queue.to_s]
34
+ !!@rate_limits[queue.to_s]
35
35
  end
36
36
 
37
37
  def queue_at_or_over_rate_limit?(queue)
@@ -65,9 +65,16 @@ Resque.extend(Resque::Plugins::Throttler)
65
65
 
66
66
  class Resque::Job
67
67
 
68
+ def initialize_with_throttler(queue, payload)
69
+ @throttled = Resque.queue_rate_limited?(queue)
70
+ @throttler_uuid = SecureRandom.uuid
71
+ initialize_without_throttler(queue, payload)
72
+ end
73
+ alias_method :initialize_without_throttler, :initialize
74
+ alias_method :initialize, :initialize_with_throttler
75
+
68
76
  def perform_with_throttler
69
- if Resque.queue_rate_limited?(self.queue)
70
- @throttler_uuid = SecureRandom.uuid
77
+ if @throttled
71
78
  begin
72
79
  # TODO this needs to be wrapped in a transcation
73
80
  redis.hmset("throttler:jobs:#{@throttler_uuid}", "started_at", Time.now.to_i)
@@ -86,7 +93,7 @@ class Resque::Job
86
93
  # This is added for when there is a dirty exit
87
94
  # TODO: testme
88
95
  def fail_with_throttler(exception)
89
- if defined?(@throttler_uuid)
96
+ if @throttled
90
97
  redis.hmset("throttler:jobs:#{@throttler_uuid}", "ended_at", Time.now.to_i)
91
98
  end
92
99
  fail_without_throttler(exception)
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "resque-throttler"
3
- s.version = '0.1.4'
3
+ s.version = '0.1.5'
4
4
  s.licenses = ['MIT']
5
5
  s.authors = ["Jon Bracy"]
6
6
  s.email = ["jonbracy@gmail.com"]
@@ -17,6 +17,21 @@ class Resque::JobTest < Minitest::Test
17
17
  Resque.instance_variable_set(:@rate_limits, {})
18
18
  end
19
19
 
20
+ test 'Resque::Job::initialize sets up @throttled and @throttler_uuid' do
21
+ SecureRandom.expects(:uuid).returns("jobuuid").twice
22
+ Resque.rate_limit(:myqueue, :at => 10, :per => 1)
23
+
24
+ job = Resque::Job.new(:other_queue, { 'class' => 'MyJob', 'args' => [] })
25
+ assert_equal 'jobuuid', job.instance_variable_get(:@throttler_uuid)
26
+ assert_equal false, job.instance_variable_get(:@throttled)
27
+
28
+
29
+
30
+ job = Resque::Job.new(:myqueue, { 'class' => 'MyJob', 'args' => [] })
31
+ assert_equal 'jobuuid', job.instance_variable_get(:@throttler_uuid)
32
+ assert_equal true, job.instance_variable_get(:@throttled)
33
+ end
34
+
20
35
  test "Resque::Job::perform on unthrottled job" do
21
36
  Resque.rate_limit(:myqueue, :at => 10, :per => 1)
22
37
 
@@ -36,6 +51,7 @@ class Resque::JobTest < Minitest::Test
36
51
  end
37
52
 
38
53
  test "Resque::Job::perform on throttled job" do
54
+ SecureRandom.expects(:uuid).returns("jobuuid")
39
55
  Resque.rate_limit(:myqueue, :at => 10, :per => 1)
40
56
 
41
57
  job = Resque::Job.new(:myqueue, {
@@ -44,7 +60,6 @@ class Resque::JobTest < Minitest::Test
44
60
  })
45
61
 
46
62
  travel_to Time.now do
47
- SecureRandom.expects(:uuid).returns("jobuuid")
48
63
  Resque.redis.expects(:hmset).with("throttler:jobs:jobuuid", "started_at", Time.now.to_i).once
49
64
  Resque.redis.expects(:sadd).with("throttler:myqueue_uuids", "jobuuid").once
50
65
  Resque.redis.expects(:hmset).with("throttler:jobs:jobuuid", "ended_at", Time.now.to_i).once
@@ -54,6 +69,7 @@ class Resque::JobTest < Minitest::Test
54
69
  end
55
70
 
56
71
  test "Resque::Job::perform on throttled job with job that throws error" do
72
+ SecureRandom.expects(:uuid).returns("jobuuid")
57
73
  Resque.rate_limit(:myqueue, :at => 10, :per => 1)
58
74
 
59
75
  job = Resque::Job.new('myqueue', {
@@ -62,7 +78,6 @@ class Resque::JobTest < Minitest::Test
62
78
  })
63
79
 
64
80
  travel_to Time.now do
65
- SecureRandom.expects(:uuid).returns("jobuuid")
66
81
  Resque.redis.expects(:hmset).with("throttler:jobs:jobuuid", "started_at", Time.now.to_i).once
67
82
  Resque.redis.expects(:sadd).with("throttler:myqueue_uuids", "jobuuid").once
68
83
  Resque.redis.expects(:hmset).with("throttler:jobs:jobuuid", "ended_at", Time.now.to_i).once
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resque-throttler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Bracy