resque-restriction 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/LICENSE +20 -0
- data/README.markdown +49 -0
- data/Rakefile +36 -0
- data/VERSION +1 -0
- data/init.rb +1 -0
- data/lib/resque-restriction/job.rb +15 -0
- data/lib/resque-restriction/restriction_job.rb +86 -0
- data/lib/resque-restriction.rb +3 -0
- data/rails/init.rb +1 -0
- data/spec/redis-test.conf +132 -0
- data/spec/resque-restriction/job_spec.rb +17 -0
- data/spec/resque-restriction/restriction_job_spec.rb +83 -0
- data/spec/spec.opts +8 -0
- data/spec/spec_helper.rb +86 -0
- metadata +81 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
spec/dump.rdb
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Richard Huang (flyerhzm@gmail.com)
|
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.
|
data/README.markdown
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
resque-restriction
|
2
|
+
===============
|
3
|
+
|
4
|
+
Resque Restriction is a plugin for the [Resque][0] queueing system (http://github.com/defunkt/resque). It adds two functions:
|
5
|
+
|
6
|
+
1. it will limit the execution number of certain jobs in a period time. For example, it can limit a certain job can be executed 1000 times per day, 100 time per hour and 30 times per 300 seconds.
|
7
|
+
|
8
|
+
2. it will execute the exceeded jobs at the next period. For example, you restrict the email sending jobs to run 1000 times per day. If your system generates 1010 email sending jobs, only 1000 email sending jobs can be executed today, and the other 10 email sending jobs will be executed tomorrow.
|
9
|
+
|
10
|
+
Resque Restriction requires Resque 1.7.0.
|
11
|
+
|
12
|
+
To use
|
13
|
+
------
|
14
|
+
|
15
|
+
The job you wish to restrict should inherit from Resque::Plugins::RestrictionJob class and add restrict definition. Example:
|
16
|
+
|
17
|
+
class MyRestrictionJob < Resque::Plugins::RestrictionJob
|
18
|
+
restrict :per_day => 1000, :per_hour => 1000, :per_300 => 30
|
19
|
+
|
20
|
+
#rest of your class here
|
21
|
+
end
|
22
|
+
|
23
|
+
MyRestrictionJob can be run 1000 times per day, 100 times per hour and 30 times per 300 seconds.
|
24
|
+
|
25
|
+
The argument of restrict method is a hash, the key of the hash is a period time, including :per_minute, :per_hour, :per_day, :per_week, :per_month, :per_year, and you can also define any period like :per_300 means per 300 seconds. the value of the hash is the job execution limit number in a period.
|
26
|
+
|
27
|
+
Contributing
|
28
|
+
------------
|
29
|
+
|
30
|
+
Once you've made your commits:
|
31
|
+
|
32
|
+
1. [Fork][1] Resque Restriction
|
33
|
+
2. Create a topic branch - `git checkout -b my_branch`
|
34
|
+
3. Push to your branch - `git push origin my_branch`
|
35
|
+
4. Create an [Issue][2] with a link to your branch
|
36
|
+
5. That's it!
|
37
|
+
|
38
|
+
Author
|
39
|
+
------
|
40
|
+
Richard Huang :: flyerhzm@gmail.com :: @flyerhzm
|
41
|
+
|
42
|
+
Copyright
|
43
|
+
---------
|
44
|
+
Copyright (c) 2010 Richard Huang. See LICENSE for details.
|
45
|
+
|
46
|
+
[0]: http://github.com/defunkt/resque
|
47
|
+
[1]: http://help.github.com/forking/
|
48
|
+
[2]: http://github.com/flyerhzm/resque-restriction/issues
|
49
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'spec/rake/spectask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :spec
|
7
|
+
|
8
|
+
desc 'Generate documentation for the resque-restriction plugin.'
|
9
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'resque_restriction'
|
12
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
13
|
+
rdoc.rdoc_files.include('README*')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
desc "Run all specs in spec directory"
|
18
|
+
Spec::Rake::SpecTask.new(:spec) do |t|
|
19
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
20
|
+
end
|
21
|
+
|
22
|
+
begin
|
23
|
+
require 'jeweler'
|
24
|
+
Jeweler::Tasks.new do |gemspec|
|
25
|
+
gemspec.name = "resque-restriction"
|
26
|
+
gemspec.summary = "resque-restriction is an extension to resque queue system that restricts the execution number of certain jobs in a period time."
|
27
|
+
gemspec.description = "resque-restriction is an extension to resque queue system that restricts the execution number of certain jobs in a period time, the exceeded jobs will be executed at the next period."
|
28
|
+
gemspec.email = "flyerhzm@gmail.com"
|
29
|
+
gemspec.homepage = "http://github.com/flyerhzm/resque-restriction"
|
30
|
+
gemspec.authors = ["Richard Huang"]
|
31
|
+
gemspec.add_dependency "resque", ">=1.7.0"
|
32
|
+
end
|
33
|
+
Jeweler::GemcutterTasks.new
|
34
|
+
rescue
|
35
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
36
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__) + '/rails/init')
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Resque
|
2
|
+
class Job
|
3
|
+
class <<self
|
4
|
+
alias_method :origin_reserve, :reserve
|
5
|
+
|
6
|
+
def reserve(queue)
|
7
|
+
if queue == 'restriction' && payload = Resque.peek(queue)
|
8
|
+
constantize(payload['class']).repush
|
9
|
+
return
|
10
|
+
end
|
11
|
+
origin_reserve(queue)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
module Resque
|
2
|
+
module Plugins
|
3
|
+
class RestrictionJob
|
4
|
+
SECONDS = {
|
5
|
+
:per_minute => 60,
|
6
|
+
:per_hour => 60*60,
|
7
|
+
:per_day => 24*60*60,
|
8
|
+
:per_week => 7*24*60*60,
|
9
|
+
:per_month => 31*24*60*60,
|
10
|
+
:per_year => 366*24*60*60
|
11
|
+
}
|
12
|
+
|
13
|
+
class <<self
|
14
|
+
def settings
|
15
|
+
@options ||= {}
|
16
|
+
end
|
17
|
+
|
18
|
+
def restrict(options={})
|
19
|
+
settings.merge!(options)
|
20
|
+
end
|
21
|
+
|
22
|
+
def before_perform_restriction(*args)
|
23
|
+
settings.each do |period, number|
|
24
|
+
key = redis_key(period)
|
25
|
+
value = get_restrict(key)
|
26
|
+
|
27
|
+
if value.nil? or value == ""
|
28
|
+
set_restrict(key, seconds(period), number)
|
29
|
+
elsif value.to_i <= 0
|
30
|
+
Resque.push "restriction", :class => to_s, :args => args
|
31
|
+
raise Resque::Job::DontPerform
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def after_perform_restriction(*args)
|
37
|
+
settings.each do |period, number|
|
38
|
+
key = redis_key(period)
|
39
|
+
Resque.redis.decrby(key, 1)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def redis_key(period)
|
44
|
+
period_str = case period
|
45
|
+
when :per_minute, :per_hour, :per_day, :per_week then (Time.now.to_i / SECONDS[period]).to_s
|
46
|
+
when :per_month then Date.today.strftime("%Y-%m")
|
47
|
+
when :per_year then Date.today.year.to_s
|
48
|
+
else period.to_s =~ /^per_(\d+)$/ and (Time.now.to_i / $1.to_i).to_s end
|
49
|
+
[self.to_s, period_str].compact.join(":")
|
50
|
+
end
|
51
|
+
|
52
|
+
def seconds(period)
|
53
|
+
if SECONDS.keys.include? period
|
54
|
+
SECONDS[period]
|
55
|
+
else
|
56
|
+
period.to_s =~ /^per_(\d+)$/ and $1
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def repush
|
61
|
+
settings.each do |period, number|
|
62
|
+
key = redis_key(period)
|
63
|
+
value = get_restrict(key)
|
64
|
+
queue_name = Resque.queue_from_class(self)
|
65
|
+
if value.nil? or value == ""
|
66
|
+
Resque.redis.rpoplpush('queue:restriction', "queue:#{queue_name}")
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
# after operation incrby - expire, then decrby will reset the value to 0 first
|
73
|
+
# use operation set - expire - incrby instead
|
74
|
+
def set_restrict(key, seconds, number)
|
75
|
+
Resque.redis.set(key, '')
|
76
|
+
Resque.redis.expire(key, seconds)
|
77
|
+
Resque.redis.incrby(key, number)
|
78
|
+
end
|
79
|
+
|
80
|
+
def get_restrict(key)
|
81
|
+
Resque.redis.get(key)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__) + '/../lib/resque-restriction')
|
@@ -0,0 +1,132 @@
|
|
1
|
+
# Redis configuration file example
|
2
|
+
|
3
|
+
# By default Redis does not run as a daemon. Use 'yes' if you need it.
|
4
|
+
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
|
5
|
+
daemonize yes
|
6
|
+
|
7
|
+
# When run as a daemon, Redis write a pid file in /var/run/redis.pid by default.
|
8
|
+
# You can specify a custom pid file location here.
|
9
|
+
pidfile ./spec/redis-test.pid
|
10
|
+
|
11
|
+
# Accept connections on the specified port, default is 6379
|
12
|
+
port 9736
|
13
|
+
|
14
|
+
# If you want you can bind a single interface, if the bind option is not
|
15
|
+
# specified all the interfaces will listen for connections.
|
16
|
+
#
|
17
|
+
# bind 127.0.0.1
|
18
|
+
|
19
|
+
# Close the connection after a client is idle for N seconds (0 to disable)
|
20
|
+
timeout 300
|
21
|
+
|
22
|
+
# Save the DB on disk:
|
23
|
+
#
|
24
|
+
# save <seconds> <changes>
|
25
|
+
#
|
26
|
+
# Will save the DB if both the given number of seconds and the given
|
27
|
+
# number of write operations against the DB occurred.
|
28
|
+
#
|
29
|
+
# In the example below the behaviour will be to save:
|
30
|
+
# after 900 sec (15 min) if at least 1 key changed
|
31
|
+
# after 300 sec (5 min) if at least 10 keys changed
|
32
|
+
# after 60 sec if at least 10000 keys changed
|
33
|
+
save 900 1
|
34
|
+
save 300 10
|
35
|
+
save 60 10000
|
36
|
+
|
37
|
+
# The filename where to dump the DB
|
38
|
+
dbfilename dump.rdb
|
39
|
+
|
40
|
+
# For default save/load DB in/from the working directory
|
41
|
+
# Note that you must specify a directory not a file name.
|
42
|
+
dir ./spec/
|
43
|
+
|
44
|
+
# Set server verbosity to 'debug'
|
45
|
+
# it can be one of:
|
46
|
+
# debug (a lot of information, useful for development/testing)
|
47
|
+
# notice (moderately verbose, what you want in production probably)
|
48
|
+
# warning (only very important / critical messages are logged)
|
49
|
+
loglevel debug
|
50
|
+
|
51
|
+
# Specify the log file name. Also 'stdout' can be used to force
|
52
|
+
# the demon to log on the standard output. Note that if you use standard
|
53
|
+
# output for logging but daemonize, logs will be sent to /dev/null
|
54
|
+
logfile stdout
|
55
|
+
|
56
|
+
# Set the number of databases. The default database is DB 0, you can select
|
57
|
+
# a different one on a per-connection basis using SELECT <dbid> where
|
58
|
+
# dbid is a number between 0 and 'databases'-1
|
59
|
+
databases 16
|
60
|
+
|
61
|
+
################################# REPLICATION #################################
|
62
|
+
|
63
|
+
# Master-Slave replication. Use slaveof to make a Redis instance a copy of
|
64
|
+
# another Redis server. Note that the configuration is local to the slave
|
65
|
+
# so for example it is possible to configure the slave to save the DB with a
|
66
|
+
# different interval, or to listen to another port, and so on.
|
67
|
+
|
68
|
+
# slaveof <masterip> <masterport>
|
69
|
+
|
70
|
+
################################## SECURITY ###################################
|
71
|
+
|
72
|
+
# Require clients to issue AUTH <PASSWORD> before processing any other
|
73
|
+
# commands. This might be useful in environments in which you do not trust
|
74
|
+
# others with access to the host running redis-server.
|
75
|
+
#
|
76
|
+
# This should stay commented out for backward compatibility and because most
|
77
|
+
# people do not need auth (e.g. they run their own servers).
|
78
|
+
|
79
|
+
# requirepass foobared
|
80
|
+
|
81
|
+
################################### LIMITS ####################################
|
82
|
+
|
83
|
+
# Set the max number of connected clients at the same time. By default there
|
84
|
+
# is no limit, and it's up to the number of file descriptors the Redis process
|
85
|
+
# is able to open. The special value '0' means no limts.
|
86
|
+
# Once the limit is reached Redis will close all the new connections sending
|
87
|
+
# an error 'max number of clients reached'.
|
88
|
+
|
89
|
+
# maxclients 128
|
90
|
+
|
91
|
+
# Don't use more memory than the specified amount of bytes.
|
92
|
+
# When the memory limit is reached Redis will try to remove keys with an
|
93
|
+
# EXPIRE set. It will try to start freeing keys that are going to expire
|
94
|
+
# in little time and preserve keys with a longer time to live.
|
95
|
+
# Redis will also try to remove objects from free lists if possible.
|
96
|
+
#
|
97
|
+
# If all this fails, Redis will start to reply with errors to commands
|
98
|
+
# that will use more memory, like SET, LPUSH, and so on, and will continue
|
99
|
+
# to reply to most read-only commands like GET.
|
100
|
+
#
|
101
|
+
# WARNING: maxmemory can be a good idea mainly if you want to use Redis as a
|
102
|
+
# 'state' server or cache, not as a real DB. When Redis is used as a real
|
103
|
+
# database the memory usage will grow over the weeks, it will be obvious if
|
104
|
+
# it is going to use too much memory in the long run, and you'll have the time
|
105
|
+
# to upgrade. With maxmemory after the limit is reached you'll start to get
|
106
|
+
# errors for write operations, and this may even lead to DB inconsistency.
|
107
|
+
|
108
|
+
# maxmemory <bytes>
|
109
|
+
|
110
|
+
############################### ADVANCED CONFIG ###############################
|
111
|
+
|
112
|
+
# Glue small output buffers together in order to send small replies in a
|
113
|
+
# single TCP packet. Uses a bit more CPU but most of the times it is a win
|
114
|
+
# in terms of number of queries per second. Use 'yes' if unsure.
|
115
|
+
glueoutputbuf yes
|
116
|
+
|
117
|
+
# Use object sharing. Can save a lot of memory if you have many common
|
118
|
+
# string in your dataset, but performs lookups against the shared objects
|
119
|
+
# pool so it uses more CPU and can be a bit slower. Usually it's a good
|
120
|
+
# idea.
|
121
|
+
#
|
122
|
+
# When object sharing is enabled (shareobjects yes) you can use
|
123
|
+
# shareobjectspoolsize to control the size of the pool used in order to try
|
124
|
+
# object sharing. A bigger pool size will lead to better sharing capabilities.
|
125
|
+
# In general you want this value to be at least the double of the number of
|
126
|
+
# very common strings you have in your dataset.
|
127
|
+
#
|
128
|
+
# WARNING: object sharing is experimental, don't enable this feature
|
129
|
+
# in production before of Redis 1.0-stable. Still please try this feature in
|
130
|
+
# your development environment so that we can test it better.
|
131
|
+
#shareobjects no
|
132
|
+
#shareobjectspoolsize 1024
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe Resque::Job do
|
4
|
+
it "should repush restrictioin queue when reserve" do
|
5
|
+
Resque.redis.flushall
|
6
|
+
Resque.push('restriction', :class => 'OneHourRestrictionJob', :args => 'any args')
|
7
|
+
Resque::Job.reserve('restriction').should be_nil
|
8
|
+
Resque::Job.reserve('normal').should == Resque::Job.new('normal', {'class' => 'OneHourRestrictionJob', 'args' => 'any args'})
|
9
|
+
Resque::Job.reserve('normal').should be_nil
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should not repush when reserve normal queue" do
|
13
|
+
Resque.push('normal', :class => 'OneHourRestrictionJob', :args => 'any args')
|
14
|
+
Resque::Job.reserve('normal').should == Resque::Job.new('normal', {'class' => 'OneHourRestrictionJob', 'args' => 'any args'})
|
15
|
+
Resque::Job.reserve('normal').should be_nil
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe Resque::Plugins::RestrictionJob do
|
4
|
+
it "should follow the convention" do
|
5
|
+
Resque::Plugin.lint(Resque::Plugins::RestrictionJob)
|
6
|
+
end
|
7
|
+
|
8
|
+
context "redis_key" do
|
9
|
+
it "should get redis_key with different period" do
|
10
|
+
Resque::Plugins::RestrictionJob.redis_key(:per_minute).should == "Resque::Plugins::RestrictionJob:#{Time.now.to_i / 60}"
|
11
|
+
Resque::Plugins::RestrictionJob.redis_key(:per_hour).should == "Resque::Plugins::RestrictionJob:#{Time.now.to_i / (60*60)}"
|
12
|
+
Resque::Plugins::RestrictionJob.redis_key(:per_day).should == "Resque::Plugins::RestrictionJob:#{Time.now.to_i / (24*60*60)}"
|
13
|
+
Resque::Plugins::RestrictionJob.redis_key(:per_month).should == "Resque::Plugins::RestrictionJob:#{Date.today.strftime("%Y-%m")}"
|
14
|
+
Resque::Plugins::RestrictionJob.redis_key(:per_year).should == "Resque::Plugins::RestrictionJob:#{Date.today.year}"
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should accept customization" do
|
18
|
+
Resque::Plugins::RestrictionJob.redis_key(:per_1800).should == "Resque::Plugins::RestrictionJob:#{Time.now.to_i / 1800}"
|
19
|
+
Resque::Plugins::RestrictionJob.redis_key(:per_7200).should == "Resque::Plugins::RestrictionJob:#{Time.now.to_i / 7200}"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context "settings" do
|
24
|
+
it "get correct number to restriction jobs" do
|
25
|
+
OneDayRestrictionJob.settings.should == {:per_day => 100}
|
26
|
+
OneHourRestrictionJob.settings.should == {:per_hour => 10}
|
27
|
+
MultipleRestrictionJob.settings.should == {:per_hour => 10, :per_300 => 2}
|
28
|
+
MultiCallRestrictionJob.settings.should == {:per_hour => 10, :per_300 => 2}
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context "resque" do
|
33
|
+
include PerformJob
|
34
|
+
|
35
|
+
before(:each) do
|
36
|
+
Resque.redis.flushall
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should set execution number and decrement it when one job first executed" do
|
40
|
+
result = perform_job(OneHourRestrictionJob, "any args")
|
41
|
+
result.should be_true
|
42
|
+
Resque.redis.get(OneHourRestrictionJob.redis_key(:per_hour)).should == "9"
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should decrement execution number when one job executed" do
|
46
|
+
Resque.redis.set(OneHourRestrictionJob.redis_key(:per_hour), 6)
|
47
|
+
result = perform_job(OneHourRestrictionJob, "any args")
|
48
|
+
result.should be_true
|
49
|
+
Resque.redis.get(OneHourRestrictionJob.redis_key(:per_hour)).should == "5"
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should put the job into restriction queue when execution count <= 0" do
|
53
|
+
Resque.redis.set(OneHourRestrictionJob.redis_key(:per_hour), 0)
|
54
|
+
result = perform_job(OneHourRestrictionJob, "any args")
|
55
|
+
result.should_not be_true
|
56
|
+
Resque.redis.get(OneHourRestrictionJob.redis_key(:per_hour)).should == "0"
|
57
|
+
Resque.redis.lrange("queue:restriction", 0, -1).should == [Resque.encode(:class => "OneHourRestrictionJob", :args => ["any args"])]
|
58
|
+
end
|
59
|
+
|
60
|
+
context "multiple restrict" do
|
61
|
+
it "should restrict per_minute" do
|
62
|
+
result = perform_job(MultipleRestrictionJob, "any args")
|
63
|
+
Resque.redis.get(MultipleRestrictionJob.redis_key(:per_hour)).should == "9"
|
64
|
+
Resque.redis.get(MultipleRestrictionJob.redis_key(:per_300)).should == "1"
|
65
|
+
result = perform_job(MultipleRestrictionJob, "any args")
|
66
|
+
result = perform_job(MultipleRestrictionJob, "any args")
|
67
|
+
Resque.redis.get(MultipleRestrictionJob.redis_key(:per_hour)).should == "8"
|
68
|
+
Resque.redis.get(MultipleRestrictionJob.redis_key(:per_300)).should == "0"
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should restrict per_hour" do
|
72
|
+
Resque.redis.set(MultipleRestrictionJob.redis_key(:per_hour), 1)
|
73
|
+
Resque.redis.set(MultipleRestrictionJob.redis_key(:per_300), 2)
|
74
|
+
result = perform_job(MultipleRestrictionJob, "any args")
|
75
|
+
Resque.redis.get(MultipleRestrictionJob.redis_key(:per_hour)).should == "0"
|
76
|
+
Resque.redis.get(MultipleRestrictionJob.redis_key(:per_300)).should == "1"
|
77
|
+
result = perform_job(MultipleRestrictionJob, "any args")
|
78
|
+
Resque.redis.get(MultipleRestrictionJob.redis_key(:per_hour)).should == "0"
|
79
|
+
Resque.redis.get(MultipleRestrictionJob.redis_key(:per_300)).should == "1"
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'spec/autorun'
|
3
|
+
require 'mocha'
|
4
|
+
|
5
|
+
dir = File.dirname(__FILE__)
|
6
|
+
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../lib"))
|
7
|
+
require 'resque-restriction'
|
8
|
+
|
9
|
+
#
|
10
|
+
# make sure we can run redis
|
11
|
+
#
|
12
|
+
|
13
|
+
if !system("which redis-server")
|
14
|
+
puts '', "** can't find `redis-server` in your path"
|
15
|
+
puts "** try running `sudo rake install`"
|
16
|
+
abort ''
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
#
|
21
|
+
# start our own redis when the tests start,
|
22
|
+
# kill it when they end
|
23
|
+
#
|
24
|
+
|
25
|
+
at_exit do
|
26
|
+
next if $!
|
27
|
+
|
28
|
+
exit_code = Spec::Runner.run
|
29
|
+
|
30
|
+
pid = `ps -e -o pid,command | grep [r]edis-test`.split(" ")[0]
|
31
|
+
puts "Killing test redis server..."
|
32
|
+
`rm -f #{dir}/dump.rdb`
|
33
|
+
Process.kill("KILL", pid.to_i)
|
34
|
+
exit exit_code
|
35
|
+
end
|
36
|
+
|
37
|
+
puts "Starting redis for testing at localhost:9736..."
|
38
|
+
`redis-server #{dir}/redis-test.conf`
|
39
|
+
Resque.redis = 'localhost:9736'
|
40
|
+
|
41
|
+
##
|
42
|
+
# Helper to perform job classes
|
43
|
+
#
|
44
|
+
module PerformJob
|
45
|
+
def perform_job(klass, *args)
|
46
|
+
resque_job = Resque::Job.new(:testqueue, 'class' => klass, 'args' => args)
|
47
|
+
resque_job.perform
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
class OneDayRestrictionJob < Resque::Plugins::RestrictionJob
|
52
|
+
restrict :per_day => 100
|
53
|
+
|
54
|
+
@queue = 'normal'
|
55
|
+
|
56
|
+
def self.perform(args)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
class OneHourRestrictionJob < Resque::Plugins::RestrictionJob
|
61
|
+
restrict :per_hour => 10
|
62
|
+
|
63
|
+
@queue = 'normal'
|
64
|
+
|
65
|
+
def self.perform(args)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
class MultipleRestrictionJob < Resque::Plugins::RestrictionJob
|
70
|
+
restrict :per_hour => 10, :per_300 => 2
|
71
|
+
|
72
|
+
@queue = 'normal'
|
73
|
+
|
74
|
+
def self.perform(args)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
class MultiCallRestrictionJob < Resque::Plugins::RestrictionJob
|
79
|
+
restrict :per_hour => 10
|
80
|
+
restrict :per_300 => 2
|
81
|
+
|
82
|
+
@queue = 'normal'
|
83
|
+
|
84
|
+
def self.perform(args)
|
85
|
+
end
|
86
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: resque-restriction
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Richard Huang
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-04-30 00:00:00 -06:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: resque
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.7.0
|
24
|
+
version:
|
25
|
+
description: resque-restriction is an extension to resque queue system that restricts the execution number of certain jobs in a period time, the exceeded jobs will be executed at the next period.
|
26
|
+
email: flyerhzm@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.markdown
|
34
|
+
files:
|
35
|
+
- .gitignore
|
36
|
+
- LICENSE
|
37
|
+
- README.markdown
|
38
|
+
- Rakefile
|
39
|
+
- VERSION
|
40
|
+
- init.rb
|
41
|
+
- lib/resque-restriction.rb
|
42
|
+
- lib/resque-restriction/job.rb
|
43
|
+
- lib/resque-restriction/restriction_job.rb
|
44
|
+
- rails/init.rb
|
45
|
+
- spec/redis-test.conf
|
46
|
+
- spec/resque-restriction/job_spec.rb
|
47
|
+
- spec/resque-restriction/restriction_job_spec.rb
|
48
|
+
- spec/spec.opts
|
49
|
+
- spec/spec_helper.rb
|
50
|
+
has_rdoc: true
|
51
|
+
homepage: http://github.com/flyerhzm/resque-restriction
|
52
|
+
licenses: []
|
53
|
+
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options:
|
56
|
+
- --charset=UTF-8
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
version:
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: "0"
|
70
|
+
version:
|
71
|
+
requirements: []
|
72
|
+
|
73
|
+
rubyforge_project:
|
74
|
+
rubygems_version: 1.3.5
|
75
|
+
signing_key:
|
76
|
+
specification_version: 3
|
77
|
+
summary: resque-restriction is an extension to resque queue system that restricts the execution number of certain jobs in a period time.
|
78
|
+
test_files:
|
79
|
+
- spec/resque-restriction/job_spec.rb
|
80
|
+
- spec/resque-restriction/restriction_job_spec.rb
|
81
|
+
- spec/spec_helper.rb
|