Skip to content
This repository has been archived by the owner on Jan 29, 2023. It is now read-only.

Commit

Permalink
keeps getting smaller
Browse files Browse the repository at this point in the history
  • Loading branch information
freels committed Oct 6, 2010
1 parent d433117 commit f0d032f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 25 deletions.
33 changes: 13 additions & 20 deletions lib/kestrel/client/transactional.rb
Expand Up @@ -44,9 +44,11 @@ def get(key, opts = {})

close_last_transaction

if job = client.get(normal_or_error_queue(key), opts.merge(:open => true))
queue = read_from_error_queue? ? key + "_errors" : key

if job = client.get(queue, opts.merge(:open => true))
@job = job.is_a?(RetryableJob) ? job : RetryableJob.new(0, job)
@current_is_retry = job.is_a? RetryableJob
@last_read_queue = queue
@current_queue = key
@job.job
end
Expand All @@ -56,6 +58,13 @@ def current_try
@job.retries + 1
end

def close_last_transaction #:nodoc:
return unless @last_read_queue

client.get_from_last(@last_read_queue + "/close")
@last_read_queue = @current_queue = @job = nil
end

# Enqueues the current job on the error queue for later
# retry. If the job has been retried DEFAULT_RETRIES times,
# gives up entirely.
Expand All @@ -65,8 +74,6 @@ def current_try
#
#
def retry(item = nil)
enqueued_retry = false

job =
if item
current_retries = (@job ? @job.retries : 0)
Expand All @@ -85,23 +92,9 @@ def retry(item = nil)
job.retries < @max_retries
end

def close_last_transaction #:nodoc:
return unless @job

queue_for_last_job =
if @current_is_retry
current_queue + "_errors"
else
current_queue
end

client.get_from_last("#{queue_for_last_job}/close")
@current_is_retry = @current_queue = @job = nil
end

private

def normal_or_error_queue(key)
(rand < @error_rate) ? key + "_errors" : key
def read_from_error_queue?
rand < @error_rate
end
end
10 changes: 5 additions & 5 deletions spec/kestrel/client/transactional_spec.rb
Expand Up @@ -217,15 +217,15 @@ def get_job
end
end

describe "#normal_or_error_queue" do
describe "#read_from_error_queue?" do
it "returns the error queue ERROR_PROCESSING_RATE pct. of the time" do
mock(@kestrel).rand { Kestrel::Client::Transactional::ERROR_PROCESSING_RATE - 0.05 }
@kestrel.send(:normal_or_error_queue, @queue).should == @queue + "_errors"
@kestrel.send(:read_from_error_queue?).should == true
end

it "returns the normal queue most of the time" do
mock(@kestrel).rand { Kestrel::Client::Transactional::ERROR_PROCESSING_RATE + 0.05 }
@kestrel.send(:normal_or_error_queue, @queue).should == @queue
@kestrel.send(:read_from_error_queue?).should == false
end
end

Expand All @@ -237,7 +237,7 @@ def get_job
end

it "closes the normal queue if the job was pulled off of the normal queue" do
mock(@kestrel).normal_or_error_queue(@queue) { @queue }
mock(@kestrel).read_from_error_queue? { false }
mock(@raw_kestrel_client).get(@queue, :open => true) { :mcguffin }
mock(@raw_kestrel_client).get_from_last(@queue + "/close")
mock(@raw_kestrel_client).get_from_last(@queue + "_errors/close").never
Expand All @@ -247,7 +247,7 @@ def get_job
end

it "closes the error queue if the job was pulled off of the error queue" do
mock(@kestrel).normal_or_error_queue(@queue) { @queue + "_errors" }
mock(@kestrel).read_from_error_queue? { true }
mock(@raw_kestrel_client).get(@queue + "_errors", anything) { Kestrel::Client::Transactional::RetryableJob.new 1, :mcguffin }
mock(@raw_kestrel_client).get_from_last(@queue + "/close").never
mock(@raw_kestrel_client).get_from_last(@queue + "_errors/close")
Expand Down

0 comments on commit f0d032f

Please sign in to comment.