Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kimoto committed Jul 12, 2009
1 parent d546e3d commit 842c856
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 0 deletions.
20 changes: 20 additions & 0 deletions LICENSE
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.
51 changes: 51 additions & 0 deletions Rakefile
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
4 changes: 4 additions & 0 deletions TODO
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
62 changes: 62 additions & 0 deletions lib/retry-handler.rb
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 added pkg/retry-handler-0.0.1.gem
Binary file not shown.
14 changes: 14 additions & 0 deletions script/destroy
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)
14 changes: 14 additions & 0 deletions script/generate
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)
7 changes: 7 additions & 0 deletions spec/retry-handler_spec.rb
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
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$TESTING=true
$:.push File.join(File.dirname(__FILE__), '..', 'lib')

0 comments on commit 842c856

Please sign in to comment.