fhwang-resque-throttle 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Add dependencies required to use your gem here.
4
+ gem 'resque', '>= 1.6.0'
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "jeweler", "~> 1.5.2"
10
+ gem 'thoughtbot-shoulda', '>= 0'
11
+ gem 'mocha', '>= 0.9.8'
12
+ end
@@ -0,0 +1,34 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.5.2)
6
+ bundler (~> 1.0.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ json (1.4.6)
10
+ mocha (0.9.12)
11
+ rack (1.2.1)
12
+ rake (0.8.7)
13
+ redis (2.0.10)
14
+ redis-namespace (0.8.0)
15
+ redis (< 3.0.0)
16
+ resque (1.10.0)
17
+ json (~> 1.4.6)
18
+ redis-namespace (~> 0.8.0)
19
+ sinatra (>= 0.9.2)
20
+ vegas (~> 0.1.2)
21
+ sinatra (1.0)
22
+ rack (>= 1.0)
23
+ thoughtbot-shoulda (2.11.1)
24
+ vegas (0.1.7)
25
+ rack (>= 1.0.0)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ jeweler (~> 1.5.2)
32
+ mocha (>= 0.9.8)
33
+ resque (>= 1.6.0)
34
+ thoughtbot-shoulda
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Zendesk
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,66 @@
1
+ resque-throttle
2
+ ===============
3
+
4
+ Resque Throttle is a plugin for the [Resque][0] queueing system
5
+ (http://github.com/defunkt/resque). It adds a ThrottledJob class that will
6
+ limit the amount of times it can be enqueued per class.
7
+
8
+ To use
9
+ ------
10
+ The job you wish to throttle should inherit from the Resque::ThrottledJob class.
11
+
12
+ In your class, add the can_run_every in which the job should be throttled. Example:
13
+
14
+ class MyThrottledJob < Resque::ThrottledJob
15
+ throttle :can_run_every => 24.hours
16
+
17
+ #rest of your class here
18
+ end
19
+
20
+ By default, the key which identifies the job is simply the class name. This "identifier"
21
+ is stored in a redis key with a TTL equal to that of the can_run_every. Thus the
22
+ default behavior is a single Job class inheriting from Resque::ThrottledJob can only
23
+ run every 30 minutes.
24
+
25
+ If you'd like to override that to be more granular, you can do that in the identifier class method
26
+ by returning a string. For example, if you want the job to be limited to once a day per
27
+ account, do something like the following:
28
+
29
+ class MyThrottledJob < Resque::ThrottledJob
30
+ throttle :can_run_every => 24.hours
31
+
32
+ def self.identifier(*args)
33
+ account_id = *args
34
+ "account_id:#{account_id}"
35
+ end
36
+
37
+ #rest of your class here
38
+ end
39
+
40
+ The *args passed to identifier are the same arguments that are passed to perform.
41
+
42
+ When a job is throttled, it will raise a ThrottledError and the job will not be enqueued.
43
+
44
+ Contributing
45
+ ------------
46
+
47
+ Once you've made your commits:
48
+
49
+ 1. [Fork][1] Resque Throttle
50
+ 2. Create a topic branch - `git checkout -b my_branch`
51
+ 3. Push to your branch - `git push origin my_branch`
52
+ 4. Create an [Issue][2] with a link to your branch
53
+ 5. That's it!
54
+
55
+ Author
56
+ ------
57
+ Scott J. Tamosunas :: tamosunas@gmail.com :: @scotttam
58
+
59
+ Copyright
60
+ ---------
61
+ Copyright (c) 2010 Zendesk. See LICENSE for details.
62
+
63
+ [0]: http://github.com/defunkt/resque
64
+ [1]: http://help.github.com/forking/
65
+ [2]: http://github.com/scotttam/resque-throttle/issues
66
+
@@ -0,0 +1,42 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "fhwang-resque-throttle"
16
+ gem.homepage = "http://github.com/fhwang/resque-throttle"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{resque-throttle is an extension to the resque queue system that restricts the frequency in which certain jobs are run.}
19
+ gem.description = %Q{resque-throttle is an extension to the resque queue system that restricts the frequency in which certain jobs are run.}
20
+ gem.email = "francis.hwang@profitably.com"
21
+ gem.authors = ["Francis Hwang"]
22
+ end
23
+ Jeweler::RubygemsDotOrgTasks.new
24
+
25
+ require 'rake/testtask'
26
+ Rake::TestTask.new(:test) do |test|
27
+ test.libs << 'lib' << 'test'
28
+ test.pattern = 'test/**/test_*.rb'
29
+ test.verbose = true
30
+ end
31
+
32
+ task :default => :test
33
+
34
+ require 'rake/rdoctask'
35
+ Rake::RDocTask.new do |rdoc|
36
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
37
+
38
+ rdoc.rdoc_dir = 'rdoc'
39
+ rdoc.title = "fhwang-resque-throttle #{version}"
40
+ rdoc.rdoc_files.include('README*')
41
+ rdoc.rdoc_files.include('lib/**/*.rb')
42
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.0
@@ -0,0 +1,3 @@
1
+ require "resque/resque"
2
+ require "resque/throttle"
3
+ require "resque/throttled_job"
@@ -0,0 +1,40 @@
1
+ require "resque/throttle"
2
+
3
+ module Resque
4
+ extend self
5
+
6
+ # Raised when trying to create a job that is throttled
7
+ class ThrottledError < RuntimeError; end
8
+
9
+ def enqueue_with_throttle(klass, *args)
10
+ if should_throttle?(klass, *args)
11
+ unless klass.throttle_settings[:silent]
12
+ raise(
13
+ ThrottledError,
14
+ "#{klass} with key #{klass.key(*args)} has exceeded it's throttle limit"
15
+ )
16
+ end
17
+ else
18
+ enqueue_without_throttle(klass, *args)
19
+ end
20
+ end
21
+ alias_method :enqueue_without_throttle, :enqueue
22
+ alias_method :enqueue, :enqueue_with_throttle
23
+
24
+ private
25
+
26
+ def should_throttle?(klass, *args)
27
+ return false if !throttle_job?(klass) || klass.disabled
28
+ return true if key_found?(klass, *args)
29
+ redis.setex(klass.key(*args), klass.can_run_every, true)
30
+ return false
31
+ end
32
+
33
+ def key_found?(klass, *args)
34
+ redis.get(klass.key(*args))
35
+ end
36
+
37
+ def throttle_job?(klass)
38
+ klass.ancestors.include?(Resque::ThrottledJob)
39
+ end
40
+ end
@@ -0,0 +1,5 @@
1
+ require 'resque'
2
+
3
+ module Resque
4
+ end
5
+
@@ -0,0 +1,34 @@
1
+ module Resque
2
+ class SettingNotFound < RuntimeError; end
3
+
4
+ class ThrottledJob
5
+
6
+ THROTTLE_DEFAULTS = {
7
+ :can_run_every => 1800,
8
+ :disabled => false,
9
+ }
10
+
11
+ def self.throttle_settings
12
+ @throttle_settings ||= THROTTLE_DEFAULTS.dup
13
+ end
14
+
15
+ def self.throttle(args = {})
16
+ throttle_settings.merge!(args)
17
+ end
18
+
19
+ def self.identifier(*args)
20
+ end
21
+
22
+ def self.key(*args)
23
+ ['resque-throttle', self.to_s, identifier(*args)].compact.join(":")
24
+ end
25
+
26
+ def self.can_run_every
27
+ throttle_settings[:can_run_every]
28
+ end
29
+
30
+ def self.disabled
31
+ throttle_settings[:disabled]
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,65 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{resque-throttle}
8
+ s.version = "0.2.19"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Scott J. Tamosunas"]
12
+ s.date = %q{2010-03-10}
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
+ s.email = %q{tamosunas@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.markdown"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.markdown",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/resque-throttle.rb",
27
+ "lib/resque/resque.rb",
28
+ "lib/resque/throttle.rb",
29
+ "lib/resque/throttled_job.rb",
30
+ "resque-throttle.gemspec",
31
+ "test/resque/resque_test.rb",
32
+ "test/resque/throttled_job_test.rb",
33
+ "test/test_helper.rb"
34
+ ]
35
+ s.homepage = %q{http://github.com/scotttam/resque-throttle}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.5}
39
+ s.summary = %q{resque-throttle is an extension to the resque queue system that restricts the frequency in which certain jobs are run.}
40
+ s.test_files = [
41
+ "test/resque/resque_test.rb",
42
+ "test/resque/throttled_job_test.rb",
43
+ "test/test_helper.rb"
44
+ ]
45
+
46
+ if s.respond_to? :specification_version then
47
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
48
+ s.specification_version = 3
49
+
50
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
51
+ s.add_runtime_dependency(%q<resque>, [">= 1.6.0"])
52
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
53
+ s.add_development_dependency(%q<mocha>, [">= 0.9.8"])
54
+ else
55
+ s.add_dependency(%q<resque>, [">= 1.6.0"])
56
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
57
+ s.add_dependency(%q<mocha>, [">= 0.9.8"])
58
+ end
59
+ else
60
+ s.add_dependency(%q<resque>, [">= 1.6.0"])
61
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
62
+ s.add_dependency(%q<mocha>, [">= 0.9.8"])
63
+ end
64
+ end
65
+
@@ -0,0 +1,42 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class ResqueTest < Test::Unit::TestCase
4
+
5
+ context "Resque" do
6
+ setup do
7
+ Resque.redis.flushall
8
+ assert_nil Resque.redis.get(OneHourThrottledJob.key)
9
+ @bogus_args = "bogus_arg"
10
+ end
11
+
12
+ context "#enqueue" do
13
+ should "add a throttled job key to the set with the proper TTL (Expire)" do
14
+ Resque.expects(:enqueue_without_throttle).returns(true)
15
+ assert Resque.enqueue(IdentifierThrottledJob, @bogus_args)
16
+ assert(
17
+ Resque.redis.keys('*').include?(
18
+ "resque-throttle:IdentifierThrottledJob:my_bogus_arg"
19
+ )
20
+ )
21
+ assert_equal 3600, Resque.redis.ttl(IdentifierThrottledJob.key(@bogus_args))
22
+ end
23
+
24
+ context "job has not reached throttle limit" do
25
+ should "not add another job to the queue and raise a throttled exception" do
26
+ Resque.expects(:enqueue_without_throttle).once
27
+ assert_raises(Resque::ThrottledError) { 2.times { Resque.enqueue(OneHourThrottledJob, @bogus_args) } }
28
+ end
29
+ end
30
+
31
+ should "enqueue a job without throttling if the job is disabled" do
32
+ Resque.expects(:enqueue_without_throttle).twice
33
+ 2.times { Resque.enqueue(DisabledThrottledJob, @bogus_args) }
34
+ end
35
+
36
+ should "silently throttle a job if that's how resque-throttle is configured" do
37
+ Resque.expects(:enqueue_without_throttle).once
38
+ 2.times { Resque.enqueue(SilentThrottledJob, @bogus_args) }
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,57 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class ThrottledJobTest < Test::Unit::TestCase
4
+
5
+ context "Resque::ThrottledJob" do
6
+ should "instantiate a new Resque::ThrottledJob" do
7
+ assert Resque::ThrottledJob.new
8
+ end
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", IdentifierThrottledJob.identifier("identifier")
24
+ end
25
+
26
+ should "return nil if not defined" do
27
+ assert_nil DefaultThrottledJob.identifier
28
+ end
29
+ end
30
+
31
+ context "#disabled" do
32
+ should "not be disabled by default" do
33
+ assert !DefaultThrottledJob.disabled
34
+ end
35
+
36
+ should "be able to be overriden" do
37
+ assert DisabledThrottledJob.disabled
38
+ end
39
+ end
40
+ end
41
+
42
+ context "#key" do
43
+ should "consist of a prefix, the class name and the identifier" do
44
+ assert_equal(
45
+ "resque-throttle:IdentifierThrottledJob:my_identifier",
46
+ IdentifierThrottledJob.key("identifier")
47
+ )
48
+ end
49
+
50
+ should "consist of just the module prefix and the class name if the identifier is not provided" do
51
+ assert_equal(
52
+ "resque-throttle:DefaultThrottledJob", DefaultThrottledJob.key
53
+ )
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,59 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'mocha'
5
+
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+ require 'resque/resque'
9
+ require 'resque/throttle'
10
+ require 'resque/throttled_job'
11
+
12
+ class Test::Unit::TestCase
13
+ end
14
+
15
+ #fixture classes
16
+ class DefaultThrottledJob < Resque::ThrottledJob
17
+ @queue = :some_queue
18
+
19
+ def self.perform(some_id, some_other_thing)
20
+ end
21
+ end
22
+
23
+ class OneHourThrottledJob < Resque::ThrottledJob
24
+ @queue = :some_queue
25
+ throttle :can_run_every => 3600
26
+
27
+ def self.perform(some_id, some_other_thing)
28
+ end
29
+ end
30
+
31
+ class IdentifierThrottledJob < Resque::ThrottledJob
32
+ @queue = :some_queue
33
+
34
+ throttle :can_run_every => 3600
35
+
36
+ def self.perform(some_id, some_other_thing)
37
+ end
38
+
39
+ def self.identifier(*args)
40
+ first, second = *args
41
+ "my_#{first}"
42
+ end
43
+ end
44
+
45
+ class DisabledThrottledJob < Resque::ThrottledJob
46
+ @queue = :some_queue
47
+ throttle :disabled => true
48
+
49
+ def self.perform(some_id, some_other_thing)
50
+ end
51
+ end
52
+
53
+ class SilentThrottledJob < Resque::ThrottledJob
54
+ @queue = :some_queue
55
+ throttle :silent => true
56
+
57
+ def self.perform(some_id, some_other_thing)
58
+ end
59
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fhwang-resque-throttle
3
+ version: !ruby/object:Gem::Version
4
+ hash: 19
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
11
+ platform: ruby
12
+ authors:
13
+ - Francis Hwang
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-17 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ hash: 15
28
+ segments:
29
+ - 1
30
+ - 6
31
+ - 0
32
+ version: 1.6.0
33
+ type: :runtime
34
+ name: resque
35
+ prerelease: false
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 7
44
+ segments:
45
+ - 1
46
+ - 5
47
+ - 2
48
+ version: 1.5.2
49
+ type: :development
50
+ name: jeweler
51
+ prerelease: false
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ type: :development
64
+ name: thoughtbot-shoulda
65
+ prerelease: false
66
+ version_requirements: *id003
67
+ - !ruby/object:Gem::Dependency
68
+ requirement: &id004 !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 43
74
+ segments:
75
+ - 0
76
+ - 9
77
+ - 8
78
+ version: 0.9.8
79
+ type: :development
80
+ name: mocha
81
+ prerelease: false
82
+ version_requirements: *id004
83
+ description: resque-throttle is an extension to the resque queue system that restricts the frequency in which certain jobs are run.
84
+ email: francis.hwang@profitably.com
85
+ executables: []
86
+
87
+ extensions: []
88
+
89
+ extra_rdoc_files:
90
+ - LICENSE
91
+ - README.markdown
92
+ files:
93
+ - .document
94
+ - Gemfile
95
+ - Gemfile.lock
96
+ - LICENSE
97
+ - README.markdown
98
+ - Rakefile
99
+ - VERSION
100
+ - lib/resque-throttle.rb
101
+ - lib/resque/resque.rb
102
+ - lib/resque/throttle.rb
103
+ - lib/resque/throttled_job.rb
104
+ - resque-throttle.gemspec
105
+ - test/resque/resque_test.rb
106
+ - test/resque/throttled_job_test.rb
107
+ - test/test_helper.rb
108
+ has_rdoc: true
109
+ homepage: http://github.com/fhwang/resque-throttle
110
+ licenses:
111
+ - MIT
112
+ post_install_message:
113
+ rdoc_options: []
114
+
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ hash: 3
123
+ segments:
124
+ - 0
125
+ version: "0"
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ hash: 3
132
+ segments:
133
+ - 0
134
+ version: "0"
135
+ requirements: []
136
+
137
+ rubyforge_project:
138
+ rubygems_version: 1.3.7
139
+ signing_key:
140
+ specification_version: 3
141
+ summary: resque-throttle is an extension to the resque queue system that restricts the frequency in which certain jobs are run.
142
+ test_files:
143
+ - test/resque/resque_test.rb
144
+ - test/resque/throttled_job_test.rb
145
+ - test/test_helper.rb