Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ retry-handler自体の処理の流れを可視化したいときは:loggerオプ
begin
end.retry(:max => 3)

Proc はリトライ回数を引数に受け取ります。
Proc.new{ |retry_count|
warn "失敗 #{retry_count} 回目"
# なんかエラーの起こるコード
}.retry(:max => 3, :accept_exception => StandardError)


== Method#retryの使い方
たとえばFile.readをretryableにするコード例です
File.method(:read).retry("./not_found.txt", :accept_exception => StandardError, :logger => Logger.new(STDERR))
Expand Down
4 changes: 4 additions & 0 deletions Rakefile
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
#!/usr/bin/env rake
require "bundler/gem_tasks"
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new("spec")
task :default => :spec
20 changes: 12 additions & 8 deletions lib/retry-handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ def self.retry_handler(options={}, &block)
exception = options[:accept_exception] || RetryError
logger = options[:logger] || Logger.new(nil)

_retry_handler(max, wait, exception, logger) do
_retry_handler(max, wait, exception, logger) do |retry_cnt|
timeout(timeout, RetryError) do
block.call
block.call(retry_cnt)
end
end
end

private
def self._retry_handler(max_retry, wait_time, accept_exception, logger, &block)
retry_cnt = 0
retry_cnt = 0

begin
block.call
block.call(retry_cnt)
rescue accept_exception => ex
logger.error ex

Expand All @@ -48,17 +48,21 @@ def self._retry_handler(max_retry, wait_time, accept_exception, logger, &block)

class Proc
def retry(options={})
RetryHandler.retry_handler(options){
self.call
RetryHandler.retry_handler(options){ |retry_cnt|
self.call(retry_cnt)
}
end
end

class Method
def retry(*args)
options = args.last.is_a?(::Hash) ? args.pop : {}
RetryHandler.retry_handler(options) do
self.call(*args)
RetryHandler.retry_handler(options) do |retry_cnt|
if self.arity.zero?
self.call(*args)
else
self.call(*args, retry_cnt)
end
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions retry-handler.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ Gem::Specification.new do |gem|
gem.name = "retry-handler"
gem.require_paths = ["lib"]
gem.version = Retry::Handler::VERSION

gem.add_development_dependency "rspec"
end
70 changes: 66 additions & 4 deletions spec/retry-handler_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,69 @@
require File.dirname(__FILE__) + '/spec_helper'

describe "retry-handler" do
it "should do nothing" do
true.should == true
describe RetryHandler do
let(:retry_count) { 5 }
let(:wait) { 0.1 }
let(:exception) { StandardError }

describe '.retry_handler' do
it 'calls and retries given block' do
proc = Proc.new { }
proc.should_receive(:call).exactly(1 + retry_count).times.and_raise(exception)

expect {
RetryHandler.retry_handler({max: retry_count, wait: wait, accept_exception: exception}, &proc)
}.to raise_error(exception)
end

context 'given block takes argument' do
it 'passes retry count to given block' do
proc = Proc.new { }
proc.should_receive(:call).with do |retry_cnt|
retry_cnt.should be_a(Fixnum)
end

RetryHandler.retry_handler({max: retry_count, wait: wait, accept_exception: exception}, &proc)
end
end
end
end

describe Method do
let(:retry_count) { 5 }
let(:wait) { 0.1 }
let(:exception) { StandardError }

describe '#retry' do
context 'arity is 0' do
it 'does not pass retry count to given block' do
klass = Class.new do
define_method(:retriable) do
raise StandardError
end
end

method = klass.new.method(:retriable)
method.should_receive(:call).with(no_args())

method.retry(accept_exception: exception)
end
end

context 'arity larger than 0' do
it 'passes retry count to given block' do
klass = Class.new do
define_method(:retriable) do |retry_cnt, _|
raise StandardError
end
end

method = klass.new.method(:retriable)
method.should_receive(:call).with do |retry_cnt|
retry_cnt.should be_a(Fixnum)
end

method.retry(accept_exception: exception)
end
end
end
end
end
7 changes: 5 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
$TESTING=true
$:.push File.join(File.dirname(__FILE__), '..', 'lib')
require 'bundler/setup'
require 'retry-handler'

RSpec.configure do |config|
end