lurker-retry-handler 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 YOUR NAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,3 @@
1
+ == retry-handler
2
+
3
+ A gem that provides...
data/Rakefile ADDED
@@ -0,0 +1,51 @@
1
+ require 'rubygems' unless ENV['NO_RUBYGEMS']
2
+ require 'rake/gempackagetask'
3
+ require 'rubygems/specification'
4
+ require 'date'
5
+ require 'spec/rake/spectask'
6
+
7
+ spec = Gem::Specification.new do |s|
8
+ s.name = "retry-handler"
9
+ s.version = "0.0.1"
10
+ s.author = "Takumi KIMOTO"
11
+ s.email = "peerler@gmail.com"
12
+ s.homepage = "http://twitter.com/lurker_"
13
+ s.description = s.summary = "retry handler"
14
+
15
+ s.platform = Gem::Platform::RUBY
16
+ s.has_rdoc = true
17
+ s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
18
+ s.summary = "retry handler"
19
+
20
+ # Uncomment this to add a dependency
21
+ # s.add_dependency "foo"
22
+
23
+ s.require_path = 'lib'
24
+ s.autorequire = 'lib/retry-handler.rb'
25
+ s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,spec}/**/*")
26
+ end
27
+
28
+ task :default => :spec
29
+
30
+ desc "Run specs"
31
+ Spec::Rake::SpecTask.new do |t|
32
+ t.spec_files = FileList['spec/**/*_spec.rb']
33
+ t.spec_opts = %w(-fs --color)
34
+ end
35
+
36
+
37
+ Rake::GemPackageTask.new(spec) do |pkg|
38
+ pkg.gem_spec = spec
39
+ end
40
+
41
+ desc "install the gem locally"
42
+ task :install => [:package] do
43
+ sh %{sudo gem install pkg/#{GEM}-#{GEM_VERSION}}
44
+ end
45
+
46
+ desc "create a gemspec file"
47
+ task :make_spec do
48
+ File.open("#{GEM}.gemspec", "w") do |file|
49
+ file.puts spec.to_ruby
50
+ end
51
+ end
data/TODO ADDED
@@ -0,0 +1,4 @@
1
+ TODO:
2
+ Fix LICENSE with your name
3
+ Fix Rakefile with your name and contact info
4
+ Add your code to lib/<%= name %>.rb
@@ -0,0 +1,62 @@
1
+ require 'timeout'
2
+ require 'logger'
3
+
4
+ class RetryError < StandardError; end
5
+ class RetryOverError < RetryError; end
6
+
7
+ module RetryHandler
8
+ DEFAULT_MAX_RETRY = 3
9
+ DEFAULT_WAIT_TIME = 1
10
+ INFINITY_EXECUTE = nil
11
+
12
+ def self.retry_handler(options={}, &block)
13
+ max = options[:max] || DEFAULT_MAX_RETRY
14
+ wait = options[:wait] || DEFAULT_WAIT_TIME
15
+ timeout = options[:timeout] || INFINITY_EXECUTE
16
+ exception = options[:accept_exception] || RetryError
17
+ logger = options[:logger] || Logger.new(nil)
18
+
19
+ _retry_handler(max, wait, exception, logger) do
20
+ timeout(timeout, RetryError) do
21
+ block.call
22
+ end
23
+ end
24
+ end
25
+
26
+ private
27
+ def self._retry_handler(max_retry, wait_time, accept_exception, logger, &block)
28
+ retry_cnt = 0
29
+
30
+ begin
31
+ block.call
32
+ rescue accept_exception => ex
33
+ logger.error ex
34
+
35
+ if retry_cnt >= max_retry
36
+ logger.info "retry over error: #{max_retry}"
37
+ raise RetryOverError.new, ex
38
+ else
39
+ retry_cnt += 1
40
+ logger.info "retry:#{retry_cnt}"
41
+ logger.info "wait #{wait_time} sec"
42
+ sleep wait_time
43
+ retry
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ class Proc
50
+ def retry(options={})
51
+ RetryHandler.retry_handler(options){
52
+ self.call
53
+ }
54
+ end
55
+ end
56
+
57
+ module Kernel
58
+ def retry_handler(options={}, &block)
59
+ block.retry(options) if block_given?
60
+ end
61
+ end
62
+
@@ -0,0 +1,7 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe "retry-handler" do
4
+ it "should do nothing" do
5
+ true.should == true
6
+ end
7
+ end
@@ -0,0 +1,2 @@
1
+ $TESTING=true
2
+ $:.push File.join(File.dirname(__FILE__), '..', 'lib')
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lurker-retry-handler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Takumi KIMOTO
8
+ autorequire: lib/retry-handler.rb
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-13 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: retry handler
17
+ email: peerler@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ - LICENSE
25
+ - TODO
26
+ files:
27
+ - LICENSE
28
+ - README
29
+ - Rakefile
30
+ - TODO
31
+ - lib/retry-handler.rb
32
+ - spec/retry-handler_spec.rb
33
+ - spec/spec_helper.rb
34
+ has_rdoc: false
35
+ homepage: http://twitter.com/lurker_
36
+ post_install_message:
37
+ rdoc_options: []
38
+
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ requirements: []
54
+
55
+ rubyforge_project:
56
+ rubygems_version: 1.2.0
57
+ signing_key:
58
+ specification_version: 3
59
+ summary: retry handler
60
+ test_files: []
61
+