rescue-retry 0.1.0
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 +5 -0
- data/Gemfile +4 -0
- data/README.md +56 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/rescue/handler.rb +43 -0
- data/lib/rescue/retry.rb +22 -0
- data/lib/rescue/version.rb +5 -0
- data/rescue-retry.gemspec +26 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2a8b1fc794a6e2af2f1f49306d53a4c78dfef9e1
|
4
|
+
data.tar.gz: 21799f0fea3fb29a50cc4a4bdd4eb06da5d2588e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 46b16c7045ac1eaa166bf10c6b8fcff1bab2a50a15f3e6c6f1ddea676709ac60f37cecdebcb3efa5ded07b158e212ed2dc84af642a91e79f2e2605a6eaf033db
|
7
|
+
data.tar.gz: 95018b6100071aff5798470fed71815fcdcab1508a0a903338ca61efdd17f243f90dc9f6eb33786feb97f1b87e9f9dead7d9f679a5967c5471343cea91029137
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# Rescue::Retry
|
2
|
+
|
3
|
+
Wraps a instance method in a configurable retry handler with additional/optional delay logic as well.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'rescue-retry'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install rescue-retry
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require 'rescue/retry'
|
25
|
+
|
26
|
+
class StateCache
|
27
|
+
extend Rescue::Retry
|
28
|
+
|
29
|
+
# Simple
|
30
|
+
rescue_retry :retrieve, Redis::CannotConnectError
|
31
|
+
|
32
|
+
# Verbose
|
33
|
+
rescue_retry :update, [Redis::CannotConnectError, ArgumentError],
|
34
|
+
max_attempts: 5, # optional
|
35
|
+
delay: :exponential # optional: :linear, :random
|
36
|
+
|
37
|
+
def retrieve
|
38
|
+
redis.get(key)
|
39
|
+
end
|
40
|
+
|
41
|
+
def update(data)
|
42
|
+
redis.set(key, data)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
## Development
|
48
|
+
|
49
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
50
|
+
|
51
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
52
|
+
|
53
|
+
## Contributing
|
54
|
+
|
55
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[robert2d]/rescue-retry.
|
56
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rescue/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,43 @@
|
|
1
|
+
require 'logger'
|
2
|
+
|
3
|
+
module Rescue
|
4
|
+
class Handler
|
5
|
+
attr_reader :errors, :logger, :attempt, :max_attempts, :delay
|
6
|
+
|
7
|
+
def initialize(options = {})
|
8
|
+
@errors = options[:errors] || []
|
9
|
+
@max_attempts = options[:max_attempts] || 3
|
10
|
+
@delay = options[:delay] || :none
|
11
|
+
@logger = options[:logger] || ::Logger.new($stdout)
|
12
|
+
@attempt = 1
|
13
|
+
end
|
14
|
+
|
15
|
+
def call(&block)
|
16
|
+
block.call
|
17
|
+
rescue *errors => e
|
18
|
+
logger.warn "Failed attempt (#{attempt} of #{max_attempts}) #<#{e.class.name}.#{e.message}>"
|
19
|
+
re_raise(e) if max_attempts?
|
20
|
+
@attempt += 1
|
21
|
+
sleep delay_retry(attempt) unless delay == :none
|
22
|
+
retry
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def delay_retry(attempt)
|
28
|
+
return attempt ** 2 if delay == :exponential
|
29
|
+
return attempt * 2 if delay == :linear
|
30
|
+
return (rand + 1) * 0.5 * attempt if delay == :random
|
31
|
+
raise ArgumentError, "unknown delay \"#{delay}\". accepts: :exponential, :linear and :random"
|
32
|
+
end
|
33
|
+
|
34
|
+
def re_raise(error)
|
35
|
+
logger.error "maximum attempts (#{max_attempts}) reached"
|
36
|
+
raise error.class, error.message
|
37
|
+
end
|
38
|
+
|
39
|
+
def max_attempts?
|
40
|
+
attempt == max_attempts
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/rescue/retry.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rescue/version'
|
2
|
+
require 'rescue/handler'
|
3
|
+
|
4
|
+
module Rescue
|
5
|
+
module Retry
|
6
|
+
def rescue_retry(method, errors, options = {})
|
7
|
+
proxy = Module.new do
|
8
|
+
define_method(method) do |*args|
|
9
|
+
Handler.new(
|
10
|
+
{
|
11
|
+
errors: Array(errors),
|
12
|
+
logger: respond_to?(:logger) ? logger : nil
|
13
|
+
}.merge(options)
|
14
|
+
).call do
|
15
|
+
super *args
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
self.prepend proxy
|
20
|
+
end
|
21
|
+
end
|
22
|
+
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 'rescue/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rescue-retry"
|
8
|
+
spec.version = Rescue::Retry::VERSION
|
9
|
+
spec.authors = ["robert2d"]
|
10
|
+
spec.email = ["david.robertson@crichq.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Rescue and Retry a instance method, whilst keeping a method free from rescue's}
|
13
|
+
spec.description = %q{Wraps an instance method with retry and error capture without adding logic directly to the method}
|
14
|
+
spec.homepage = "https://github.com/robert2d/rescue-retry"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rescue-retry
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- robert2d
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-08-02 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.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
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: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: Wraps an instance method with retry and error capture without adding
|
56
|
+
logic directly to the method
|
57
|
+
email:
|
58
|
+
- david.robertson@crichq.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- ".travis.yml"
|
66
|
+
- Gemfile
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lib/rescue/handler.rb
|
72
|
+
- lib/rescue/retry.rb
|
73
|
+
- lib/rescue/version.rb
|
74
|
+
- rescue-retry.gemspec
|
75
|
+
homepage: https://github.com/robert2d/rescue-retry
|
76
|
+
licenses: []
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.5.1
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: Rescue and Retry a instance method, whilst keeping a method free from rescue's
|
98
|
+
test_files: []
|