Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

raise an exception if retry is called when no transaction is open

  • Loading branch information...
commit b155e1f62ccf760b31e60afeb81865fca633c68c 1 parent fafc3b6
@freels authored
View
28 lib/kestrel/client/transactional.rb
@@ -4,10 +4,14 @@ class Kestrel::Client::Transactional < Kestrel::Client::Proxy
# multiple queues.
class MultipleQueueException < StandardError; end
+ # Raised when a caller attempts to retry a job if
+ # there is no current open transaction
+ class NoOpenTransaction < StandardError; end
+
class RetryableJob < Struct.new(:retries, :job); end
# Number of times to retry a job before giving up
- DEFAULT_RETRIES = 100
+ DEFAULT_RETRIES = 10
# Pct. of the time during 'normal' processing we check the error queue first
ERROR_PROCESSING_RATE = 0.1
@@ -39,6 +43,9 @@ def initialize(client, max_retries = nil, error_rate = nil)
# ==== Returns
# Job, possibly retryable, or nil
#
+ # ==== Raises
+ # MultipleQueueException
+ #
def get(key, opts = {})
raise MultipleQueueException if current_queue && key != current_queue
@@ -69,20 +76,21 @@ def close_last_transaction #:nodoc:
# retry. If the job has been retried DEFAULT_RETRIES times,
# gives up entirely.
#
+ # ==== Parameters
+ # item (optional):: if specified, the job set to the error
+ # queue with the given payload instead of what
+ # was originally fetched.
+ #
# ==== Returns
# Boolean:: true if the job is enqueued in the retry queue, false otherwise
#
+ # ==== Raises
+ # NoOpenTransaction
#
def retry(item = nil)
- job =
- if item
- current_retries = (@job ? @job.retries : 0)
- RetryableJob.new(current_retries, item)
- else
- @job.dup
- end
-
- return unless job
+ raise NoOpenTransaction unless @last_read_queue
+
+ job = item ? RetryableJob.new(@job.retries, item) : @job.dup
job.retries += 1
View
13 spec/kestrel/client/transactional_spec.rb
@@ -18,6 +18,7 @@ def get_job
returns = [:mcguffin]
stub(@raw_kestrel_client).get(@queue, anything) { returns.shift }
stub(@raw_kestrel_client).get(@queue + "_errors", anything)
+
mock(@raw_kestrel_client).get_from_last(@queue + "/close")
get_job.should == :mcguffin
@@ -29,6 +30,7 @@ def get_job
returns = [Kestrel::Client::Transactional::RetryableJob.new(1, :mcguffin)]
stub(@raw_kestrel_client).get(@queue + "_errors", anything) { returns.shift }
stub(@raw_kestrel_client).get(@queue, anything)
+
mock(@raw_kestrel_client).get_from_last(@queue + "_errors/close")
get_job.should == :mcguffin
@@ -40,6 +42,7 @@ def get_job
returns = [:mcguffin]
stub(@raw_kestrel_client).get(@queue, anything) { returns.shift }
stub(@raw_kestrel_client).get(@queue + "_errors", anything)
+
mock(@raw_kestrel_client).set(@queue + "_errors", anything) do |q,j|
j.retries.should == 1
j.job.should == :mcguffin
@@ -155,6 +158,16 @@ def get_job
@kestrel.get(@queue)
end
+ it "raises an exception if called when there is no open transaction" do
+ @kestrel.close_last_transaction
+ lambda { @kestrel.retry }.should raise_error(Kestrel::Client::Transactional::NoOpenTransaction)
+ end
+
+ it "raises an exception if retry has already been called" do
+ @kestrel.retry
+ lambda { @kestrel.retry }.should raise_error(Kestrel::Client::Transactional::NoOpenTransaction)
+ end
+
it "enqueues a fresh failed job to the errors queue with a retry count" do
mock(@raw_kestrel_client).set(@queue + "_errors", anything) do |queue, job|
job.retries.should == 1
Please sign in to comment.
Something went wrong with that request. Please try again.