Skip to content
Browse files

keeps getting smaller

  • Loading branch information...
1 parent d433117 commit f0d032f015b7073ca7a5ba1721bbb1d8819f188f @freels committed Oct 6, 2010
Showing with 18 additions and 25 deletions.
  1. +13 −20 lib/kestrel/client/transactional.rb
  2. +5 −5 spec/kestrel/client/transactional_spec.rb
View
33 lib/kestrel/client/transactional.rb
@@ -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
@@ -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.
@@ -65,8 +74,6 @@ def current_try
#
#
def retry(item = nil)
- enqueued_retry = false
-
job =
if item
current_retries = (@job ? @job.retries : 0)
@@ -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
View
10 spec/kestrel/client/transactional_spec.rb
@@ -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
@@ -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
@@ -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")

0 comments on commit f0d032f

Please sign in to comment.
Something went wrong with that request. Please try again.