resque-restriction 0.4.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +7 -0
- data/.travis.yml +5 -0
- data/CHANGELOG.md +16 -1
- data/README.md +112 -0
- data/Rakefile +2 -18
- data/lib/{resque-restriction → resque/plugins}/job.rb +3 -3
- data/lib/{resque-restriction/restriction_job.rb → resque/plugins/restriction.rb} +17 -31
- data/lib/resque/restriction.rb +4 -0
- data/lib/resque/restriction/config.rb +49 -0
- data/lib/resque/restriction/version.rb +5 -0
- data/resque-restriction.gemspec +21 -54
- metadata +50 -28
- data/Gemfile.lock +0 -116
- data/README.markdown +0 -83
- data/VERSION +0 -1
- data/init.rb +0 -1
- data/lib/resque-restriction.rb +0 -3
- data/rails/init.rb +0 -1
- data/spec/redis-test.conf +0 -132
- data/spec/resque-restriction/job_spec.rb +0 -39
- data/spec/resque-restriction/restriction_job_spec.rb +0 -222
- data/spec/spec_helper.rb +0 -107
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 681852b48ac3fb45d6a5d1806af5617d6c4c566ee6483c777d1e7d1e837443ba
|
4
|
+
data.tar.gz: '0706749a9fc0beb80cb532a84e5988157055759b0151e48b1a08d2fa8c515b21'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2015e5527ba932161e0cdfbf20efb102214403232f5595f496571df3b8964d5157bef27fa21e3feb2363f89fbf502576c8b13596b9322ef61873861669c71a32
|
7
|
+
data.tar.gz: b7fce8d53acc85f50d6c21626b03e7aed86a5f04d0cd01f3a40301ff2534ae149bf3caf7cd53a5e093765a25d233c06ff6819f733f7fcc502a7586d1ac940ec9
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
CHANGED
@@ -1,7 +1,22 @@
|
|
1
1
|
### Unreleased
|
2
2
|
|
3
|
+
# 0.7.0 (2021-06-06)
|
4
|
+
|
5
|
+
* Support `per_second`
|
6
|
+
* Reduce Redis thrashing on large restriction queues
|
7
|
+
|
8
|
+
# 0.6.0
|
9
|
+
|
10
|
+
* support customized period like `per_minute_and_user_id`
|
11
|
+
* prepend restriction to `cache_key`
|
12
|
+
|
13
|
+
# 0.5.0
|
14
|
+
|
15
|
+
* drop activejob support
|
16
|
+
* reorganize file structure
|
17
|
+
|
3
18
|
# 0.4.0
|
4
19
|
* release for Rails>=4.2 with ActiveJob
|
5
20
|
|
6
21
|
# 0.3.0
|
7
|
-
* release for Rails<4.2
|
22
|
+
* release for Rails<4.2
|
data/README.md
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
resque-restriction
|
2
|
+
===============
|
3
|
+
|
4
|
+
[![AwesomeCode Status for flyerhzm/resque-restriction](https://awesomecode.io/projects/ebc1a493-78f5-4d8a-9bb7-097fdaa657ec/status)](https://awesomecode.io/repos/flyerhzm/resque-restriction)
|
5
|
+
[![Build Status](https://secure.travis-ci.org/flyerhzm/resque-restriction.png)](http://travis-ci.org/flyerhzm/resque-restriction)
|
6
|
+
|
7
|
+
Resque Restriction is a plugin for the [Resque](https://github.com/resque/resque) queueing system. It adds two functions:
|
8
|
+
|
9
|
+
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.
|
10
|
+
|
11
|
+
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.
|
12
|
+
|
13
|
+
Resque Restriction requires Resque 1.7.0.
|
14
|
+
|
15
|
+
*Please make sure your workers are checking restriction_xxx queue.*.
|
16
|
+
e.g., if you add restriction plugin to high_priority queue, you need to
|
17
|
+
check restriction_high_priority queue.
|
18
|
+
|
19
|
+
Attention
|
20
|
+
---------
|
21
|
+
|
22
|
+
The `identifier` method is renamed to `restriction_identifier` to solve the confliction with resque-retry from version 0.3.0.
|
23
|
+
|
24
|
+
Install
|
25
|
+
-------
|
26
|
+
|
27
|
+
Add this line to your application's Gemfile:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
gem 'resque-restriction'
|
31
|
+
```
|
32
|
+
|
33
|
+
And then execute:
|
34
|
+
|
35
|
+
$ bundle
|
36
|
+
|
37
|
+
Or install it yourself as:
|
38
|
+
|
39
|
+
$ gem install resque-restriction
|
40
|
+
|
41
|
+
To use
|
42
|
+
------
|
43
|
+
|
44
|
+
It is especially useful when a system has an email invitation resque job, because sending emails too frequentyly will be treated as a spam. What you should do for the InvitationJob is to inherit it from Resque::Plugins::RestrictionJob class and add restrict definition. Example:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
class InvitationJob
|
48
|
+
extend Resque::Plugins::Restriction
|
49
|
+
|
50
|
+
restrict :per_day => 1000, :per_hour => 100, :per_300 => 30
|
51
|
+
|
52
|
+
# rest of your class here
|
53
|
+
end
|
54
|
+
```
|
55
|
+
|
56
|
+
That means the InvitationJob can not be executed more than 1000 times per day, 100 times per hour and 30 times per 300 seconds. All restrictions have to be met for the job to execute.
|
57
|
+
|
58
|
+
The argument of restrict method is a hash, the key of the hash is a period time, including :concurrent, :per_second, :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. The :concurrent option restricts the number of jobs that run simultaneously.
|
59
|
+
|
60
|
+
Advance
|
61
|
+
-------
|
62
|
+
|
63
|
+
### Custom Restrictions
|
64
|
+
|
65
|
+
You can also add customized restriction as you like. For example, we have a job to restrict the facebook post numbers 40 times per user per day, we can define as:
|
66
|
+
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
class GenerateFacebookShares
|
70
|
+
extend Resque::Plugins::Restriction
|
71
|
+
|
72
|
+
restrict :per_day_and_user_id => 40
|
73
|
+
|
74
|
+
# rest of your class here
|
75
|
+
def self.perform(options)
|
76
|
+
# options["user_id"] exists
|
77
|
+
end
|
78
|
+
end
|
79
|
+
```
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
class GenerateFacebookShares
|
83
|
+
extend Resque::Plugins::Restriction
|
84
|
+
|
85
|
+
restrict :per_day => 40
|
86
|
+
|
87
|
+
def self.restriction_identifier(options)
|
88
|
+
[self.to_s, options["user_id"]].join(":")
|
89
|
+
end
|
90
|
+
|
91
|
+
# rest of your class here
|
92
|
+
end
|
93
|
+
```
|
94
|
+
|
95
|
+
options["user_id"] returns the user's facebook uid, the key point is that the different restriction_identifiers can restrict different job execution numbers.
|
96
|
+
|
97
|
+
### Reducing Redis Thrashing
|
98
|
+
|
99
|
+
With large restriction queues (1,000+ jobs), Redis is hit with a large number of `Resque.pop` and re-enqueue calls which puts the job immediately back into the restriction queue. That process translates to extraneous Redis requests.
|
100
|
+
|
101
|
+
To avoid the extraneous Redis calls, configure `max_queue_peek`. An appropriate `max_queue_peek` value depends on your application, number of workers, job processing time, and peek jobs in the queue. By default `max_queue_peek` is disabled.
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
Resque::Restriction.configure do |config|
|
105
|
+
config.max_queue_peek = 100 # jobs
|
106
|
+
end
|
107
|
+
```
|
108
|
+
|
109
|
+
Contributing
|
110
|
+
------------
|
111
|
+
|
112
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/flyerhzm/resque-restriction.
|
data/Rakefile
CHANGED
@@ -1,22 +1,6 @@
|
|
1
|
-
require '
|
1
|
+
require 'bundler/gem_tasks'
|
2
2
|
require 'rspec/core/rake_task'
|
3
3
|
|
4
4
|
RSpec::Core::RakeTask.new(:spec)
|
5
5
|
|
6
|
-
task :
|
7
|
-
|
8
|
-
begin
|
9
|
-
require 'jeweler'
|
10
|
-
Jeweler::Tasks.new do |gemspec|
|
11
|
-
gemspec.name = "resque-restriction"
|
12
|
-
gemspec.summary = "resque-restriction is an extension to resque queue system that restricts the execution number of certain jobs in a period time."
|
13
|
-
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."
|
14
|
-
gemspec.email = "flyerhzm@gmail.com"
|
15
|
-
gemspec.homepage = "http://github.com/flyerhzm/resque-restriction"
|
16
|
-
gemspec.authors = ["Richard Huang"]
|
17
|
-
gemspec.add_dependency "resque", ">=1.7.0"
|
18
|
-
end
|
19
|
-
Jeweler::GemcutterTasks.new
|
20
|
-
rescue
|
21
|
-
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
22
|
-
end
|
6
|
+
task default: :spec
|
@@ -7,8 +7,8 @@ module Resque
|
|
7
7
|
if queue =~ /^#{Plugins::Restriction::RESTRICTION_QUEUE_PREFIX}/
|
8
8
|
# If processing the restriction queue, when poping and pushing to end,
|
9
9
|
# we can't tell when we reach the original one, so just walk the length
|
10
|
-
# of the queue so we don't run infinitely long
|
11
|
-
Resque.size(queue).times do
|
10
|
+
# of the queue or up to max_queue_peek so we don't run infinitely long
|
11
|
+
[Resque.size(queue), Restriction.config.max_queue_peek(queue)].compact.min.times do
|
12
12
|
# For the job at the head of the queue, repush to restricition queue
|
13
13
|
# if still restricted, otherwise we have a runnable job, so create it
|
14
14
|
# and return
|
@@ -19,7 +19,7 @@ module Resque
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
end
|
22
|
-
|
22
|
+
nil
|
23
23
|
else
|
24
24
|
# drop through to original Job::Reserve if not restriction queue
|
25
25
|
origin_reserve(queue)
|
@@ -1,10 +1,8 @@
|
|
1
|
-
require 'active_job'
|
2
|
-
|
3
1
|
module Resque
|
4
2
|
module Plugins
|
5
|
-
|
6
3
|
module Restriction
|
7
4
|
SECONDS = {
|
5
|
+
:per_second => 1,
|
8
6
|
:per_minute => 60,
|
9
7
|
:per_hour => 60*60,
|
10
8
|
:per_day => 24*60*60,
|
@@ -23,6 +21,8 @@ module Resque
|
|
23
21
|
end
|
24
22
|
|
25
23
|
def before_perform_restriction(*args)
|
24
|
+
return if Resque.inline?
|
25
|
+
|
26
26
|
keys_decremented = []
|
27
27
|
settings.each do |period, number|
|
28
28
|
key = redis_key(period, *args)
|
@@ -65,25 +65,32 @@ module Resque
|
|
65
65
|
end
|
66
66
|
|
67
67
|
def redis_key(period, *args)
|
68
|
-
|
68
|
+
period_key, custom_key = period.to_s.split('_and_')
|
69
|
+
period_str = case period_key.to_sym
|
69
70
|
when :concurrent then "*"
|
70
|
-
when :per_minute, :per_hour, :per_day, :per_week then (Time.now.to_i / SECONDS[
|
71
|
+
when :per_second, :per_minute, :per_hour, :per_day, :per_week then (Time.now.to_i / SECONDS[period_key.to_sym]).to_s
|
71
72
|
when :per_month then Date.today.strftime("%Y-%m")
|
72
73
|
when :per_year then Date.today.year.to_s
|
73
|
-
else
|
74
|
-
|
74
|
+
else period_key =~ /^per_(\d+)$/ and (Time.now.to_i / $1.to_i).to_s end
|
75
|
+
custom_value = (custom_key && args.first && args.first.is_a?(Hash)) ? args.first[custom_key] : nil
|
76
|
+
[RESTRICTION_QUEUE_PREFIX, self.restriction_identifier(*args), custom_value, period_str].compact.join(":")
|
75
77
|
end
|
76
78
|
|
77
79
|
def restriction_identifier(*args)
|
78
80
|
self.to_s
|
79
81
|
end
|
80
82
|
|
83
|
+
def restriction_queue_name
|
84
|
+
queue_name = Resque.queue_from_class(self)
|
85
|
+
"#{RESTRICTION_QUEUE_PREFIX}_#{queue_name}"
|
86
|
+
end
|
81
87
|
|
82
88
|
def seconds(period)
|
83
|
-
|
84
|
-
|
89
|
+
period_key, _ = period.to_s.split('_and_')
|
90
|
+
if SECONDS.keys.include?(period_key.to_sym)
|
91
|
+
SECONDS[period_key.to_sym]
|
85
92
|
else
|
86
|
-
|
93
|
+
period_key =~ /^per_(\d+)$/ and $1.to_i
|
87
94
|
end
|
88
95
|
end
|
89
96
|
|
@@ -111,26 +118,5 @@ module Resque
|
|
111
118
|
Resque.redis.expire(key, seconds(period)) unless period == :concurrent
|
112
119
|
end
|
113
120
|
end
|
114
|
-
|
115
|
-
class RestrictionJob < ActiveJob::Base
|
116
|
-
extend Restriction
|
117
|
-
|
118
|
-
before_perform do |job|
|
119
|
-
self.class.before_perform_restriction(*job.arguments)
|
120
|
-
end
|
121
|
-
|
122
|
-
after_perform do |job|
|
123
|
-
self.class.after_perform_restriction(*job.arguments)
|
124
|
-
end
|
125
|
-
|
126
|
-
rescue_from(StandardError) do |err|
|
127
|
-
self.class.on_failure_restriction(err, *self.arguments)
|
128
|
-
end
|
129
|
-
|
130
|
-
def self.restriction_queue_name
|
131
|
-
queue_name = self.new.queue_name
|
132
|
-
"#{Resque::Plugins::Restriction::RESTRICTION_QUEUE_PREFIX}_#{queue_name}"
|
133
|
-
end
|
134
|
-
end
|
135
121
|
end
|
136
122
|
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module Resque
|
2
|
+
module Restriction
|
3
|
+
def self.configure
|
4
|
+
yield config
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.config
|
8
|
+
@config ||= Config.new
|
9
|
+
end
|
10
|
+
|
11
|
+
class Config
|
12
|
+
def initialize
|
13
|
+
@max_queue_peek = nil
|
14
|
+
end
|
15
|
+
|
16
|
+
def max_queue_peek=(value_or_callable)
|
17
|
+
if value_or_callable.respond_to?(:call)
|
18
|
+
@max_queue_peek = value_or_callable
|
19
|
+
elsif value_or_callable.nil?
|
20
|
+
@max_queue_peek = nil
|
21
|
+
else
|
22
|
+
@max_queue_peek = validated_max_queue_peek(value_or_callable)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def max_queue_peek(queue)
|
27
|
+
@max_queue_peek.respond_to?(:call) ? @max_queue_peek.call(queue) : @max_queue_peek
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def validated_max_queue_peek(value)
|
33
|
+
peek = nil
|
34
|
+
|
35
|
+
begin
|
36
|
+
peek = Integer(value)
|
37
|
+
|
38
|
+
if peek <= 0
|
39
|
+
raise ArgumentError
|
40
|
+
end
|
41
|
+
rescue ArgumentError
|
42
|
+
raise ArgumentError, "max_queue_peek should be either nil or an Integer greater than 0 but #{value.inspect} was provided"
|
43
|
+
end
|
44
|
+
|
45
|
+
peek
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/resque-restriction.gemspec
CHANGED
@@ -1,59 +1,26 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: resque-restriction 0.4.0 ruby lib
|
1
|
+
lib = File.expand_path('lib', __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'resque/restriction/version'
|
6
4
|
|
7
|
-
Gem::Specification.new do |
|
8
|
-
|
9
|
-
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'resque-restriction'
|
7
|
+
spec.version = Resque::Restriction::VERSION
|
8
|
+
spec.authors = ['Richard Huang']
|
9
|
+
spec.email = ['flyerhzm@gmail.com']
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
s.date = "2015-10-19"
|
15
|
-
s.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."
|
16
|
-
s.email = "flyerhzm@gmail.com"
|
17
|
-
s.extra_rdoc_files = [
|
18
|
-
"LICENSE",
|
19
|
-
"README.markdown"
|
20
|
-
]
|
21
|
-
s.files = [
|
22
|
-
"CHANGELOG.md",
|
23
|
-
"Gemfile",
|
24
|
-
"Gemfile.lock",
|
25
|
-
"LICENSE",
|
26
|
-
"README.markdown",
|
27
|
-
"Rakefile",
|
28
|
-
"VERSION",
|
29
|
-
"init.rb",
|
30
|
-
"lib/resque-restriction.rb",
|
31
|
-
"lib/resque-restriction/job.rb",
|
32
|
-
"lib/resque-restriction/restriction_job.rb",
|
33
|
-
"rails/init.rb",
|
34
|
-
"resque-restriction.gemspec",
|
35
|
-
"spec/redis-test.conf",
|
36
|
-
"spec/resque-restriction/job_spec.rb",
|
37
|
-
"spec/resque-restriction/restriction_job_spec.rb",
|
38
|
-
"spec/spec_helper.rb"
|
39
|
-
]
|
40
|
-
s.homepage = "http://github.com/flyerhzm/resque-restriction"
|
41
|
-
s.rubygems_version = "2.4.8"
|
42
|
-
s.summary = "resque-restriction is an extension to resque queue system that restricts the execution number of certain jobs in a period time."
|
11
|
+
spec.summary = 'resque-restriction is an extension to resque queue system that restricts the execution number of certain jobs in a period time.'
|
12
|
+
spec.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.'
|
13
|
+
spec.homepage = 'https://github.com/flyerhzm/resque-restriction'
|
43
14
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
48
|
-
s.add_runtime_dependency(%q<resque-restriction>, [">= 0"])
|
49
|
-
s.add_runtime_dependency(%q<resque>, [">= 1.7.0"])
|
50
|
-
else
|
51
|
-
s.add_dependency(%q<resque-restriction>, [">= 0"])
|
52
|
-
s.add_dependency(%q<resque>, [">= 1.7.0"])
|
53
|
-
end
|
54
|
-
else
|
55
|
-
s.add_dependency(%q<resque-restriction>, [">= 0"])
|
56
|
-
s.add_dependency(%q<resque>, [">= 1.7.0"])
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
16
|
+
f.match(%r{^(test|spec|features)/})
|
57
17
|
end
|
58
|
-
|
18
|
+
spec.bindir = 'exe'
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ['lib']
|
59
21
|
|
22
|
+
spec.add_development_dependency 'bundler'
|
23
|
+
spec.add_development_dependency 'rake'
|
24
|
+
spec.add_development_dependency 'resque', '>= 1.7.0'
|
25
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
26
|
+
end
|
metadata
CHANGED
@@ -1,23 +1,37 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: resque-restriction
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richard Huang
|
8
|
-
autorequire:
|
9
|
-
bindir:
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-06-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
|
-
type: :
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
21
35
|
prerelease: false
|
22
36
|
version_requirements: !ruby/object:Gem::Requirement
|
23
37
|
requirements:
|
@@ -31,44 +45,53 @@ dependencies:
|
|
31
45
|
- - ">="
|
32
46
|
- !ruby/object:Gem::Version
|
33
47
|
version: 1.7.0
|
34
|
-
type: :
|
48
|
+
type: :development
|
35
49
|
prerelease: false
|
36
50
|
version_requirements: !ruby/object:Gem::Requirement
|
37
51
|
requirements:
|
38
52
|
- - ">="
|
39
53
|
- !ruby/object:Gem::Version
|
40
54
|
version: 1.7.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
41
69
|
description: resque-restriction is an extension to resque queue system that restricts
|
42
70
|
the execution number of certain jobs in a period time, the exceeded jobs will be
|
43
71
|
executed at the next period.
|
44
|
-
email:
|
72
|
+
email:
|
73
|
+
- flyerhzm@gmail.com
|
45
74
|
executables: []
|
46
75
|
extensions: []
|
47
|
-
extra_rdoc_files:
|
48
|
-
- LICENSE
|
49
|
-
- README.markdown
|
76
|
+
extra_rdoc_files: []
|
50
77
|
files:
|
78
|
+
- ".gitignore"
|
79
|
+
- ".travis.yml"
|
51
80
|
- CHANGELOG.md
|
52
81
|
- Gemfile
|
53
|
-
- Gemfile.lock
|
54
82
|
- LICENSE
|
55
|
-
- README.
|
83
|
+
- README.md
|
56
84
|
- Rakefile
|
57
|
-
-
|
58
|
-
-
|
59
|
-
- lib/resque
|
60
|
-
- lib/resque
|
61
|
-
- lib/resque
|
62
|
-
- rails/init.rb
|
85
|
+
- lib/resque/plugins/job.rb
|
86
|
+
- lib/resque/plugins/restriction.rb
|
87
|
+
- lib/resque/restriction.rb
|
88
|
+
- lib/resque/restriction/config.rb
|
89
|
+
- lib/resque/restriction/version.rb
|
63
90
|
- resque-restriction.gemspec
|
64
|
-
|
65
|
-
- spec/resque-restriction/job_spec.rb
|
66
|
-
- spec/resque-restriction/restriction_job_spec.rb
|
67
|
-
- spec/spec_helper.rb
|
68
|
-
homepage: http://github.com/flyerhzm/resque-restriction
|
91
|
+
homepage: https://github.com/flyerhzm/resque-restriction
|
69
92
|
licenses: []
|
70
93
|
metadata: {}
|
71
|
-
post_install_message:
|
94
|
+
post_install_message:
|
72
95
|
rdoc_options: []
|
73
96
|
require_paths:
|
74
97
|
- lib
|
@@ -83,9 +106,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
83
106
|
- !ruby/object:Gem::Version
|
84
107
|
version: '0'
|
85
108
|
requirements: []
|
86
|
-
|
87
|
-
|
88
|
-
signing_key:
|
109
|
+
rubygems_version: 3.1.6
|
110
|
+
signing_key:
|
89
111
|
specification_version: 4
|
90
112
|
summary: resque-restriction is an extension to resque queue system that restricts
|
91
113
|
the execution number of certain jobs in a period time.
|
data/Gemfile.lock
DELETED
@@ -1,116 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
resque-restriction (0.4.0)
|
5
|
-
activejob
|
6
|
-
jeweler
|
7
|
-
mocha
|
8
|
-
resque
|
9
|
-
rspec
|
10
|
-
|
11
|
-
GEM
|
12
|
-
remote: https://rubygems.org/
|
13
|
-
specs:
|
14
|
-
activejob (4.2.0)
|
15
|
-
activesupport (= 4.2.0)
|
16
|
-
globalid (>= 0.3.0)
|
17
|
-
activesupport (4.2.0)
|
18
|
-
i18n (~> 0.7)
|
19
|
-
json (~> 1.7, >= 1.7.7)
|
20
|
-
minitest (~> 5.1)
|
21
|
-
thread_safe (~> 0.3, >= 0.3.4)
|
22
|
-
tzinfo (~> 1.1)
|
23
|
-
addressable (2.3.6)
|
24
|
-
builder (3.2.2)
|
25
|
-
descendants_tracker (0.0.4)
|
26
|
-
thread_safe (~> 0.3, >= 0.3.1)
|
27
|
-
diff-lcs (1.2.5)
|
28
|
-
faraday (0.9.0)
|
29
|
-
multipart-post (>= 1.2, < 3)
|
30
|
-
git (1.2.9.1)
|
31
|
-
github_api (0.12.4)
|
32
|
-
addressable (~> 2.3)
|
33
|
-
descendants_tracker (~> 0.0.4)
|
34
|
-
faraday (~> 0.8, < 0.10)
|
35
|
-
hashie (>= 3.4)
|
36
|
-
multi_json (>= 1.7.5, < 2.0)
|
37
|
-
nokogiri (~> 1.6.6)
|
38
|
-
oauth2
|
39
|
-
globalid (0.3.3)
|
40
|
-
activesupport (>= 4.1.0)
|
41
|
-
hashie (3.4.2)
|
42
|
-
highline (1.6.21)
|
43
|
-
i18n (0.7.0)
|
44
|
-
jeweler (2.0.1)
|
45
|
-
builder
|
46
|
-
bundler (>= 1.0)
|
47
|
-
git (>= 1.2.5)
|
48
|
-
github_api
|
49
|
-
highline (>= 1.6.15)
|
50
|
-
nokogiri (>= 1.5.10)
|
51
|
-
rake
|
52
|
-
rdoc
|
53
|
-
json (1.8.3)
|
54
|
-
jwt (1.0.0)
|
55
|
-
metaclass (0.0.4)
|
56
|
-
mini_portile (0.6.1)
|
57
|
-
minitest (5.7.0)
|
58
|
-
mocha (1.1.0)
|
59
|
-
metaclass (~> 0.0.1)
|
60
|
-
mono_logger (1.1.0)
|
61
|
-
multi_json (1.11.2)
|
62
|
-
multi_xml (0.5.5)
|
63
|
-
multipart-post (2.0.0)
|
64
|
-
nokogiri (1.6.6.2)
|
65
|
-
mini_portile (~> 0.6.0)
|
66
|
-
oauth2 (1.0.0)
|
67
|
-
faraday (>= 0.8, < 0.10)
|
68
|
-
jwt (~> 1.0)
|
69
|
-
multi_json (~> 1.3)
|
70
|
-
multi_xml (~> 0.5)
|
71
|
-
rack (~> 1.2)
|
72
|
-
rack (1.6.0)
|
73
|
-
rack-protection (1.5.3)
|
74
|
-
rack
|
75
|
-
rake (10.4.2)
|
76
|
-
rdoc (4.1.0)
|
77
|
-
redis (3.2.1)
|
78
|
-
redis-namespace (1.5.1)
|
79
|
-
redis (~> 3.0, >= 3.0.4)
|
80
|
-
resque (1.25.2)
|
81
|
-
mono_logger (~> 1.0)
|
82
|
-
multi_json (~> 1.0)
|
83
|
-
redis-namespace (~> 1.3)
|
84
|
-
sinatra (>= 0.9.2)
|
85
|
-
vegas (~> 0.1.2)
|
86
|
-
rspec (3.1.0)
|
87
|
-
rspec-core (~> 3.1.0)
|
88
|
-
rspec-expectations (~> 3.1.0)
|
89
|
-
rspec-mocks (~> 3.1.0)
|
90
|
-
rspec-core (3.1.7)
|
91
|
-
rspec-support (~> 3.1.0)
|
92
|
-
rspec-expectations (3.1.2)
|
93
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
94
|
-
rspec-support (~> 3.1.0)
|
95
|
-
rspec-mocks (3.1.3)
|
96
|
-
rspec-support (~> 3.1.0)
|
97
|
-
rspec-support (3.1.2)
|
98
|
-
sinatra (1.4.5)
|
99
|
-
rack (~> 1.4)
|
100
|
-
rack-protection (~> 1.4)
|
101
|
-
tilt (~> 1.3, >= 1.3.4)
|
102
|
-
thread_safe (0.3.5)
|
103
|
-
tilt (1.4.1)
|
104
|
-
tzinfo (1.2.2)
|
105
|
-
thread_safe (~> 0.1)
|
106
|
-
vegas (0.1.11)
|
107
|
-
rack (>= 1.0.0)
|
108
|
-
|
109
|
-
PLATFORMS
|
110
|
-
ruby
|
111
|
-
|
112
|
-
DEPENDENCIES
|
113
|
-
resque-restriction!
|
114
|
-
|
115
|
-
BUNDLED WITH
|
116
|
-
1.10.3
|
data/README.markdown
DELETED
@@ -1,83 +0,0 @@
|
|
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
|
-
Attention
|
13
|
-
---------
|
14
|
-
|
15
|
-
The <code>identifier</code> method is renamed to <code>restriction_identifier</code> to solve the confliction with resque-retry from version 0.3.0.
|
16
|
-
|
17
|
-
Install
|
18
|
-
-------
|
19
|
-
|
20
|
-
sudo gem install resque-restriction
|
21
|
-
|
22
|
-
To use
|
23
|
-
------
|
24
|
-
|
25
|
-
It is especially useful when a system has an email invitation resque job, because sending emails too frequentyly will be treated as a spam. What you should do for the InvitationJob is to inherit it from Resque::Plugins::RestrictionJob class and add restrict definition. Example:
|
26
|
-
|
27
|
-
class InvitationJob < Resque::Plugins::RestrictionJob
|
28
|
-
restrict :per_day => 1000, :per_hour => 100, :per_300 => 30
|
29
|
-
|
30
|
-
#rest of your class here
|
31
|
-
end
|
32
|
-
|
33
|
-
That means the InvitationJob can not be executed more than 1000 times per day, 100 times per hour and 30 times per 300 seconds. All restrictions have to be met for the job to execute.
|
34
|
-
|
35
|
-
The argument of restrict method is a hash, the key of the hash is a period time, including :concurrent, :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. The :concurrent option restricts the number of jobs that run simultaneously.
|
36
|
-
|
37
|
-
Advance
|
38
|
-
-------
|
39
|
-
|
40
|
-
You can also add customized restriction as you like. For example, we have a job to restrict the facebook post numbers 40 times per user per day, we can define as:
|
41
|
-
|
42
|
-
class GenerateFacebookShares < Resque::Plugins::RestrictionJob
|
43
|
-
restrict :per_day => 40
|
44
|
-
|
45
|
-
def self.restriction_identifier(options)
|
46
|
-
[self.to_s, options["user_id"]].join(":")
|
47
|
-
end
|
48
|
-
|
49
|
-
#rest of your class here
|
50
|
-
end
|
51
|
-
|
52
|
-
options["user_id"] returns the user's facebook uid, the key point is that the different restriction_identifiers can restrict different job execution numbers.
|
53
|
-
|
54
|
-
|
55
|
-
Contributing
|
56
|
-
------------
|
57
|
-
|
58
|
-
Once you've made your commits:
|
59
|
-
|
60
|
-
1. [Fork][1] Resque Restriction
|
61
|
-
2. Create a topic branch - `git checkout -b my_branch`
|
62
|
-
3. Push to your branch - `git push origin my_branch`
|
63
|
-
4. Create an [Issue][2] with a link to your branch
|
64
|
-
5. That's it!
|
65
|
-
|
66
|
-
Author
|
67
|
-
------
|
68
|
-
Richard Huang :: flyerhzm@gmail.com :: @flyerhzm
|
69
|
-
|
70
|
-
Contributors
|
71
|
-
------------
|
72
|
-
Matt Conway :: matt@conwaysplace.com :: @mattconway
|
73
|
-
|
74
|
-
Martin Fourcade :: fourcade.m@gmail.com :: @mfourcade
|
75
|
-
|
76
|
-
Copyright
|
77
|
-
---------
|
78
|
-
Copyright (c) 2010 Richard Huang. See LICENSE for details.
|
79
|
-
|
80
|
-
[0]: http://github.com/defunkt/resque
|
81
|
-
[1]: http://help.github.com/forking/
|
82
|
-
[2]: http://github.com/flyerhzm/resque-restriction/issues
|
83
|
-
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
0.4.0
|
data/init.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__) + '/rails/init')
|
data/lib/resque-restriction.rb
DELETED
data/rails/init.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__) + '/../lib/resque-restriction')
|
data/spec/redis-test.conf
DELETED
@@ -1,132 +0,0 @@
|
|
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
|
@@ -1,39 +0,0 @@
|
|
1
|
-
require File.expand_path('../spec_helper', File.dirname(__FILE__))
|
2
|
-
|
3
|
-
RSpec.describe Resque::Job do
|
4
|
-
before(:example) do
|
5
|
-
Resque.redis.flushall
|
6
|
-
end
|
7
|
-
|
8
|
-
it "should repush restriction queue when reserve" do
|
9
|
-
Resque.push('restriction_normal', :class => 'OneHourRestrictionJob', :args => ['any args'])
|
10
|
-
expect(Resque::Job.reserve('restriction_normal')).to eq Resque::Job.new('restriction_normal', {'class' => 'OneHourRestrictionJob', 'args' => ['any args']})
|
11
|
-
expect(Resque::Job.reserve('restriction_normal')).to be_nil
|
12
|
-
expect(Resque::Job.reserve('normal')).to be_nil
|
13
|
-
end
|
14
|
-
|
15
|
-
it "should push back to restriction queue when still restricted" do
|
16
|
-
Resque.redis.set(OneHourRestrictionJob.redis_key(:per_hour), -1)
|
17
|
-
Resque.push('restriction_normal', :class => 'OneHourRestrictionJob', :args => ['any args'])
|
18
|
-
expect(Resque::Job.reserve('restriction_normal')).to be_nil
|
19
|
-
expect(Resque.pop('restriction_normal')).to eq({'class' => 'OneHourRestrictionJob', 'args' => ['any args']})
|
20
|
-
expect(Resque::Job.reserve('normal')).to be_nil
|
21
|
-
end
|
22
|
-
|
23
|
-
it "should not repush when reserve normal queue" do
|
24
|
-
Resque.push('normal', :class => 'OneHourRestrictionJob', :args => ['any args'])
|
25
|
-
expect(Resque::Job.reserve('normal')).to eq Resque::Job.new('normal', {'class' => 'OneHourRestrictionJob', 'args' => ['any args']})
|
26
|
-
expect(Resque::Job.reserve('normal')).to be_nil
|
27
|
-
expect(Resque::Job.reserve('restriction_normal')).to be_nil
|
28
|
-
end
|
29
|
-
|
30
|
-
it "should only push back queue_length times to restriction queue" do
|
31
|
-
Resque.redis.set(OneHourRestrictionJob.redis_key(:per_hour), -1)
|
32
|
-
3.times { Resque.push('restriction_normal', :class => 'OneHourRestrictionJob', :args => ['any args']) }
|
33
|
-
expect(Resque.size('restriction_normal')).to eq 3
|
34
|
-
expect(OneHourRestrictionJob).to receive(:repush).exactly(3).times.and_return(true)
|
35
|
-
Resque::Job.reserve('restriction_normal')
|
36
|
-
end
|
37
|
-
|
38
|
-
|
39
|
-
end
|
@@ -1,222 +0,0 @@
|
|
1
|
-
require File.expand_path('../spec_helper', File.dirname(__FILE__))
|
2
|
-
|
3
|
-
RSpec.describe Resque::Plugins::RestrictionJob do
|
4
|
-
skip "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
|
-
expect(Resque::Plugins::RestrictionJob.redis_key(:per_minute)).to eq "Resque::Plugins::RestrictionJob:#{Time.now.to_i / 60}"
|
11
|
-
expect(Resque::Plugins::RestrictionJob.redis_key(:per_hour)).to eq "Resque::Plugins::RestrictionJob:#{Time.now.to_i / (60*60)}"
|
12
|
-
expect(Resque::Plugins::RestrictionJob.redis_key(:per_day)).to eq "Resque::Plugins::RestrictionJob:#{Time.now.to_i / (24*60*60)}"
|
13
|
-
expect(Resque::Plugins::RestrictionJob.redis_key(:per_month)).to eq "Resque::Plugins::RestrictionJob:#{Date.today.strftime("%Y-%m")}"
|
14
|
-
expect(Resque::Plugins::RestrictionJob.redis_key(:per_year)).to eq "Resque::Plugins::RestrictionJob:#{Date.today.year}"
|
15
|
-
end
|
16
|
-
|
17
|
-
it "should accept customization" do
|
18
|
-
expect(Resque::Plugins::RestrictionJob.redis_key(:per_1800)).to eq "Resque::Plugins::RestrictionJob:#{Time.now.to_i / 1800}"
|
19
|
-
expect(Resque::Plugins::RestrictionJob.redis_key(:per_7200)).to eq "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
|
-
expect(OneDayRestrictionJob.settings).to eq({:per_day => 100})
|
26
|
-
expect(OneHourRestrictionJob.settings).to eq({:per_hour => 10})
|
27
|
-
expect(MultipleRestrictionJob.settings).to eq({:per_hour => 10, :per_300 => 2})
|
28
|
-
expect(MultiCallRestrictionJob.settings).to eq({:per_hour => 10, :per_300 => 2})
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
context 'restriction_queue_name' do
|
33
|
-
class MyJob < Resque::Plugins::RestrictionJob
|
34
|
-
queue_as 'awesome_queue_name'
|
35
|
-
|
36
|
-
def perform(args)
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
it 'concats restriction queue prefix with queue name' do
|
41
|
-
expect(MyJob.restriction_queue_name).to eq("#{Resque::Plugins::Restriction::RESTRICTION_QUEUE_PREFIX}_awesome_queue_name")
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
context "resque" do
|
46
|
-
include PerformJob
|
47
|
-
|
48
|
-
before(:example) do
|
49
|
-
Resque.redis.flushall
|
50
|
-
end
|
51
|
-
|
52
|
-
it "should set execution number and decrement it when one job first executed" do
|
53
|
-
result = perform_job(OneHourRestrictionJob, "any args")
|
54
|
-
expect(Resque.redis.get(OneHourRestrictionJob.redis_key(:per_hour))).to eq "9"
|
55
|
-
end
|
56
|
-
|
57
|
-
it "should use restriction_identifier to set exclusive execution counts" do
|
58
|
-
result = perform_job(IdentifiedRestrictionJob, 1)
|
59
|
-
result = perform_job(IdentifiedRestrictionJob, 1)
|
60
|
-
result = perform_job(IdentifiedRestrictionJob, 2)
|
61
|
-
|
62
|
-
expect(Resque.redis.get(IdentifiedRestrictionJob.redis_key(:per_hour, 1))).to eq "8"
|
63
|
-
expect(Resque.redis.get(IdentifiedRestrictionJob.redis_key(:per_hour, 2))).to eq "9"
|
64
|
-
end
|
65
|
-
|
66
|
-
it "should decrement execution number when one job executed" do
|
67
|
-
Resque.redis.set(OneHourRestrictionJob.redis_key(:per_hour), 6)
|
68
|
-
result = perform_job(OneHourRestrictionJob, "any args")
|
69
|
-
|
70
|
-
expect(Resque.redis.get(OneHourRestrictionJob.redis_key(:per_hour))).to eq "5"
|
71
|
-
end
|
72
|
-
|
73
|
-
it "should increment execution number when concurrent job completes" do
|
74
|
-
t = Thread.new do
|
75
|
-
perform_job(ConcurrentRestrictionJob, "any args")
|
76
|
-
end
|
77
|
-
sleep 0.1
|
78
|
-
expect(Resque.redis.get(ConcurrentRestrictionJob.redis_key(:concurrent))).to eq "0"
|
79
|
-
t.join
|
80
|
-
expect(Resque.redis.get(ConcurrentRestrictionJob.redis_key(:concurrent))).to eq "1"
|
81
|
-
end
|
82
|
-
|
83
|
-
it "should increment execution number when concurrent job fails" do
|
84
|
-
expect_any_instance_of(ConcurrentRestrictionJob).to receive(:perform).and_raise("bad")
|
85
|
-
perform_job(ConcurrentRestrictionJob, "any args") rescue nil
|
86
|
-
expect(Resque.redis.get(ConcurrentRestrictionJob.redis_key(:concurrent))).to eq "1"
|
87
|
-
end
|
88
|
-
|
89
|
-
it "should put the job into restriction queue when execution count < 0" do
|
90
|
-
Resque.redis.set(OneHourRestrictionJob.redis_key(:per_hour), 0)
|
91
|
-
result = perform_job(OneHourRestrictionJob, "any args")
|
92
|
-
# expect(result).to_not be(true)
|
93
|
-
expect(Resque.redis.get(OneHourRestrictionJob.redis_key(:per_hour))).to eq "0"
|
94
|
-
expect(Resque.redis.lrange("queue:restriction_normal", 0, -1)).to eq [Resque.encode(:class => "OneHourRestrictionJob", :args => ["any args"])]
|
95
|
-
end
|
96
|
-
|
97
|
-
describe "expiration of period keys" do
|
98
|
-
class MyJob < Resque::Plugins::RestrictionJob
|
99
|
-
def perform(args)
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
shared_examples_for "expiration" do
|
104
|
-
before(:example) do
|
105
|
-
MyJob.restrict period => 10
|
106
|
-
end
|
107
|
-
|
108
|
-
context "when the key is not set" do
|
109
|
-
it "should mark period keys to expire" do
|
110
|
-
perform_job(MyJob, "any args")
|
111
|
-
expect(Resque.redis.ttl(MyJob.redis_key(period))).to eq MyJob.seconds(period)
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
context "when the key is set" do
|
116
|
-
before(:example) do
|
117
|
-
Resque.redis.set(MyJob.redis_key(period), 5)
|
118
|
-
end
|
119
|
-
|
120
|
-
it "should not mark period keys to expire" do
|
121
|
-
perform_job(MyJob, "any args")
|
122
|
-
expect(Resque.redis.ttl(MyJob.redis_key(period))).to eq -1
|
123
|
-
end
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
describe "per minute" do
|
128
|
-
def period
|
129
|
-
:per_minute
|
130
|
-
end
|
131
|
-
|
132
|
-
it_should_behave_like "expiration"
|
133
|
-
end
|
134
|
-
|
135
|
-
describe "per hour" do
|
136
|
-
def period
|
137
|
-
:per_hour
|
138
|
-
end
|
139
|
-
|
140
|
-
it_should_behave_like "expiration"
|
141
|
-
end
|
142
|
-
|
143
|
-
describe "per day" do
|
144
|
-
def period
|
145
|
-
:per_day
|
146
|
-
end
|
147
|
-
|
148
|
-
it_should_behave_like "expiration"
|
149
|
-
end
|
150
|
-
|
151
|
-
describe "per week" do
|
152
|
-
def period
|
153
|
-
:per_week
|
154
|
-
end
|
155
|
-
|
156
|
-
it_should_behave_like "expiration"
|
157
|
-
end
|
158
|
-
|
159
|
-
describe "per month" do
|
160
|
-
def period
|
161
|
-
:per_month
|
162
|
-
end
|
163
|
-
|
164
|
-
it_should_behave_like "expiration"
|
165
|
-
end
|
166
|
-
|
167
|
-
describe "per year" do
|
168
|
-
def period
|
169
|
-
:per_year
|
170
|
-
end
|
171
|
-
|
172
|
-
it_should_behave_like "expiration"
|
173
|
-
end
|
174
|
-
|
175
|
-
describe "per custom period" do
|
176
|
-
def period
|
177
|
-
:per_359
|
178
|
-
end
|
179
|
-
|
180
|
-
it_should_behave_like "expiration"
|
181
|
-
end
|
182
|
-
end
|
183
|
-
|
184
|
-
context "multiple restrict" do
|
185
|
-
it "should restrict per_minute" do
|
186
|
-
result = perform_job(MultipleRestrictionJob, "any args")
|
187
|
-
expect(Resque.redis.get(MultipleRestrictionJob.redis_key(:per_hour))).to eq "9"
|
188
|
-
expect(Resque.redis.get(MultipleRestrictionJob.redis_key(:per_300))).to eq "1"
|
189
|
-
result = perform_job(MultipleRestrictionJob, "any args")
|
190
|
-
result = perform_job(MultipleRestrictionJob, "any args")
|
191
|
-
expect(Resque.redis.get(MultipleRestrictionJob.redis_key(:per_hour))).to eq "8"
|
192
|
-
expect(Resque.redis.get(MultipleRestrictionJob.redis_key(:per_300))).to eq "0"
|
193
|
-
end
|
194
|
-
|
195
|
-
it "should restrict per_hour" do
|
196
|
-
Resque.redis.set(MultipleRestrictionJob.redis_key(:per_hour), 1)
|
197
|
-
Resque.redis.set(MultipleRestrictionJob.redis_key(:per_300), 2)
|
198
|
-
result = perform_job(MultipleRestrictionJob, "any args")
|
199
|
-
expect(Resque.redis.get(MultipleRestrictionJob.redis_key(:per_hour))).to eq "0"
|
200
|
-
expect(Resque.redis.get(MultipleRestrictionJob.redis_key(:per_300))).to eq "1"
|
201
|
-
result = perform_job(MultipleRestrictionJob, "any args")
|
202
|
-
expect(Resque.redis.get(MultipleRestrictionJob.redis_key(:per_hour))).to eq "0"
|
203
|
-
expect(Resque.redis.get(MultipleRestrictionJob.redis_key(:per_300))).to eq "1"
|
204
|
-
end
|
205
|
-
end
|
206
|
-
|
207
|
-
context "repush" do
|
208
|
-
it "should push restricted jobs onto restriction queue" do
|
209
|
-
Resque.redis.set(OneHourRestrictionJob.redis_key(:per_hour), -1)
|
210
|
-
expect(Resque).to receive(:push).once.with('restriction_normal', :class => 'OneHourRestrictionJob', :args => ['any args'])
|
211
|
-
expect(OneHourRestrictionJob.repush('any args')).to be(true)
|
212
|
-
end
|
213
|
-
|
214
|
-
it "should not push unrestricted jobs onto restriction queue" do
|
215
|
-
Resque.redis.set(OneHourRestrictionJob.redis_key(:per_hour), 1)
|
216
|
-
expect(Resque).not_to receive(:push)
|
217
|
-
expect(OneHourRestrictionJob.repush('any args')).to be(false)
|
218
|
-
end
|
219
|
-
|
220
|
-
end
|
221
|
-
end
|
222
|
-
end
|
data/spec/spec_helper.rb
DELETED
@@ -1,107 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'mocha'
|
3
|
-
|
4
|
-
dir = File.dirname(__FILE__)
|
5
|
-
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../lib"))
|
6
|
-
require 'resque-restriction'
|
7
|
-
|
8
|
-
#
|
9
|
-
# make sure we can run redis
|
10
|
-
#
|
11
|
-
|
12
|
-
if !system("which redis-server")
|
13
|
-
puts '', "** can't find `redis-server` in your path"
|
14
|
-
puts "** try running `sudo rake install`"
|
15
|
-
abort ''
|
16
|
-
end
|
17
|
-
|
18
|
-
|
19
|
-
#
|
20
|
-
# start our own redis when the tests start,
|
21
|
-
# kill it when they end
|
22
|
-
#
|
23
|
-
|
24
|
-
at_exit do
|
25
|
-
next if $!
|
26
|
-
|
27
|
-
exit_code = Spec::Runner.run
|
28
|
-
|
29
|
-
pid = `ps -e -o pid,command | grep [r]edis-test`.split(" ")[0]
|
30
|
-
puts "Killing test redis server [#{pid}]..."
|
31
|
-
`rm -f #{dir}/dump.rdb`
|
32
|
-
Process.kill("KILL", pid.to_i)
|
33
|
-
exit exit_code
|
34
|
-
end
|
35
|
-
|
36
|
-
puts "Starting redis for testing at localhost:9736..."
|
37
|
-
`redis-server #{dir}/redis-test.conf`
|
38
|
-
Resque.redis = 'localhost:9736'
|
39
|
-
|
40
|
-
#
|
41
|
-
# Helper to perform job classes
|
42
|
-
#
|
43
|
-
module PerformJob
|
44
|
-
def perform_job(klass, *args)
|
45
|
-
klass.perform_now(*args)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
class OneDayRestrictionJob < Resque::Plugins::RestrictionJob
|
50
|
-
restrict :per_day => 100
|
51
|
-
|
52
|
-
queue_as 'normal'
|
53
|
-
|
54
|
-
def perform(args)
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
class OneHourRestrictionJob < Resque::Plugins::RestrictionJob
|
59
|
-
restrict :per_hour => 10
|
60
|
-
|
61
|
-
queue_as 'normal'
|
62
|
-
|
63
|
-
def perform(args)
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
class IdentifiedRestrictionJob < Resque::Plugins::RestrictionJob
|
68
|
-
restrict :per_hour => 10
|
69
|
-
|
70
|
-
queue_as 'normal'
|
71
|
-
|
72
|
-
def self.restriction_identifier(*args)
|
73
|
-
[self.to_s, args.first].join(":")
|
74
|
-
end
|
75
|
-
|
76
|
-
def perform(args)
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
class ConcurrentRestrictionJob < Resque::Plugins::RestrictionJob
|
81
|
-
restrict :concurrent => 1
|
82
|
-
|
83
|
-
queue_as 'normal'
|
84
|
-
|
85
|
-
def perform(args)
|
86
|
-
sleep 0.2
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
class MultipleRestrictionJob < Resque::Plugins::RestrictionJob
|
91
|
-
restrict :per_hour => 10, :per_300 => 2
|
92
|
-
|
93
|
-
queue_as 'normal'
|
94
|
-
|
95
|
-
def perform(args)
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
class MultiCallRestrictionJob < Resque::Plugins::RestrictionJob
|
100
|
-
restrict :per_hour => 10
|
101
|
-
restrict :per_300 => 2
|
102
|
-
|
103
|
-
queue_as 'normal'
|
104
|
-
|
105
|
-
def perform(args)
|
106
|
-
end
|
107
|
-
end
|