-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 22f532b
Showing
10 changed files
with
229 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Copyright (c) 2010 Richard Huang (flyerhzm@gmail.com) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
resque-restriction | ||
=============== | ||
|
||
Resque Restriction is a plugin for the [Resque][0] queueing system | ||
(http://github.com/defunkt/resque). It adds a RestrictionJob class that will | ||
restrict the execution number of certain jobs in a period time. | ||
|
||
To use | ||
------ | ||
|
||
|
||
Contributing | ||
------------ | ||
|
||
Once you've made your commits: | ||
|
||
1. [Fork][1] Resque Restriction | ||
2. Create a topic branch - `git checkout -b my_branch` | ||
3. Push to your branch - `git push origin my_branch` | ||
4. Create an [Issue][2] with a link to your branch | ||
5. That's it! | ||
|
||
Author | ||
------ | ||
Richard Huang :: flyerhzm@gmail.com :: @flyerhzm | ||
|
||
Copyright | ||
--------- | ||
Copyright (c) 2010 Richard Huang. See LICENSE for details. | ||
|
||
[0]: http://github.com/defunkt/resque | ||
[1]: http://help.github.com/forking/ | ||
[2]: http://github.com/flyerhm/resque-restriction/issues | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require 'rake' | ||
require 'spec/rake/spectask' | ||
require 'rake/rdoctask' | ||
|
||
desc 'Default: run unit tests.' | ||
task :default => :spec | ||
|
||
desc 'Generate documentation for the resque-restriction plugin.' | ||
Rake::RDocTask.new(:rdoc) do |rdoc| | ||
rdoc.rdoc_dir = 'rdoc' | ||
rdoc.title = 'resque_restriction' | ||
rdoc.options << '--line-numbers' << '--inline-source' | ||
rdoc.rdoc_files.include('README*') | ||
rdoc.rdoc_files.include('lib/**/*.rb') | ||
end | ||
|
||
desc "Run all specs in spec directory" | ||
Spec::Rake::SpecTask.new(:spec) do |t| | ||
t.spec_files = FileList['spec/**/*_spec.rb'] | ||
end | ||
|
||
begin | ||
require 'jeweler' | ||
Jeweler::Tasks.new do |gemspec| | ||
gemspec.name = "resque-restriction" | ||
gemspec.summary = "resque-restriction is an extension to resque queue system that restricts the execution number of certain jobs in a period time." | ||
gemspec.description = "resque-restriction is an extension to resque queue system that restricts the execution number of certain jobs in a period time." | ||
gemspec.email = "flyerhzm@gmail.com" | ||
gemspec.homepage = "http://github.com/flyerhzm/resque-restriction" | ||
gemspec.authors = ["Richard Huang"] | ||
gemspec.add_dependency "resque", ">=1.7.0" | ||
end | ||
Jeweler::GemcutterTasks.new | ||
rescue | ||
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require File.join(File.dirname(__FILE__) + '/rails/init') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require 'resque' | ||
require 'resque/restriction_job' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
module Resque | ||
module Plugins | ||
class RestrictionJob | ||
ONE_DAY = 60*60*24 | ||
|
||
RESCRICTION_SETTINGS = { | ||
:number => 1000, | ||
:period => ONE_DAY | ||
} | ||
|
||
def self.settings | ||
@settings ||= RESCRICTION_SETTINGS.dup | ||
end | ||
|
||
def self.restrict(options = {}) | ||
settings.merge!(options) | ||
end | ||
|
||
def self.key(*args) | ||
[self.to_s, Date.today].compact.join(":") | ||
end | ||
|
||
def self.number | ||
settings[:number] | ||
end | ||
|
||
def self.period | ||
settings[:period] | ||
end | ||
|
||
def self.before_perform_restriction(*args) | ||
if should_restrict?(*args) | ||
raise Resque::Job::DontPerform | ||
end | ||
end | ||
|
||
def self.after_perform_restriction(*args) | ||
Resque.redis.decrby(self.key(*args), 1) | ||
end | ||
|
||
|
||
private | ||
def self.should_restrict?(*args) | ||
key = self.key(*args) | ||
num = Resque.redis.get(key) | ||
|
||
if num.nil? | ||
# after operation incrby - expire, then decrby will reset the value to 0 first | ||
# use operation set - expire - incrby instead | ||
Resque.redis.set(key, '') | ||
Resque.redis.expire(key, period) | ||
Resque.redis.incrby(key, number) | ||
Resque.redis.rpoplpush('queue:restriction', "queue:#{queue}") | ||
return false | ||
elsif num.to_i > 0 | ||
return false | ||
else | ||
Resque.push "restriction", :class => self.to_s, :args => args | ||
return true | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require File.join(File.dirname(__FILE__) + '/../lib/resque-restriction') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require File.join(File.dirname(__FILE__) + '/../spec_helper') | ||
|
||
describe Resque::Plugins::RestrictionJob do | ||
it "should follow the convention" do | ||
Resque::Plugin.lint(Resque::Plugins::RestrictionJob) | ||
end | ||
|
||
context "settings" do | ||
it "get correct number to restriction jobs" do | ||
DefaultRestrictionJob.number.should == 1000 | ||
TenNumberRestrictionJob.number.should == 10 | ||
end | ||
|
||
it "get correct period to restriction jobs" do | ||
DefaultRestrictionJob.period.should == 60*60*24 | ||
OneHourRestrictionJob.period.should == 60*60 | ||
end | ||
end | ||
|
||
context "resque" do | ||
before(:each) do | ||
Resque.redis.flush_all | ||
@bogus_args = "bogus_args" | ||
end | ||
|
||
it "should set key for restriction number" do | ||
Resque.expects(:enqueue).returns(true) | ||
Resque.enqueue(DefaultRestrictionJob, @bogus_args) | ||
Resque.redis.keys('*').include?("resque:DefaultRestrictionJob:#{Date.today}") | ||
Resque.redis.get("resque:DefaultRestrictionJob:#{Date.today}").should == 1000 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--colour | ||
--format | ||
specdoc | ||
--reverse | ||
--timeout | ||
20 | ||
--loadby | ||
mtime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require 'rubygems' | ||
require 'spec/autorun' | ||
require 'mocha' | ||
|
||
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../lib")) | ||
require 'resque-restriction' | ||
|
||
|
||
class DefaultRestrictionJob < Resque::Plugins::RestrictionJob | ||
@queue = :some_queue | ||
|
||
def self.perform(some_id, some_other_thing) | ||
end | ||
end | ||
|
||
class OneHourRestrictionJob < Resque::Plugins::RestrictionJob | ||
@queue = :some_queue | ||
restrict :period => 3600 | ||
|
||
def self.perform(some_id, some_other_thing) | ||
end | ||
end | ||
|
||
class TenNumberRestrictionJob < Resque::Plugins::RestrictionJob | ||
@queue = :some_queue | ||
restrict :number => 10 | ||
|
||
def self.perform(some_id, some_other_thing) | ||
end | ||
end |