Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto corrected by following Ruby Space #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions lib/resque/plugins/restriction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ module Restriction
SECONDS = {
:per_second => 1,
:per_minute => 60,
:per_hour => 60*60,
:per_day => 24*60*60,
:per_week => 7*24*60*60,
:per_month => 31*24*60*60,
:per_year => 366*24*60*60
:per_hour => 60 * 60,
:per_day => 24 * 60 * 60,
:per_week => 7 * 24 * 60 * 60,
:per_month => 31 * 24 * 60 * 60,
:per_year => 366 * 24 * 60 * 60
}
RESTRICTION_QUEUE_PREFIX = 'restriction'

def settings
@options ||= {}
end

def restrict(options={})
def restrict(options = {})
settings.merge!(options)
end

Expand All @@ -30,7 +30,7 @@ def before_perform_restriction(*args)
# first try to set period key to be the total allowed for the period
# if we get a 0 result back, the key wasn't set, so we know we are
# already tracking the count for that period'
period_active = ! Resque.redis.setnx(key, number.to_i - 1)
period_active = !Resque.redis.setnx(key, number.to_i - 1)
# If we are already tracking that period, then decrement by one to
# see if we are allowed to run, pushing to restriction queue to run
# later if not. Note that the value stored is the number of outstanding
Expand All @@ -42,7 +42,7 @@ def before_perform_restriction(*args)
if value < 0
# reincrement the keys if one of the periods triggers DontPerform so
# that we accurately track capacity
keys_decremented.each {|k| Resque.redis.incrby(k, 1) }
keys_decremented.each { |k| Resque.redis.incrby(k, 1) }
Resque.push restriction_queue_name, :class => to_s, :args => args
raise Resque::Job::DontPerform
end
Expand Down
6 changes: 3 additions & 3 deletions spec/resque/job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

it "should repush restriction queue when reserve" do
Resque.push('restriction_normal', :class => 'OneHourRestrictionJob', :args => ['any args'])
expect(Resque::Job.reserve('restriction_normal')).to eq Resque::Job.new('restriction_normal', {'class' => 'OneHourRestrictionJob', 'args' => ['any args']})
expect(Resque::Job.reserve('restriction_normal')).to eq Resque::Job.new('restriction_normal', { 'class' => 'OneHourRestrictionJob', 'args' => ['any args'] })
expect(Resque::Job.reserve('restriction_normal')).to be_nil
expect(Resque::Job.reserve('normal')).to be_nil
end
Expand All @@ -16,13 +16,13 @@
Resque.redis.set(OneHourRestrictionJob.redis_key(:per_hour), -1)
Resque.push('restriction_normal', :class => 'OneHourRestrictionJob', :args => ['any args'])
expect(Resque::Job.reserve('restriction_normal')).to be_nil
expect(Resque.pop('restriction_normal')).to eq({'class' => 'OneHourRestrictionJob', 'args' => ['any args']})
expect(Resque.pop('restriction_normal')).to eq({ 'class' => 'OneHourRestrictionJob', 'args' => ['any args'] })
expect(Resque::Job.reserve('normal')).to be_nil
end

it "should not repush when reserve normal queue" do
Resque.push('normal', :class => 'OneHourRestrictionJob', :args => ['any args'])
expect(Resque::Job.reserve('normal')).to eq Resque::Job.new('normal', {'class' => 'OneHourRestrictionJob', 'args' => ['any args']})
expect(Resque::Job.reserve('normal')).to eq Resque::Job.new('normal', { 'class' => 'OneHourRestrictionJob', 'args' => ['any args'] })
expect(Resque::Job.reserve('normal')).to be_nil
expect(Resque::Job.reserve('restriction_normal')).to be_nil
end
Expand Down
22 changes: 11 additions & 11 deletions spec/resque/plugins/restriction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
context "redis_key" do
it "should get redis_key with different period" do
expect(MyJob.redis_key(:per_minute)).to eq "restriction:MyJob:#{Time.now.to_i / 60}"
expect(MyJob.redis_key(:per_hour)).to eq "restriction:MyJob:#{Time.now.to_i / (60*60)}"
expect(MyJob.redis_key(:per_day)).to eq "restriction:MyJob:#{Time.now.to_i / (24*60*60)}"
expect(MyJob.redis_key(:per_hour)).to eq "restriction:MyJob:#{Time.now.to_i / (60 * 60)}"
expect(MyJob.redis_key(:per_day)).to eq "restriction:MyJob:#{Time.now.to_i / (24 * 60 * 60)}"
expect(MyJob.redis_key(:per_month)).to eq "restriction:MyJob:#{Date.today.strftime("%Y-%m")}"
expect(MyJob.redis_key(:per_year)).to eq "restriction:MyJob:#{Date.today.year}"
expect(MyJob.redis_key(:per_minute_and_foo, 'foo' => 'bar')).to eq "restriction:MyJob:bar:#{Time.now.to_i / 60}"
Expand All @@ -25,11 +25,11 @@
context "seconds" do
it "should get seconds with different period" do
expect(MyJob.seconds(:per_minute)).to eq 60
expect(MyJob.seconds(:per_hour)).to eq 60*60
expect(MyJob.seconds(:per_day)).to eq 24*60*60
expect(MyJob.seconds(:per_week)).to eq 7*24*60*60
expect(MyJob.seconds(:per_month)).to eq 31*24*60*60
expect(MyJob.seconds(:per_year)).to eq 366*24*60*60
expect(MyJob.seconds(:per_hour)).to eq 60 * 60
expect(MyJob.seconds(:per_day)).to eq 24 * 60 * 60
expect(MyJob.seconds(:per_week)).to eq 7 * 24 * 60 * 60
expect(MyJob.seconds(:per_month)).to eq 31 * 24 * 60 * 60
expect(MyJob.seconds(:per_year)).to eq 366 * 24 * 60 * 60
expect(MyJob.seconds(:per_minute_and_foo)).to eq 60
end

Expand All @@ -42,10 +42,10 @@

context "settings" do
it "get correct number to restriction jobs" do
expect(OneDayRestrictionJob.settings).to eq({:per_day => 100})
expect(OneHourRestrictionJob.settings).to eq({:per_hour => 10})
expect(MultipleRestrictionJob.settings).to eq({:per_hour => 10, :per_300 => 2})
expect(MultiCallRestrictionJob.settings).to eq({:per_hour => 10, :per_300 => 2})
expect(OneDayRestrictionJob.settings).to eq({ :per_day => 100 })
expect(OneHourRestrictionJob.settings).to eq({ :per_hour => 10 })
expect(MultipleRestrictionJob.settings).to eq({ :per_hour => 10, :per_300 => 2 })
expect(MultiCallRestrictionJob.settings).to eq({ :per_hour => 10, :per_300 => 2 })
end
end

Expand Down