perfect_retry 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +102 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/perfect_retry/config.rb +21 -0
- data/lib/perfect_retry/version.rb +3 -0
- data/lib/perfect_retry.rb +87 -0
- data/perfect_retry.gemspec +26 -0
- metadata +113 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: df42efb005b014fff2b0f07ecd946275f6b1a5aa
|
4
|
+
data.tar.gz: 35c9efee15aa976457560695596c2cd81384b7b0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ba546819efc99ae687d3e8903caeb6e57f8275b3dbe26d5579c89993a296295140479b25c94813a03b0438afff3fca865b5c754592279630f354a0b7ff66998a
|
7
|
+
data.tar.gz: 208cbaf2b5526cbaacb424d28ecd567054135bdf25ae6df5125df2f85fcc2e9c5054d9ace4db7bab05896c5ecbd9be84f420aa442059e0ec87bd56bf3ee7c771
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- 1.9.3
|
4
|
+
- 2.0.0
|
5
|
+
- 2.1
|
6
|
+
- 2.2.3
|
7
|
+
- jruby-19mode
|
8
|
+
- jruby-9.0.0.0
|
9
|
+
- jruby-head
|
10
|
+
- ruby-head
|
11
|
+
|
12
|
+
sudo: false
|
13
|
+
|
14
|
+
addons:
|
15
|
+
code_climate:
|
16
|
+
repo_token: 0ddf2a14a01d14b70843d9ad37ce4e7f1ff7edd7c08aec35d5d830299342850d
|
17
|
+
|
18
|
+
before_install: gem install bundler -v 1.10.6
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 uu59
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/uu59/perfect_retry.svg?branch=master)](https://travis-ci.org/uu59/perfect_retry)
|
2
|
+
[![Code Climate](https://codeclimate.com/github/uu59/perfect_retry/badges/gpa.svg)](https://codeclimate.com/github/uu59/perfect_retry)
|
3
|
+
[![Test Coverage](https://codeclimate.com/github/uu59/perfect_retry/badges/coverage.svg)](https://codeclimate.com/github/uu59/perfect_retry/coverage)
|
4
|
+
|
5
|
+
# PerfectRetry
|
6
|
+
|
7
|
+
Implement to handle retry kit.
|
8
|
+
|
9
|
+
## Usage and Config
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
PerfectRetry.with_retry do
|
13
|
+
do_something_have_possibilities_errors_task()
|
14
|
+
end
|
15
|
+
```
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
# in setup.rb
|
19
|
+
|
20
|
+
require "timeout"
|
21
|
+
|
22
|
+
PerfectRetry.register(:timeout_handling) do |config|
|
23
|
+
# Try 4 times retry.
|
24
|
+
# default: 3
|
25
|
+
config.limit = 4
|
26
|
+
|
27
|
+
# Rescue these error in a block.
|
28
|
+
# default: [StandardError]
|
29
|
+
config.rescues = [Timeout::Error, StandardError]
|
30
|
+
|
31
|
+
# Sleep this seconds before next retry. `n` is a retry times (1-origin).
|
32
|
+
# default: proc{|n| n ** 2}
|
33
|
+
config.sleep = proc{|n| n * 5 }
|
34
|
+
|
35
|
+
# Logger for something information e.g. '[2/5] Retrying after 3 seconds blah blah'.
|
36
|
+
# default: Logger.new(STDERR)
|
37
|
+
config.logger = Logger.new("/var/log/agent.log")
|
38
|
+
|
39
|
+
# Ensure block. Call this block after with_retry block finished with and without any errors.
|
40
|
+
# default: proc {}
|
41
|
+
config.ensure = proc { puts "finished" }
|
42
|
+
end
|
43
|
+
|
44
|
+
# in main.rb
|
45
|
+
|
46
|
+
require "open-uri"
|
47
|
+
|
48
|
+
PerfectRetry.new(:timeout_handling).with_retry do
|
49
|
+
open("http://example.com")
|
50
|
+
end
|
51
|
+
|
52
|
+
# or
|
53
|
+
|
54
|
+
PerfectRetry.with_retry(:timeout_handling) do
|
55
|
+
open("http://example.com")
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
### Manually retry
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
PerfectRetry.register(:dont_retry_automatically) do |config|
|
63
|
+
config.limit = 0
|
64
|
+
end
|
65
|
+
|
66
|
+
PerfectRetry.with_retry(:dont_retry_automatically) do
|
67
|
+
response = HTTPClient.get("http://example.com")
|
68
|
+
if response.code == 500
|
69
|
+
sleep 3
|
70
|
+
throw :retry
|
71
|
+
end
|
72
|
+
end
|
73
|
+
```
|
74
|
+
|
75
|
+
`throw :retry` redo the block at first without `config.limit` checking. In above case, infinity retry while example.com returns 500.
|
76
|
+
|
77
|
+
|
78
|
+
## Installation
|
79
|
+
|
80
|
+
Add this line to your application's Gemfile:
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
gem 'perfect_retry'
|
84
|
+
```
|
85
|
+
|
86
|
+
And then execute:
|
87
|
+
|
88
|
+
$ bundle
|
89
|
+
|
90
|
+
Or install it yourself as:
|
91
|
+
|
92
|
+
$ gem install perfect_retry
|
93
|
+
|
94
|
+
# See also
|
95
|
+
|
96
|
+
- [retryable](https://github.com/nfedyashev/retryable)
|
97
|
+
- [retry-handler](https://github.com/kimoto/retry-handler)
|
98
|
+
|
99
|
+
## License
|
100
|
+
|
101
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
102
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "perfect_retry"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require "logger"
|
2
|
+
|
3
|
+
class PerfectRetry
|
4
|
+
class Config < Struct.new(:limit, :rescues, :logger, :sleep, :ensure)
|
5
|
+
def self.create_from_hash(hash)
|
6
|
+
config = new
|
7
|
+
hash.each do |k, v|
|
8
|
+
config.send("#{k}=", v)
|
9
|
+
end
|
10
|
+
config
|
11
|
+
end
|
12
|
+
|
13
|
+
def sleep_sec(count)
|
14
|
+
if sleep.is_a?(Proc)
|
15
|
+
sleep.call(count)
|
16
|
+
else
|
17
|
+
sleep
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require "perfect_retry/version"
|
2
|
+
require "perfect_retry/config"
|
3
|
+
|
4
|
+
class PerfectRetry
|
5
|
+
class TooManyRetry < StandardError; end
|
6
|
+
|
7
|
+
REGISTERED_CONFIG = { }
|
8
|
+
|
9
|
+
DEFAULTS = {
|
10
|
+
limit: 5,
|
11
|
+
rescues: [StandardError],
|
12
|
+
logger: Logger.new(STDERR),
|
13
|
+
sleep: lambda{|n| n ** 2},
|
14
|
+
ensure: lambda{},
|
15
|
+
}.freeze
|
16
|
+
|
17
|
+
def self.with_retry(config_key = nil, &block)
|
18
|
+
new(config_key).with_retry(&block)
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.register(name, &block)
|
22
|
+
config = Config.create_from_hash(DEFAULTS)
|
23
|
+
block.call(config)
|
24
|
+
REGISTERED_CONFIG[name] = config
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.registered_config_all
|
28
|
+
REGISTERED_CONFIG
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.registered_config(key)
|
32
|
+
REGISTERED_CONFIG[key]
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.deregister_all
|
36
|
+
REGISTERED_CONFIG.clear
|
37
|
+
end
|
38
|
+
|
39
|
+
attr_reader :config
|
40
|
+
|
41
|
+
def initialize(config_key = nil)
|
42
|
+
@config = REGISTERED_CONFIG[config_key] || default_config
|
43
|
+
end
|
44
|
+
|
45
|
+
def default_config
|
46
|
+
Config.create_from_hash(DEFAULTS)
|
47
|
+
end
|
48
|
+
|
49
|
+
def with_retry(&block)
|
50
|
+
count = 0
|
51
|
+
begin
|
52
|
+
retry_with_catch(count, &block)
|
53
|
+
rescue *config.rescues => e
|
54
|
+
if should_retry?(count)
|
55
|
+
count += 1
|
56
|
+
config.logger.warn "[#{count}/#{config.limit || "Infinitiy"}] Retrying after #{config.sleep_sec(count)} seconds. Ocurred: #{e}(#{e.class})"
|
57
|
+
sleep_before_retry(count)
|
58
|
+
retry
|
59
|
+
end
|
60
|
+
|
61
|
+
raise TooManyRetry.new("too many retry (#{config.limit} times)")
|
62
|
+
ensure
|
63
|
+
config.ensure.call
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def sleep_before_retry(count)
|
68
|
+
sleep config.sleep_sec(count)
|
69
|
+
end
|
70
|
+
|
71
|
+
def should_retry?(count)
|
72
|
+
return true unless config.limit
|
73
|
+
count < config.limit
|
74
|
+
end
|
75
|
+
|
76
|
+
private
|
77
|
+
|
78
|
+
def retry_with_catch(count, &block)
|
79
|
+
proc do
|
80
|
+
catch(:retry) do
|
81
|
+
return block.call(count)
|
82
|
+
end
|
83
|
+
|
84
|
+
redo # reached here only `throw :retry` called in a block
|
85
|
+
end.call
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'perfect_retry/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "perfect_retry"
|
8
|
+
spec.version = PerfectRetry::VERSION
|
9
|
+
spec.authors = ["uu59"]
|
10
|
+
spec.email = ["k@uu59.org"]
|
11
|
+
|
12
|
+
spec.summary = %q{Retry handling kit}
|
13
|
+
spec.description = %q{Retry handling kit}
|
14
|
+
spec.homepage = "https://github.com/uu59/perfect_retry"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
spec.add_development_dependency "codeclimate-test-reporter"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: perfect_retry
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- uu59
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
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: codeclimate-test-reporter
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Retry handling kit
|
70
|
+
email:
|
71
|
+
- k@uu59.org
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/console
|
84
|
+
- bin/setup
|
85
|
+
- lib/perfect_retry.rb
|
86
|
+
- lib/perfect_retry/config.rb
|
87
|
+
- lib/perfect_retry/version.rb
|
88
|
+
- perfect_retry.gemspec
|
89
|
+
homepage: https://github.com/uu59/perfect_retry
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata: {}
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 2.4.5.1
|
110
|
+
signing_key:
|
111
|
+
specification_version: 4
|
112
|
+
summary: Retry handling kit
|
113
|
+
test_files: []
|