resque-throttle 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -19,3 +19,4 @@ rdoc
19
19
  pkg
20
20
 
21
21
  ## PROJECT::SPECIFIC
22
+ .idea
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
data/lib/resque/resque.rb CHANGED
@@ -18,14 +18,14 @@ module Resque
18
18
  private
19
19
 
20
20
  def should_throttle?(klass)
21
- return false unless throttle_job?(klass)
22
-
23
- if redis.get(klass.key)
24
- return true
25
- else
26
- redis.set(klass.key, true, klass.throttle)
27
- return false
28
- end
21
+ return false if !throttle_job?(klass) || klass.disabled
22
+ return true if key_found?(klass)
23
+ redis.set(klass.key, true, klass.can_run_every)
24
+ return false
25
+ end
26
+
27
+ def key_found?(klass)
28
+ redis.get(klass.key)
29
29
  end
30
30
 
31
31
  def throttle_job?(klass)
@@ -1,16 +1,32 @@
1
1
  module Resque
2
+ class SettingNotFound < RuntimeError; end
3
+
2
4
  class ThrottledJob < Job
3
-
4
- def self.identifier
5
- nil
5
+
6
+ THROTTLE_DEFAULTS = {
7
+ :can_run_every => 1800,
8
+ :enqueued => 'Your job has been submitted. You will receive an email with a download link shortly.',
9
+ :throttled => 'Frequency has been exceeded. Job not submitted. Try again a little later.',
10
+ :latest => 'Download the most recent result',
11
+ :disabled => false,
12
+ :identifier => nil
13
+ }
14
+
15
+ def self.settings
16
+ @settings ||= THROTTLE_DEFAULTS.dup
6
17
  end
7
-
8
- def self.throttle
9
- 1800
18
+
19
+ def self.throttle(args = {})
20
+ settings.merge!(args)
10
21
  end
11
-
22
+
12
23
  def self.key
13
24
  [self.to_s, identifier].compact.join(":")
14
25
  end
26
+
27
+ def self.method_missing(method, *args)
28
+ raise SettingNotFound("Could not find the #{method} setting") if !settings.key?(method)
29
+ settings[method]
30
+ end
15
31
  end
16
32
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{resque-throttle}
8
- s.version = "0.1.0"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["scotttam"]
12
- s.date = %q{2010-02-17}
12
+ s.date = %q{2010-02-18}
13
13
  s.description = %q{resque-throttle is an extension to the resque queue system that restricts the frequency in which certain jobs are run. Add more description here.}
14
14
  s.email = %q{scott@zendesk.com}
