allow multiple scribd strands

we don't want scribd jobs to run wild, but we don't necessarily want to
limit them to 1 at a time either.

test plan: upload attachments that are scribdable, and by default
they'll all be created on the strand "scribd". Increase the
"scribd_num_strands" Setting and they'll also be created on multiple
numbered strands like "scribd:2".

Change-Id: Id06400d903a7badf6def5d46c61505e4f5e50fcb
Reviewed-on: https://gerrit.instructure.com/11338
Tested-by: Jenkins <jenkins@instructure.com>
Reviewed-by: Cody Cutrer <cody@instructure.com>
This commit is contained in:
Brian Palmer 2012-06-05 17:47:25 -06:00
parent 3d67154ee1
commit ea43fa2ef6
3 changed files with 25 additions and 1 deletions

View File

@ -109,7 +109,7 @@ class Attachment < ActiveRecord::Base
# a no-op.)
self.process
elsif ScribdAPI.enabled? && !Attachment.skip_scribd_submits?
send_later_enqueue_args(:submit_to_scribd!, { :strand => 'scribd', :max_attempts => 1 })
send_later_enqueue_args(:submit_to_scribd!, { :n_strand => 'scribd', :max_attempts => 1 })
end
send_later(:infer_encoding) if self.encoding.nil? && self.content_type =~ /text/

View File

@ -34,6 +34,15 @@ module Delayed
options[:queue] ||= Delayed::Worker.queue
options[:max_attempts] ||= Delayed::Worker.max_attempts
options[:current_shard] = Shard.current
if options[:n_strand]
strand_name = options.delete(:n_strand)
num_strands = Setting.get_cached("#{strand_name}_num_strands", "1").to_i
strand_num = num_strands > 1 ? rand(num_strands) + 1 : 1
strand_name += ":#{strand_num}" if strand_num > 1
options[:strand] = strand_name
end
if options[:singleton]
options[:strand] = options.delete :singleton
self.transaction do

View File

@ -304,6 +304,21 @@ shared_examples_for 'a backend' do
@backend.get_and_lock_next_available('w1', 60).should be_nil
end
end
context 'n_strand' do
it "should default to 1" do
Delayed::Job.expects(:rand).never
job = Delayed::Job.enqueue(SimpleJob.new, :n_strand => 'njobs')
job.strand.should == "njobs"
end
it "should pick a strand randomly out of N" do
Setting.set("njobs_num_strands", "3")
Delayed::Job.expects(:rand).with(3).returns(1)
job = Delayed::Job.enqueue(SimpleJob.new, :n_strand => 'njobs')
job.strand.should == "njobs:2"
end
end
end
context "on hold" do