undertaker-rails 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +52 -0
- data/Rakefile +1 -0
- data/lib/undertaker.rb +73 -0
- data/lib/undertaker/version.rb +3 -0
- data/spec/lib/undertaker_spec.rb +67 -0
- data/undertaker-rails.gemspec +29 -0
- metadata +124 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 89bb816806ea0dbd56b5483f3c931677f4ba4242
|
4
|
+
data.tar.gz: 10a1c9e62a33a4ac37e524c1714978d205af5392
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ea99e7436faf32584467c23fa0ea68cf97598eff22439581dcd9196822c7779a9c030bb628a857a3904f353eb77fc46f18be5fe6c7e7b371cea2097eef7d068a
|
7
|
+
data.tar.gz: 49a67ef0dc1b8f6c85694967a3881fe600f3cdccfb38346869ace8580a53b1b4f60f5a4dd13d412b9bcfd3dca39dc66b0e4b78a380bb3c9fb86dd20aa89d6c4f
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Brian LaMattina
|
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.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# Undertaker
|
2
|
+
|
3
|
+
Easy exponential back off.
|
4
|
+
|
5
|
+
Undertaker will run a block of code and if it raises an exception it will
|
6
|
+
retry.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'undertaker-rails'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install undertaker
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
### Retry any StandardError
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
undertaker = Undertaker.new(limit: 5, logger: Rails.logger)
|
28
|
+
|
29
|
+
undertaker.execute do
|
30
|
+
something...
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
### With a custom retry condition
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
undertaker = Undertaker.new
|
38
|
+
|
39
|
+
undertaker.retry_when { |exception| exception == SomeError }
|
40
|
+
|
41
|
+
undertaker.execute do
|
42
|
+
something...
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
## Contributing
|
47
|
+
|
48
|
+
1. Fork it ( http://github.com/<my-github-username>/undertaker/fork )
|
49
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
50
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
51
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
52
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/undertaker.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
require "undertaker/version"
|
2
|
+
require "active_support/logger"
|
3
|
+
|
4
|
+
module Undertaker
|
5
|
+
|
6
|
+
def self.new(*args)
|
7
|
+
Undertaker.new(*args)
|
8
|
+
end
|
9
|
+
|
10
|
+
class Undertaker
|
11
|
+
attr_accessor :attempts, :retry_condition
|
12
|
+
attr_reader :limit
|
13
|
+
|
14
|
+
def initialize(options = {})
|
15
|
+
@exceptions = []
|
16
|
+
@attempts = 0
|
17
|
+
@limit = options[:limit] || 10
|
18
|
+
@logger = options[:logger] || ActiveSupport::Logger.new(STDOUT)
|
19
|
+
end
|
20
|
+
|
21
|
+
def execute
|
22
|
+
setup_execution
|
23
|
+
yield
|
24
|
+
rescue StandardError => exception
|
25
|
+
add_exception exception
|
26
|
+
if should_retry?
|
27
|
+
setup_retry
|
28
|
+
retry
|
29
|
+
end
|
30
|
+
raise exception
|
31
|
+
end
|
32
|
+
|
33
|
+
def retry_when(&block)
|
34
|
+
self.retry_condition = block
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def setup_execution
|
40
|
+
self.attempts += 1
|
41
|
+
end
|
42
|
+
|
43
|
+
def setup_retry
|
44
|
+
log_retry
|
45
|
+
sleep exponential_backoff
|
46
|
+
end
|
47
|
+
|
48
|
+
def add_exception(exception)
|
49
|
+
@exceptions << exception
|
50
|
+
end
|
51
|
+
|
52
|
+
def last_exception
|
53
|
+
@exceptions.last
|
54
|
+
end
|
55
|
+
|
56
|
+
def should_retry?
|
57
|
+
attempts < limit && retry_condition?
|
58
|
+
end
|
59
|
+
|
60
|
+
def retry_condition?
|
61
|
+
retry_condition.nil? || retry_condition.call(last_exception)
|
62
|
+
end
|
63
|
+
|
64
|
+
def exponential_backoff
|
65
|
+
(1.0 / 2.0 * (2.0**attempts - 1.0)).ceil
|
66
|
+
end
|
67
|
+
|
68
|
+
def log_retry
|
69
|
+
@logger.warn "Undertaker: #{last_exception} raised retring..."
|
70
|
+
@logger.warn last_exception.backtrace[0..5].join("\n")
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'pry'
|
2
|
+
require 'benchmark'
|
3
|
+
require 'undertaker'
|
4
|
+
|
5
|
+
describe Undertaker, 'configuration' do
|
6
|
+
|
7
|
+
it 'allows the limit and logger to be configured' do
|
8
|
+
undertaker = Undertaker.new(limit: 1, logger: "Logger")
|
9
|
+
|
10
|
+
expect(undertaker.instance_variable_get(:@limit)).to eq 1
|
11
|
+
expect(undertaker.instance_variable_get(:@logger)).to eq "Logger"
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'allows configuration of the retry configuration' do
|
15
|
+
undertaker = Undertaker.new(limit: 2)
|
16
|
+
test_attempts = 0
|
17
|
+
undertaker.retry_when do |exception|
|
18
|
+
exception == NameError
|
19
|
+
end
|
20
|
+
|
21
|
+
expect do
|
22
|
+
undertaker.execute do
|
23
|
+
test_attempts += 1
|
24
|
+
raise RangeError
|
25
|
+
end
|
26
|
+
end.to raise_error(RangeError)
|
27
|
+
|
28
|
+
expect(test_attempts).to eq 1
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe Undertaker, '#execute' do
|
33
|
+
it 'executes a block' do
|
34
|
+
x = 5
|
35
|
+
|
36
|
+
Undertaker.new.execute do
|
37
|
+
x = 10
|
38
|
+
end
|
39
|
+
|
40
|
+
expect(x).to be 10
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'retries on failure until the limit is hit' do
|
44
|
+
test_attempts = 0
|
45
|
+
|
46
|
+
expect do
|
47
|
+
Undertaker.new(limit: 3).execute do
|
48
|
+
test_attempts += 1
|
49
|
+
raise NameError
|
50
|
+
end
|
51
|
+
end.to raise_error(NameError)
|
52
|
+
|
53
|
+
expect(test_attempts).to eq 3
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'backs off exponentially on failure' do
|
57
|
+
duration = Benchmark.measure do
|
58
|
+
begin
|
59
|
+
Undertaker.new(limit: 3).execute do
|
60
|
+
raise StandardError
|
61
|
+
end
|
62
|
+
rescue StandardError => e
|
63
|
+
end
|
64
|
+
end
|
65
|
+
expect(duration.real).to be >= 2
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'undertaker/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "undertaker-rails"
|
8
|
+
spec.version = Undertaker::VERSION
|
9
|
+
spec.authors = ["Brian LaMattina"]
|
10
|
+
spec.email = ["brianlamattina@gmail.com"]
|
11
|
+
spec.summary = %q{Easy exponential back off}
|
12
|
+
spec.description = %q{Easy exponential back off}
|
13
|
+
spec.homepage = "https://github.com/blamattina/undertaker"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 1.9.2")
|
22
|
+
|
23
|
+
spec.add_dependency("activesupport", ">= 4.0.0")
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
26
|
+
spec.add_development_dependency "rake"
|
27
|
+
spec.add_development_dependency "rspec", "~> 2.14.0"
|
28
|
+
spec.add_development_dependency "pry", "~> 0.9.12"
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: undertaker-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brian LaMattina
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-02-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.5'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.5'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.14.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 2.14.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.9.12
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.9.12
|
83
|
+
description: Easy exponential back off
|
84
|
+
email:
|
85
|
+
- brianlamattina@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- .gitignore
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- lib/undertaker.rb
|
96
|
+
- lib/undertaker/version.rb
|
97
|
+
- spec/lib/undertaker_spec.rb
|
98
|
+
- undertaker-rails.gemspec
|
99
|
+
homepage: https://github.com/blamattina/undertaker
|
100
|
+
licenses:
|
101
|
+
- MIT
|
102
|
+
metadata: {}
|
103
|
+
post_install_message:
|
104
|
+
rdoc_options: []
|
105
|
+
require_paths:
|
106
|
+
- lib
|
107
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - '>='
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: 1.9.2
|
112
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - '>='
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
requirements: []
|
118
|
+
rubyforge_project:
|
119
|
+
rubygems_version: 2.0.14
|
120
|
+
signing_key:
|
121
|
+
specification_version: 4
|
122
|
+
summary: Easy exponential back off
|
123
|
+
test_files:
|
124
|
+
- spec/lib/undertaker_spec.rb
|