Browse files

allow specification of the job to retry

  • Loading branch information...
1 parent 0c675db commit 4507599c1dab07767bef14f5b5a870a2446f146a @freels committed Oct 5, 2010
Showing with 24 additions and 17 deletions.
  1. +15 −16 lib/kestrel/client/transactional.rb
  2. +9 −1 spec/kestrel/client/transactional_spec.rb
View
31 lib/kestrel/client/transactional.rb
@@ -4,22 +4,14 @@ class Kestrel::Client::Transactional < Kestrel::Client::Proxy
# multiple queues.
class MultipleQueueException < StandardError; end
-
class RetryableJob < Struct.new(:retries, :job); end
-
# Number of times to retry a job before giving up
DEFAULT_RETRIES = 100
-
# Pct. of the time during 'normal' processing we check the error queue first
ERROR_PROCESSING_RATE = 0.1
-
- # Maximum number of gets to execute before switching servers
- MAX_PER_SERVER = 100_000
-
-
# ==== Parameters
# client<Kestrel::Client>:: Client
# max_retries<Integer>:: Number of times to retry a job before
@@ -31,10 +23,9 @@ class RetryableJob < Struct.new(:retries, :job); end
# single server, before changing
# servers. Defaults to MAX_PER_SERVER
#
- def initialize(client, max_retries = nil, error_rate = nil, per_server = nil)
+ def initialize(client, max_retries = nil, error_rate = nil)
@max_retries = max_retries || DEFAULT_RETRIES
@error_rate = error_rate || ERROR_PROCESSING_RATE
- @per_server = per_server || MAX_PER_SERVER
@counter = 0 # Command counter
super(client)
end
@@ -77,17 +68,25 @@ def current_try
# ==== Returns
# Boolean:: true if the job is retryable, false otherwise
#
- def retry
- return unless @job
+ def retry(item = nil)
+ job =
+ if item
+ current_retries = (@job ? @job.retries : 0)
+ RetryableJob.new(current_retries, item)
+ else
+ @job
+ end
+
+ return unless job
- @job.retries += 1
+ job.retries += 1
- if should_retry = @job.retries < @max_retries
- client.set(current_queue + "_errors", @job)
+ if should_retry = job.retries < @max_retries
+ client.set(current_queue + "_errors", job)
end
# close the transaction on the original queue if this is the first retry
- close_transaction(@job.retries == 1 ? current_queue : "#{current_queue}_errors")
+ close_transaction(job.retries == 1 ? current_queue : "#{current_queue}_errors")
should_retry
end
View
10 spec/kestrel/client/transactional_spec.rb
@@ -4,7 +4,7 @@
describe "Instance Methods" do
before do
@raw_kestrel_client = Kestrel::Client.new(*Kestrel::Config.default)
- @kestrel = Kestrel::Client::Transactional.new(@raw_kestrel_client, nil, nil, 1)
+ @kestrel = Kestrel::Client::Transactional.new(@raw_kestrel_client)
stub(@kestrel).rand { 1 }
@queue = "some_queue"
end
@@ -128,6 +128,14 @@
@kestrel.retry.should be_true
end
+ it "allows specification of the job to retry" do
+ mock(@raw_kestrel_client).set(@queue + "_errors", anything) do |queue, job|
+ job.retries.should == 1
+ job.job.should == :revised_mcmuffin
+ end
+ @kestrel.retry(:revised_mcmuffin).should be_true
+ end
+
it "increments the retry count and re-enqueues the retried job" do
stub(@kestrel).rand { 0 }
stub(@raw_kestrel_client).get(@queue + "_errors", anything) do

0 comments on commit 4507599

Please sign in to comment.