resque-restriction 0.4.2 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +6 -0
- data/CHANGELOG.md +6 -1
- data/Gemfile.lock +21 -80
- data/{README.markdown → README.md} +31 -40
- data/Rakefile +1 -16
- data/lib/{resque-restriction → resque/plugins}/job.rb +0 -0
- data/lib/{resque-restriction/restriction_job.rb → resque/plugins/restriction.rb} +6 -24
- data/lib/resque/restriction.rb +3 -0
- data/lib/resque/restriction/version.rb +5 -0
- data/resque-restriction.gemspec +21 -45
- metadata +57 -21
- 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
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9d5a68d3642ec0f119a7b0a5ab5765c9e8120ecd
|
4
|
+
data.tar.gz: 0ce7caaf81c7e7c709b918b908e26f8848a4eb29
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2d84c124557820fb0defd27dd1e49d93919248d19949037a1f1208734b6857b3509f5a2c49a914111e959ead96e35dae54cb9ca2795b13ee45057753a1398920
|
7
|
+
data.tar.gz: 40c497e72004494edde919e59f3dfdbf4fb26e7ef3d2382db6111cd3affde8bde4a6e8995b9d10a57f6e814f7f880001e0564650d3131175b1a1e94323e37367
|
data/.gitignore
ADDED
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,79 +1,18 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
resque-restriction (0.
|
5
|
-
activejob
|
6
|
-
jeweler
|
7
|
-
mocha
|
8
|
-
resque
|
9
|
-
rspec
|
4
|
+
resque-restriction (0.5.0)
|
10
5
|
|
11
6
|
GEM
|
12
7
|
remote: https://rubygems.org/
|
13
8
|
specs:
|
14
|
-
|
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)
|
9
|
+
diff-lcs (1.3)
|
60
10
|
mono_logger (1.1.0)
|
61
11
|
multi_json (1.11.2)
|
62
|
-
|
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)
|
12
|
+
rack (1.6.4)
|
73
13
|
rack-protection (1.5.3)
|
74
14
|
rack
|
75
|
-
rake (10.
|
76
|
-
rdoc (4.1.0)
|
15
|
+
rake (10.5.0)
|
77
16
|
redis (3.2.1)
|
78
17
|
redis-namespace (1.5.1)
|
79
18
|
redis (~> 3.0, >= 3.0.4)
|
@@ -83,26 +22,24 @@ GEM
|
|
83
22
|
redis-namespace (~> 1.3)
|
84
23
|
sinatra (>= 0.9.2)
|
85
24
|
vegas (~> 0.1.2)
|
86
|
-
rspec (3.
|
87
|
-
rspec-core (~> 3.
|
88
|
-
rspec-expectations (~> 3.
|
89
|
-
rspec-mocks (~> 3.
|
90
|
-
rspec-core (3.1
|
91
|
-
rspec-support (~> 3.
|
92
|
-
rspec-expectations (3.
|
25
|
+
rspec (3.7.0)
|
26
|
+
rspec-core (~> 3.7.0)
|
27
|
+
rspec-expectations (~> 3.7.0)
|
28
|
+
rspec-mocks (~> 3.7.0)
|
29
|
+
rspec-core (3.7.1)
|
30
|
+
rspec-support (~> 3.7.0)
|
31
|
+
rspec-expectations (3.7.0)
|
93
32
|
diff-lcs (>= 1.2.0, < 2.0)
|
94
|
-
rspec-support (~> 3.
|
95
|
-
rspec-mocks (3.
|
96
|
-
|
97
|
-
|
33
|
+
rspec-support (~> 3.7.0)
|
34
|
+
rspec-mocks (3.7.0)
|
35
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
36
|
+
rspec-support (~> 3.7.0)
|
37
|
+
rspec-support (3.7.1)
|
98
38
|
sinatra (1.4.5)
|
99
39
|
rack (~> 1.4)
|
100
40
|
rack-protection (~> 1.4)
|
101
41
|
tilt (~> 1.3, >= 1.3.4)
|
102
|
-
thread_safe (0.3.5)
|
103
42
|
tilt (1.4.1)
|
104
|
-
tzinfo (1.2.2)
|
105
|
-
thread_safe (~> 0.1)
|
106
43
|
vegas (0.1.11)
|
107
44
|
rack (>= 1.0.0)
|
108
45
|
|
@@ -110,7 +47,11 @@ PLATFORMS
|
|
110
47
|
ruby
|
111
48
|
|
112
49
|
DEPENDENCIES
|
50
|
+
bundler (~> 1.16)
|
51
|
+
rake (~> 10.0)
|
52
|
+
resque (>= 1.7.0)
|
113
53
|
resque-restriction!
|
54
|
+
rspec (~> 3.0)
|
114
55
|
|
115
56
|
BUNDLED WITH
|
116
|
-
1.
|
57
|
+
1.16.0
|
@@ -1,7 +1,7 @@
|
|
1
1
|
resque-restriction
|
2
2
|
===============
|
3
3
|
|
4
|
-
Resque Restriction is a plugin for the [Resque]
|
4
|
+
Resque Restriction is a plugin for the [Resque](https://github.com/resque/resque) queueing system. It adds two functions:
|
5
5
|
|
6
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
7
|
|
@@ -12,23 +12,37 @@ Resque Restriction requires Resque 1.7.0.
|
|
12
12
|
Attention
|
13
13
|
---------
|
14
14
|
|
15
|
-
The
|
15
|
+
The `identifier` method is renamed to `restriction_identifier` to solve the confliction with resque-retry from version 0.3.0.
|
16
16
|
|
17
17
|
Install
|
18
18
|
-------
|
19
19
|
|
20
|
-
|
20
|
+
Add this line to your application's Gemfile:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
gem 'resque-restriction'
|
24
|
+
```
|
25
|
+
|
26
|
+
And then execute:
|
27
|
+
|
28
|
+
$ bundle
|
29
|
+
|
30
|
+
Or install it yourself as:
|
31
|
+
|
32
|
+
$ gem install resque-restriction
|
21
33
|
|
22
34
|
To use
|
23
35
|
------
|
24
36
|
|
25
37
|
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
38
|
|
27
|
-
|
28
|
-
|
39
|
+
class InvitationJob
|
40
|
+
extend Resque::Plugins::RestrictionJob
|
29
41
|
|
30
|
-
|
31
|
-
|
42
|
+
restrict :per_day => 1000, :per_hour => 100, :per_300 => 30
|
43
|
+
|
44
|
+
#rest of your class here
|
45
|
+
end
|
32
46
|
|
33
47
|
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
48
|
|
@@ -39,15 +53,17 @@ Advance
|
|
39
53
|
|
40
54
|
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
55
|
|
42
|
-
|
43
|
-
|
56
|
+
class GenerateFacebookShares
|
57
|
+
extend Resque::Plugins::RestrictionJob
|
58
|
+
|
59
|
+
restrict :per_day => 40
|
44
60
|
|
45
|
-
|
46
|
-
|
47
|
-
|
61
|
+
def self.restriction_identifier(options)
|
62
|
+
[self.to_s, options["user_id"]].join(":")
|
63
|
+
end
|
48
64
|
|
49
|
-
|
50
|
-
|
65
|
+
#rest of your class here
|
66
|
+
end
|
51
67
|
|
52
68
|
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
69
|
|
@@ -55,29 +71,4 @@ options["user_id"] returns the user's facebook uid, the key point is that the di
|
|
55
71
|
Contributing
|
56
72
|
------------
|
57
73
|
|
58
|
-
|
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
|
-
|
74
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/flyerhzm/resque-restriction.
|
data/Rakefile
CHANGED
@@ -1,22 +1,7 @@
|
|
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
6
|
task :default => :spec
|
7
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
|
File without changes
|
@@ -1,8 +1,5 @@
|
|
1
|
-
require 'active_job'
|
2
|
-
|
3
1
|
module Resque
|
4
2
|
module Plugins
|
5
|
-
|
6
3
|
module Restriction
|
7
4
|
SECONDS = {
|
8
5
|
:per_minute => 60,
|
@@ -23,6 +20,8 @@ module Resque
|
|
23
20
|
end
|
24
21
|
|
25
22
|
def before_perform_restriction(*args)
|
23
|
+
return if Resque.inline?
|
24
|
+
|
26
25
|
keys_decremented = []
|
27
26
|
settings.each do |period, number|
|
28
27
|
key = redis_key(period, *args)
|
@@ -78,6 +77,10 @@ module Resque
|
|
78
77
|
self.to_s
|
79
78
|
end
|
80
79
|
|
80
|
+
def restriction_queue_name
|
81
|
+
queue_name = Resque.queue_from_class(self)
|
82
|
+
"#{RESTRICTION_QUEUE_PREFIX}_#{queue_name}"
|
83
|
+
end
|
81
84
|
|
82
85
|
def seconds(period)
|
83
86
|
if SECONDS.keys.include? period
|
@@ -111,26 +114,5 @@ module Resque
|
|
111
114
|
Resque.redis.expire(key, seconds(period)) unless period == :concurrent
|
112
115
|
end
|
113
116
|
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
117
|
end
|
136
118
|
end
|
data/resque-restriction.gemspec
CHANGED
@@ -1,50 +1,26 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "resque/restriction/version"
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
s.email = "flyerhzm@gmail.com"
|
11
|
-
s.extra_rdoc_files = [
|
12
|
-
"LICENSE",
|
13
|
-
"README.markdown"
|
14
|
-
]
|
15
|
-
s.files = [
|
16
|
-
"CHANGELOG.md",
|
17
|
-
"Gemfile",
|
18
|
-
"Gemfile.lock",
|
19
|
-
"LICENSE",
|
20
|
-
"README.markdown",
|
21
|
-
"Rakefile",
|
22
|
-
"VERSION",
|
23
|
-
"init.rb",
|
24
|
-
"lib/resque-restriction.rb",
|
25
|
-
"lib/resque-restriction/job.rb",
|
26
|
-
"lib/resque-restriction/restriction_job.rb",
|
27
|
-
"rails/init.rb",
|
28
|
-
"resque-restriction.gemspec",
|
29
|
-
"spec/redis-test.conf",
|
30
|
-
"spec/resque-restriction/job_spec.rb",
|
31
|
-
"spec/resque-restriction/restriction_job_spec.rb",
|
32
|
-
"spec/spec_helper.rb"
|
33
|
-
]
|
34
|
-
s.homepage = "http://github.com/flyerhzm/resque-restriction"
|
35
|
-
s.rubygems_version = "2.2.2"
|
36
|
-
s.summary = "resque-restriction is an extension to resque queue system that restricts the execution number of certain jobs in a period time."
|
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"]
|
37
10
|
|
38
|
-
|
39
|
-
|
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'
|
40
14
|
|
41
|
-
|
42
|
-
|
43
|
-
else
|
44
|
-
s.add_dependency(%q<resque>, [">= 1.7.0"])
|
45
|
-
end
|
46
|
-
else
|
47
|
-
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)/})
|
48
17
|
end
|
49
|
-
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
50
21
|
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
25
|
+
spec.add_development_dependency "resque", ">= 1.7.0"
|
26
|
+
end
|
metadata
CHANGED
@@ -1,15 +1,57 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: resque-restriction
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richard Huang
|
8
8
|
autorequire:
|
9
|
-
bindir:
|
9
|
+
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-06-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
13
55
|
- !ruby/object:Gem::Dependency
|
14
56
|
name: resque
|
15
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -17,7 +59,7 @@ dependencies:
|
|
17
59
|
- - ">="
|
18
60
|
- !ruby/object:Gem::Version
|
19
61
|
version: 1.7.0
|
20
|
-
type: :
|
62
|
+
type: :development
|
21
63
|
prerelease: false
|
22
64
|
version_requirements: !ruby/object:Gem::Requirement
|
23
65
|
requirements:
|
@@ -27,31 +69,25 @@ dependencies:
|
|
27
69
|
description: resque-restriction is an extension to resque queue system that restricts
|
28
70
|
the execution number of certain jobs in a period time, the exceeded jobs will be
|
29
71
|
executed at the next period.
|
30
|
-
email:
|
72
|
+
email:
|
73
|
+
- flyerhzm@gmail.com
|
31
74
|
executables: []
|
32
75
|
extensions: []
|
33
|
-
extra_rdoc_files:
|
34
|
-
- LICENSE
|
35
|
-
- README.markdown
|
76
|
+
extra_rdoc_files: []
|
36
77
|
files:
|
78
|
+
- ".gitignore"
|
37
79
|
- CHANGELOG.md
|
38
80
|
- Gemfile
|
39
81
|
- Gemfile.lock
|
40
82
|
- LICENSE
|
41
|
-
- README.
|
83
|
+
- README.md
|
42
84
|
- Rakefile
|
43
|
-
-
|
44
|
-
-
|
45
|
-
- lib/resque
|
46
|
-
- lib/resque
|
47
|
-
- lib/resque-restriction/restriction_job.rb
|
48
|
-
- rails/init.rb
|
85
|
+
- lib/resque/plugins/job.rb
|
86
|
+
- lib/resque/plugins/restriction.rb
|
87
|
+
- lib/resque/restriction.rb
|
88
|
+
- lib/resque/restriction/version.rb
|
49
89
|
- resque-restriction.gemspec
|
50
|
-
|
51
|
-
- spec/resque-restriction/job_spec.rb
|
52
|
-
- spec/resque-restriction/restriction_job_spec.rb
|
53
|
-
- spec/spec_helper.rb
|
54
|
-
homepage: http://github.com/flyerhzm/resque-restriction
|
90
|
+
homepage: https://github.com/flyerhzm/resque-restriction
|
55
91
|
licenses: []
|
56
92
|
metadata: {}
|
57
93
|
post_install_message:
|
@@ -70,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
70
106
|
version: '0'
|
71
107
|
requirements: []
|
72
108
|
rubyforge_project:
|
73
|
-
rubygems_version: 2.
|
109
|
+
rubygems_version: 2.6.14
|
74
110
|
signing_key:
|
75
111
|
specification_version: 4
|
76
112
|
summary: resque-restriction is an extension to resque queue system that restricts
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
0.4.2
|
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
|