resque-restriction 0.5.1 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -1
- data/README.md +14 -0
- data/lib/resque/plugins/restriction.rb +10 -7
- data/lib/resque/restriction/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 135cae91708b3d84ff8c6feafdb715e7043893ec
|
4
|
+
data.tar.gz: c4d04692d1b62a3b0bf8dc797a1c679965e05c69
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e0640880f52054dcfee91e434373a2db2b1cd5f61997fbaaefedce2d59f3647fb10ca0d921afd541c0fb2310ad8b29befe5d7f35c080e55be8d50183db089130
|
7
|
+
data.tar.gz: ecb9750f0198139276220ccb85178d09c44f824eba13bf83c3b11e8f3313c837f83f24064a65ca8a43de3b34f6fffc546ade0f5bc890052a88283e1075924876
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -59,6 +59,20 @@ Advance
|
|
59
59
|
|
60
60
|
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:
|
61
61
|
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
class GenerateFacebookShares
|
65
|
+
extend Resque::Plugins::Restriction
|
66
|
+
|
67
|
+
restrict :per_day_and_user_id => 40
|
68
|
+
|
69
|
+
# rest of your class here
|
70
|
+
def self.perform(options)
|
71
|
+
# options["user_id"] exists
|
72
|
+
end
|
73
|
+
end
|
74
|
+
```
|
75
|
+
|
62
76
|
```ruby
|
63
77
|
class GenerateFacebookShares
|
64
78
|
extend Resque::Plugins::Restriction
|
@@ -64,13 +64,15 @@ module Resque
|
|
64
64
|
end
|
65
65
|
|
66
66
|
def redis_key(period, *args)
|
67
|
-
|
67
|
+
period_key, custom_key = period.to_s.split('_and_')
|
68
|
+
period_str = case period_key.to_sym
|
68
69
|
when :concurrent then "*"
|
69
|
-
when :per_minute, :per_hour, :per_day, :per_week then (Time.now.to_i / SECONDS[
|
70
|
+
when :per_minute, :per_hour, :per_day, :per_week then (Time.now.to_i / SECONDS[period_key.to_sym]).to_s
|
70
71
|
when :per_month then Date.today.strftime("%Y-%m")
|
71
72
|
when :per_year then Date.today.year.to_s
|
72
|
-
else
|
73
|
-
|
73
|
+
else period_key =~ /^per_(\d+)$/ and (Time.now.to_i / $1.to_i).to_s end
|
74
|
+
custom_value = (custom_key && args.first && args.first.is_a?(Hash)) ? args.first[custom_key] : nil
|
75
|
+
[RESTRICTION_QUEUE_PREFIX, self.restriction_identifier(*args), custom_value, period_str].compact.join(":")
|
74
76
|
end
|
75
77
|
|
76
78
|
def restriction_identifier(*args)
|
@@ -83,10 +85,11 @@ module Resque
|
|
83
85
|
end
|
84
86
|
|
85
87
|
def seconds(period)
|
86
|
-
|
87
|
-
|
88
|
+
period_key, _ = period.to_s.split('_and_')
|
89
|
+
if SECONDS.keys.include?(period_key.to_sym)
|
90
|
+
SECONDS[period_key.to_sym]
|
88
91
|
else
|
89
|
-
|
92
|
+
period_key =~ /^per_(\d+)$/ and $1.to_i
|
90
93
|
end
|
91
94
|
end
|
92
95
|
|