forked from lurker/retry-handler
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Copyright (c) 2009 YOUR NAME | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require 'rubygems' unless ENV['NO_RUBYGEMS'] | ||
require 'rake/gempackagetask' | ||
require 'rubygems/specification' | ||
require 'date' | ||
require 'spec/rake/spectask' | ||
|
||
spec = Gem::Specification.new do |s| | ||
s.name = "retry-handler" | ||
s.version = "0.0.1" | ||
s.author = "Takumi KIMOTO" | ||
s.email = "peerler@gmail.com" | ||
s.homepage = "http://twitter.com/lurker_" | ||
s.description = s.summary = "retry handler" | ||
|
||
s.platform = Gem::Platform::RUBY | ||
s.has_rdoc = true | ||
s.extra_rdoc_files = ["README", "LICENSE", 'TODO'] | ||
s.summary = "retry handler" | ||
|
||
# Uncomment this to add a dependency | ||
# s.add_dependency "foo" | ||
|
||
s.require_path = 'lib' | ||
s.autorequire = 'lib/retry-handler.rb' | ||
s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,spec}/**/*") | ||
end | ||
|
||
task :default => :spec | ||
|
||
desc "Run specs" | ||
Spec::Rake::SpecTask.new do |t| | ||
t.spec_files = FileList['spec/**/*_spec.rb'] | ||
t.spec_opts = %w(-fs --color) | ||
end | ||
|
||
|
||
Rake::GemPackageTask.new(spec) do |pkg| | ||
pkg.gem_spec = spec | ||
end | ||
|
||
desc "install the gem locally" | ||
task :install => [:package] do | ||
sh %{sudo gem install pkg/#{GEM}-#{GEM_VERSION}} | ||
end | ||
|
||
desc "create a gemspec file" | ||
task :make_spec do | ||
File.open("#{GEM}.gemspec", "w") do |file| | ||
file.puts spec.to_ruby | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
TODO: | ||
Fix LICENSE with your name | ||
Fix Rakefile with your name and contact info | ||
Add your code to lib/<%= name %>.rb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require 'timeout' | ||
require 'logger' | ||
|
||
class RetryError < StandardError; end | ||
class RetryOverError < RetryError; end | ||
|
||
module RetryHandler | ||
DEFAULT_MAX_RETRY = 3 | ||
DEFAULT_WAIT_TIME = 1 | ||
INFINITY_EXECUTE = nil | ||
|
||
def self.retry_handler(options={}, &block) | ||
max = options[:max] || DEFAULT_MAX_RETRY | ||
wait = options[:wait] || DEFAULT_WAIT_TIME | ||
timeout = options[:timeout] || INFINITY_EXECUTE | ||
exception = options[:accept_exception] || RetryError | ||
logger = options[:logger] || Logger.new(nil) | ||
|
||
_retry_handler(max, wait, exception, logger) do | ||
timeout(timeout, RetryError) do | ||
block.call | ||
end | ||
end | ||
end | ||
|
||
private | ||
def self._retry_handler(max_retry, wait_time, accept_exception, logger, &block) | ||
retry_cnt = 0 | ||
|
||
begin | ||
block.call | ||
rescue accept_exception => ex | ||
logger.error ex | ||
|
||
if retry_cnt >= max_retry | ||
logger.info "retry over error: #{max_retry}" | ||
raise RetryOverError.new, ex | ||
else | ||
retry_cnt += 1 | ||
logger.info "retry:#{retry_cnt}" | ||
logger.info "wait #{wait_time} sec" | ||
sleep wait_time | ||
retry | ||
end | ||
end | ||
end | ||
end | ||
|
||
class Proc | ||
def retry(options={}) | ||
RetryHandler.retry_handler(options){ | ||
self.call | ||
} | ||
end | ||
end | ||
|
||
module Kernel | ||
def retry_handler(options={}, &block) | ||
block.retry(options) if block_given? | ||
end | ||
end | ||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/usr/bin/env ruby | ||
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..')) | ||
|
||
begin | ||
require 'rubigen' | ||
rescue LoadError | ||
require 'rubygems' | ||
require 'rubigen' | ||
end | ||
require 'rubigen/scripts/destroy' | ||
|
||
ARGV.shift if ['--help', '-h'].include?(ARGV[0]) | ||
RubiGen::Base.use_component_sources! [:newgem_simple, :test_unit] | ||
RubiGen::Scripts::Destroy.new.run(ARGV) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/usr/bin/env ruby | ||
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..')) | ||
|
||
begin | ||
require 'rubigen' | ||
rescue LoadError | ||
require 'rubygems' | ||
require 'rubigen' | ||
end | ||
require 'rubigen/scripts/generate' | ||
|
||
ARGV.shift if ['--help', '-h'].include?(ARGV[0]) | ||
RubiGen::Base.use_component_sources! [:newgem_simple, :test_unit] | ||
RubiGen::Scripts::Generate.new.run(ARGV) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require File.dirname(__FILE__) + '/spec_helper' | ||
|
||
describe "retry-handler" do | ||
it "should do nothing" do | ||
true.should == true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
$TESTING=true | ||
$:.push File.join(File.dirname(__FILE__), '..', 'lib') |