Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Expiring restriction keys after their effective period has finished. #6

Merged
merged 1 commit into from

2 participants

@vicentemundim
  • This helps keeping redis resque namespace more clean, by using redis expire command to set an expiration for the restriction key by the same period of its effectiveness.
  • For example, if a job is restricted by minute, the first time that restriction key is set it will be marked to expire in 60 seconds, for a job that is restricted by hour it will be marked to expire after an hour, and so on.
@vicentemundim vicentemundim Expiring restriction keys after their effective period has finished.
- This helps keeping redis resque namespace more clean, by using redis expire command to set an expiration for the restriction key by the same period of its effectiveness.
- For example, if a job is restricted by minute, the first time that restriction key is set it will be marked to expire in 60 seconds, for a job that is restricted by hour it will be marked to expire after an hour, and so on.
3b68c6e
@mattes mattes referenced this pull request
Open

remove old queue key #1

@flyerhzm flyerhzm merged commit 6534409 into flyerhzm:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 5, 2011
  1. @vicentemundim

    Expiring restriction keys after their effective period has finished.

    vicentemundim authored
    - This helps keeping redis resque namespace more clean, by using redis expire command to set an expiration for the restriction key by the same period of its effectiveness.
    - For example, if a job is restricted by minute, the first time that restriction key is set it will be marked to expire in 60 seconds, for a job that is restricted by hour it will be marked to expire after an hour, and so on.
This page is out of date. Refresh to see the latest.
View
8 lib/resque-restriction/restriction_job.rb
@@ -44,6 +44,9 @@ def before_perform_restriction(*args)
Resque.push restriction_queue_name, :class => to_s, :args => args
raise Resque::Job::DontPerform
end
+ else
+ # This is the first time we set the key, so we mark it to expire
+ mark_restriction_key_to_expire_for(key, period)
end
end
end
@@ -82,7 +85,7 @@ def seconds(period)
if SECONDS.keys.include? period
SECONDS[period]
else
- period.to_s =~ /^per_(\d+)$/ and $1
+ period.to_s =~ /^per_(\d+)$/ and $1.to_i
end
end
@@ -106,6 +109,9 @@ def repush(*args)
end
end
+ def mark_restriction_key_to_expire_for(key, period)
+ Resque.redis.expire(key, seconds(period)) unless period == :concurrent
+ end
end
class RestrictionJob
View
2  spec/resque-restriction/job_spec.rb
@@ -1,4 +1,4 @@
-require File.join(File.dirname(__FILE__) + '/../spec_helper')
+require File.expand_path('../spec_helper', File.dirname(__FILE__))
describe Resque::Job do
before(:each) do
View
91 spec/resque-restriction/restriction_job_spec.rb
@@ -1,4 +1,4 @@
-require File.join(File.dirname(__FILE__) + '/../spec_helper')
+require File.expand_path('../spec_helper', File.dirname(__FILE__))
describe Resque::Plugins::RestrictionJob do
it "should follow the convention" do
@@ -85,6 +85,95 @@
Resque.redis.lrange("queue:restriction_normal", 0, -1).should == [Resque.encode(:class => "OneHourRestrictionJob", :args => ["any args"])]
end
+ describe "expiration of period keys" do
+ class MyJob
+ extend Resque::Plugins::Restriction
+
+ def self.perform(*args)
+ end
+ end
+
+ shared_examples_for "expiration" do
+ before(:each) do
+ MyJob.restrict period => 10
+ end
+
+ context "when the key is not set" do
+ it "should mark period keys to expire" do
+ perform_job(MyJob, "any args")
+ Resque.redis.ttl(MyJob.redis_key(period)).should == MyJob.seconds(period)
+ end
+ end
+
+ context "when the key is set" do
+ before(:each) do
+ Resque.redis.set(MyJob.redis_key(period), 5)
+ end
+
+ it "should not mark period keys to expire" do
+ perform_job(MyJob, "any args")
+ Resque.redis.ttl(MyJob.redis_key(period)).should == -1
+ end
+ end
+ end
+
+ describe "per minute" do
+ def period
+ :per_minute
+ end
+
+ it_should_behave_like "expiration"
+ end
+
+ describe "per hour" do
+ def period
+ :per_hour
+ end
+
+ it_should_behave_like "expiration"
+ end
+
+ describe "per day" do
+ def period
+ :per_day
+ end
+
+ it_should_behave_like "expiration"
+ end
+
+ describe "per week" do
+ def period
+ :per_week
+ end
+
+ it_should_behave_like "expiration"
+ end
+
+ describe "per month" do
+ def period
+ :per_month
+ end
+
+ it_should_behave_like "expiration"
+ end
+
+ describe "per year" do
+ def period
+ :per_year
+ end
+
+ it_should_behave_like "expiration"
+ end
+
+ describe "per custom period" do
+ def period
+ :per_359
+ end
+
+ it_should_behave_like "expiration"
+ end
+ end
+
context "multiple restrict" do
it "should restrict per_minute" do
result = perform_job(MultipleRestrictionJob, "any args")
Something went wrong with that request. Please try again.