15
15
  s.extra_rdoc_files = [
@@ -1,4 +1,4 @@
1
- require 'test_helper.rb'
1
+ require '../test_helper.rb'
2
2
 
3
3
  class ResqueTest < Test::Unit::TestCase
4
4
 
@@ -23,6 +23,11 @@ class ResqueTest < Test::Unit::TestCase
23
23
  assert_raises(Resque::ThrottledError) { 2.times { Resque.enqueue(OneHourThrottledJob, @bogus_args) } }
24
24
  end
25
25
  end
26
+
27
+ should "enqueue a job without throttling if the job is disabled" do
28
+ Resque.expects(:enqueue_without_throttle).twice
29
+ 2.times { Resque.enqueue(DisabledThrottledJob, @bogus_args) }
30
+ end
26
31
  end
27
32
  end
28
33
  end
@@ -1,37 +1,79 @@
1
- require 'test_helper.rb'
1
+ require '../test_helper.rb'
2
2
 
3
3
  class ThrottledJobTest < Test::Unit::TestCase
4
-
5
- context "Resque::ThrottledJob" do
4
+
5
+ context "Resque::ThrottledJob" do
6
6
  should "instantiate a new Resque::ThrottledJob" do
7
7
  assert Resque::ThrottledJob.new(:queue_name, OneHourThrottledJob)
8
8
  end
9
-
10
- context "#throttle" do
11
- should "return the number of seconds in which to throttle these jobs" do
12
- assert_equal 3600, OneHourThrottledJob.throttle
13
- end
14
-
15
- should "default to 30 minutes (1800 seconds) if not provided" do
16
- assert_equal 1800, DefaultThrottledJob.throttle
17
- end
18
- end
19
-
20
- context "#identifier" do
21
- should "return an additional key identifier used in storing the key in the redis SET" do
22
- assert_equal "my_identifier", IdetifierThrottledJob.identifier
9
+
10
+ context "settings" do
11
+ context "#can_run_every" do
12
+ should "return the number of seconds in which to throttle these jobs" do
13
+ assert_equal 3600, OneHourThrottledJob.can_run_every
14
+ end
15
+
16
+ should "default to 30 minutes (1800 seconds) if not provided" do
17
+ assert_equal 1800, DefaultThrottledJob.can_run_every
18
+ end
19
+ end
20
+
21
+ context "#identifier" do
22
+ should "return an additional key identifier used in storing the key in the redis SET" do
23
+ assert_equal "my_identifier", IdetifierThrottledJob.identifier
24
+ end
25
+
26
+ should "return nil if not defined" do
27
+ assert_nil DefaultThrottledJob.identifier
28
+ end
29
+ end
30
+
31
+ context "#enqueued" do
32
+ should "have a default message if not overrriden" do
33
+ assert_equal "Your job has been submitted. You will receive an email with a download link shortly.", DefaultThrottledJob.enqueued
34
+ end
35
+
36
+ should "be able to be overriden" do
37
+ assert_equal "Yada Yada", OneHourThrottledJob.enqueued
38
+ end
23
39
  end
24
-
25
- should "return nil if not defined" do
26
- assert_nil DefaultThrottledJob.identifier
40
+
41
+ context "#throttled" do
42
+ should "have a default message if not overrriden" do
43
+ assert_equal "Frequency has been exceeded. Job not submitted. Try again a little later.", DefaultThrottledJob.throttled
44
+ end
45
+
46
+ should "be able to be overriden" do
47
+ assert_equal "Yada Yada", OneHourThrottledJob.throttled
48
+ end
49
+ end
50
+
51
+ context "#latest" do
52
+ should "have a default message if not overrriden" do
53
+ assert_equal "Download the most recent result", DefaultThrottledJob.latest
54
+ end
55
+
56
+ should "be able to be overriden" do
57
+ assert_equal "Yada Yada", OneHourThrottledJob.latest
58
+ end
59
+ end
60
+
61
+ context "#disabled" do
62
+ should "not be disabled by default" do
63
+ assert !DefaultThrottledJob.disabled
64
+ end
65
+
66
+ should "be able to be overriden" do
67
+ assert DisabledThrottledJob.disabled
68
+ end
27
69
  end
28
70
  end
29
-
71
+
30
72
  context "#key" do
31
73
  should "consist of the class name and the identifier" do
32
- assert_equal "IdetifierThrottledJob:my_identifier", IdetifierThrottledJob.key
74
+ assert_equal "IdetifierThrottledJob:my_identifier", IdetifierThrottledJob.key
33
75
  end
34
-
76
+
35
77
  should "consist of just the class name if the identifier is not provided" do
36
78
  assert_equal "DefaultThrottledJob", DefaultThrottledJob.key
37
79
  end
data/test/test_helper.rb CHANGED
@@ -23,10 +23,12 @@ end
23
23
  class OneHourThrottledJob < Resque::ThrottledJob
24
24
  @queue = :some_queue
25
25
 
26
- def self.throttle
27
- 3600
28
- end
29
-
26
+ throttle :can_run_every => 3600,
27
+ :enqueued => "Yada Yada",
28
+ :throttled => "Yada Yada",
29
+ :latest => "Yada Yada"
30
+
31
+
30
32
  def self.perform(some_id, some_other_thing)
31
33
  end
32
34
  end
@@ -34,10 +36,17 @@ end
34
36
  class IdetifierThrottledJob < Resque::ThrottledJob
35
37
  @queue = :some_queue
36
38
 
37
- def self.identifier
38
- "my_identifier"
39
+ throttle :identifier => "my_identifier"
40
+
41
+ def self.perform(some_id, some_other_thing)
39
42
  end
40
-
43
+ end
44
+
45
+ class DisabledThrottledJob < Resque::ThrottledJob
46
+ @queue = :some_queue
47
+
48
+ throttle :disabled => true
49
+
41
50
  def self.perform(some_id, some_other_thing)
42
51
  end
43
52
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resque-throttle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - scotttam
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-17 00:00:00 -08:00
12
+ date: 2010-02-18 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency