exception_helper 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +17 -0
- data/Gemfile +13 -0
- data/LICENSE.txt +22 -0
- data/README.rdoc +19 -0
- data/Rakefile +12 -0
- data/VERSION +1 -0
- data/config/log4r.yml +24 -0
- data/exception_helper.gemspec +28 -0
- data/lib/exception_helper.rb +5 -0
- data/lib/exception_helper/retry.rb +71 -0
- data/lib/exception_helper/version.rb +3 -0
- data/log/.gitkeep +0 -0
- data/test/helper.rb +22 -0
- data/test/retry_test.rb +112 -0
- metadata +145 -0
data/.document
ADDED
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source 'http://rubygems.org'
|
2
|
+
|
3
|
+
gem 'activesupport', '>= 2.3.5'
|
4
|
+
|
5
|
+
# Add dependencies to develop your gem here.
|
6
|
+
# Include everything needed to run rake, tests, features, etc.
|
7
|
+
group :development do
|
8
|
+
gem 'rake'
|
9
|
+
gem 'shoulda'
|
10
|
+
gem 'bundler'
|
11
|
+
gem 'mocha'
|
12
|
+
gem 'log4r' #used for null logger
|
13
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Backupify
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= exception_helper
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Contributing to exception_helper
|
6
|
+
|
7
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
8
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
9
|
+
* Fork the project.
|
10
|
+
* Start a feature/bugfix branch.
|
11
|
+
* Commit and push until you are happy with your contribution.
|
12
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
13
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2012 Jason Haruska. See LICENSE.txt for
|
18
|
+
further details.
|
19
|
+
|
data/Rakefile
ADDED
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.1
|
data/config/log4r.yml
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
log4r_config:
|
2
|
+
# define all loggers ...
|
3
|
+
loggers:
|
4
|
+
- name : test
|
5
|
+
level : DEBUG
|
6
|
+
trace : 'true'
|
7
|
+
outputters :
|
8
|
+
- datefile
|
9
|
+
- name : development
|
10
|
+
level : DEBUG
|
11
|
+
trace : 'true'
|
12
|
+
outputters :
|
13
|
+
- datefile
|
14
|
+
|
15
|
+
# define all outputters (incl. formatters)
|
16
|
+
outputters:
|
17
|
+
- type: DateFileOutputter
|
18
|
+
name: datefile
|
19
|
+
dirname: "log"
|
20
|
+
file: "my_log" # this will be overrided by lumber config.
|
21
|
+
formatter:
|
22
|
+
date_pattern: '%H:%M:%S'
|
23
|
+
pattern : '%d %l: %m '
|
24
|
+
type : PatternFormatter
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'exception_helper/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "exception_helper"
|
8
|
+
gem.version = ExceptionHelper::VERSION
|
9
|
+
gem.authors = ["Jason Haruska"]
|
10
|
+
gem.email = ["jason@haruska.com"]
|
11
|
+
gem.description = %q{Common mixins for handling exceptions}
|
12
|
+
gem.summary = %q{Common mixins for handling exceptions including retries.}
|
13
|
+
gem.homepage = "https://github.com/backupify/exception_helper"
|
14
|
+
gem.license = "MIT"
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split($/)
|
17
|
+
gem.executables = gem.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
|
+
gem.require_paths = ["lib"]
|
20
|
+
|
21
|
+
gem.add_development_dependency "rake"
|
22
|
+
gem.add_development_dependency "shoulda"
|
23
|
+
gem.add_development_dependency "bundler"
|
24
|
+
gem.add_development_dependency "mocha"
|
25
|
+
gem.add_development_dependency "log4r"
|
26
|
+
end
|
27
|
+
|
28
|
+
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
require 'active_support/core_ext/module/aliasing'
|
3
|
+
|
4
|
+
module ExceptionHelper
|
5
|
+
module Retry
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
unless defined?(logger)
|
10
|
+
require 'log4r'
|
11
|
+
|
12
|
+
#always return the null logger
|
13
|
+
def self.logger
|
14
|
+
Log4r::Logger.root
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def retry_on_failure(*exception_list, &block)
|
20
|
+
self.class.retry_on_failure(*exception_list, &block)
|
21
|
+
end
|
22
|
+
|
23
|
+
def wrap_with_retry(*methods)
|
24
|
+
self.class.wrap_with_retry(*methods)
|
25
|
+
end
|
26
|
+
|
27
|
+
module ClassMethods
|
28
|
+
# execute the given block, retrying only when one of the given exceptions is raised
|
29
|
+
def retry_on_failure(*exception_list, &block)
|
30
|
+
opts = exception_list.last.is_a?(Hash) ? exception_list.pop : {}
|
31
|
+
opts = {:retry_count => 3}.merge(opts)
|
32
|
+
retry_count = opts[:retry_count]
|
33
|
+
begin
|
34
|
+
yield block
|
35
|
+
rescue *exception_list => e
|
36
|
+
if retry_count > 0
|
37
|
+
retry_count -= 1
|
38
|
+
logger.info "Exception, trying again #{retry_count} more times"
|
39
|
+
sleep opts[:retry_sleep].to_f if opts[:retry_sleep]
|
40
|
+
retry
|
41
|
+
else
|
42
|
+
logger.error "Too many exceptions...re-raising"
|
43
|
+
raise
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# Wraps a method with `retry_on_failure` to handle exceptions. It does this via alias_method_chain,
|
49
|
+
# so the original method name will be preserved and the method will simply be decorated with the retry logic.
|
50
|
+
def wrap_with_retry(*methods)
|
51
|
+
opts = methods.last.is_a?(Hash) ? methods.pop : {}
|
52
|
+
exception_list = opts.delete(:exceptions)
|
53
|
+
methods.each do |method|
|
54
|
+
# Extract the punctuation character from the method name if one exists.
|
55
|
+
aliased_method, punctuation = method.to_s.sub(/([?!=])$/, ''), $1
|
56
|
+
|
57
|
+
# Create the decorated method that `alias_method_chain` will look for.
|
58
|
+
define_method("#{aliased_method}_with_retry#{punctuation}") do |*args|
|
59
|
+
retry_on_failure(*exception_list, opts) do
|
60
|
+
send("#{aliased_method}_without_retry#{punctuation}", *args)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# Set up the method chain.
|
65
|
+
alias_method_chain method, :retry
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
data/log/.gitkeep
ADDED
File without changes
|
data/test/helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
|
4
|
+
begin
|
5
|
+
Bundler.setup(:default, :development)
|
6
|
+
rescue Bundler::BundlerError => e
|
7
|
+
$stderr.puts e.message
|
8
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
9
|
+
exit e.status_code
|
10
|
+
end
|
11
|
+
|
12
|
+
require 'test/unit'
|
13
|
+
require 'shoulda'
|
14
|
+
require 'mocha/setup'
|
15
|
+
|
16
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
17
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
18
|
+
|
19
|
+
require 'exception_helper'
|
20
|
+
|
21
|
+
class Test::Unit::TestCase
|
22
|
+
end
|
data/test/retry_test.rb
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/helper.rb')
|
2
|
+
|
3
|
+
class RetryTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
include ExceptionHelper::Retry
|
6
|
+
|
7
|
+
class TestException1 < Exception
|
8
|
+
end
|
9
|
+
|
10
|
+
class TestException2 < Exception
|
11
|
+
end
|
12
|
+
|
13
|
+
should "have no impact on passing code" do
|
14
|
+
expects(:puts).with("hello").once
|
15
|
+
retry_on_failure(TestException1) do
|
16
|
+
puts "hello"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
should "retry for specified exceptions" do
|
21
|
+
expects(:puts).with("hello").at_least(2)
|
22
|
+
assert_raise(TestException1) do
|
23
|
+
retry_on_failure(TestException1) do
|
24
|
+
puts "hello"
|
25
|
+
raise TestException1.new
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
should "retry by given count for specified exceptions" do
|
31
|
+
expects(:puts).with("hello").times(6)
|
32
|
+
assert_raise(TestException1) do
|
33
|
+
retry_on_failure(TestException1, TestException2, :retry_count => 5) do
|
34
|
+
puts "hello"
|
35
|
+
raise TestException1.new
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
should "not retry for unspecified exceptions" do
|
41
|
+
expects(:puts).with("hello").once
|
42
|
+
assert_raise(TestException1) do
|
43
|
+
retry_on_failure(TestException2) do
|
44
|
+
puts "hello"
|
45
|
+
raise TestException1.new
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
should "sleep if retry_sleep given" do
|
51
|
+
RetryTest.expects(:sleep).with(1)
|
52
|
+
assert_raise(TestException1) do
|
53
|
+
retry_on_failure(TestException1, :retry_count => 1, :retry_sleep => 1) do
|
54
|
+
raise TestException1.new
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
should "not sleep if retry_sleep not given" do
|
60
|
+
RetryTest.expects(:sleep).never
|
61
|
+
assert_raise(TestException1) do
|
62
|
+
retry_on_failure(TestException1, :retry_count => 1) do
|
63
|
+
raise TestException1.new
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
should "wrap method with retry" do
|
69
|
+
class WithRetry
|
70
|
+
include ExceptionHelper::Retry
|
71
|
+
|
72
|
+
def mymethod
|
73
|
+
true
|
74
|
+
end
|
75
|
+
|
76
|
+
wrap_with_retry :mymethod
|
77
|
+
end
|
78
|
+
|
79
|
+
WithRetry.stubs(:mymethod_without_retry).raises(Exception).then.returns(true)
|
80
|
+
|
81
|
+
# We should handle the exceptions. retry, and then move on without raising any further exceptions.
|
82
|
+
WithRetry.new.mymethod
|
83
|
+
end
|
84
|
+
|
85
|
+
should "wrap method with retry with options" do
|
86
|
+
class WithRetry
|
87
|
+
include ExceptionHelper::Retry
|
88
|
+
|
89
|
+
def mymethod
|
90
|
+
true
|
91
|
+
end
|
92
|
+
|
93
|
+
wrap_with_retry :mymethod, :exceptions => [TestException1, TestException2], :retry_count => 9, :retry_sleep => 8
|
94
|
+
end
|
95
|
+
|
96
|
+
WithRetry.any_instance.expects(:retry_on_failure).with(TestException1, TestException2, {:retry_count => 9, :retry_sleep => 8})
|
97
|
+
|
98
|
+
WithRetry.new.mymethod
|
99
|
+
end
|
100
|
+
|
101
|
+
should "not override logger on include" do
|
102
|
+
class WithLogger
|
103
|
+
def self.logger
|
104
|
+
"Doesn't really do much"
|
105
|
+
end
|
106
|
+
|
107
|
+
include ExceptionHelper::Retry
|
108
|
+
end
|
109
|
+
|
110
|
+
assert_equal "Doesn't really do much", WithLogger.logger
|
111
|
+
end
|
112
|
+
end
|
metadata
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: exception_helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jason Haruska
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-09-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
type: :development
|
16
|
+
name: rake
|
17
|
+
prerelease: false
|
18
|
+
requirement: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
23
|
+
none: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - ! '>='
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: '0'
|
29
|
+
none: false
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
type: :development
|
32
|
+
name: shoulda
|
33
|
+
prerelease: false
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
none: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
none: false
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
type: :development
|
48
|
+
name: bundler
|
49
|
+
prerelease: false
|
50
|
+
requirement: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
none: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
none: false
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
type: :development
|
64
|
+
name: mocha
|
65
|
+
prerelease: false
|
66
|
+
requirement: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
none: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
none: false
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
type: :development
|
80
|
+
name: log4r
|
81
|
+
prerelease: false
|
82
|
+
requirement: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
none: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ! '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
none: false
|
94
|
+
description: Common mixins for handling exceptions
|
95
|
+
email:
|
96
|
+
- jason@haruska.com
|
97
|
+
executables: []
|
98
|
+
extensions: []
|
99
|
+
extra_rdoc_files: []
|
100
|
+
files:
|
101
|
+
- .document
|
102
|
+
- .gitignore
|
103
|
+
- Gemfile
|
104
|
+
- Gemfile.lock
|
105
|
+
- LICENSE.txt
|
106
|
+
- README.rdoc
|
107
|
+
- Rakefile
|
108
|
+
- VERSION
|
109
|
+
- config/log4r.yml
|
110
|
+
- exception_helper.gemspec
|
111
|
+
- lib/exception_helper.rb
|
112
|
+
- lib/exception_helper/retry.rb
|
113
|
+
- lib/exception_helper/version.rb
|
114
|
+
- log/.gitkeep
|
115
|
+
- test/helper.rb
|
116
|
+
- test/retry_test.rb
|
117
|
+
homepage: https://github.com/backupify/exception_helper
|
118
|
+
licenses:
|
119
|
+
- MIT
|
120
|
+
post_install_message:
|
121
|
+
rdoc_options: []
|
122
|
+
require_paths:
|
123
|
+
- lib
|
124
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ! '>='
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
none: false
|
130
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - ! '>='
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
none: false
|
136
|
+
requirements: []
|
137
|
+
rubyforge_project:
|
138
|
+
rubygems_version: 1.8.23
|
139
|
+
signing_key:
|
140
|
+
specification_version: 3
|
141
|
+
summary: Common mixins for handling exceptions including retries.
|
142
|
+
test_files:
|
143
|
+
- test/helper.rb
|
144
|
+
- test/retry_test.rb
|
145
|
+
has_rdoc:
|