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

Commit

Permalink
raise an exception if retry is called when no transaction is open
Browse files Browse the repository at this point in the history
  • Loading branch information
freels committed Oct 7, 2010
1 parent fafc3b6 commit b155e1f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
28 changes: 18 additions & 10 deletions lib/kestrel/client/transactional.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
13 changes: 13 additions & 0 deletions spec/kestrel/client/transactional_spec.rb
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b155e1f

Please sign in to comment.