repeater 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +3 -0
- data/LICENSE +22 -0
- data/README.md +46 -0
- data/Rakefile +2 -0
- data/lib/repeater.rb +2 -0
- data/lib/repeater/kernel/retryable.rb +90 -0
- data/lib/repeater/version.rb +3 -0
- data/repeater.gemspec +17 -0
- metadata +55 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Roman Parashchenko
|
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,46 @@
|
|
1
|
+
# Repeater
|
2
|
+
|
3
|
+
Flexible Ruby code re-execution
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'repeater'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install repeater
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
### The possible options
|
22
|
+
```
|
23
|
+
[tries] count of tries (by default 1)
|
24
|
+
[timeout] timeout (by default 0)
|
25
|
+
[sleep] waiting for some sleep in seconds after each attempt (by default 0)
|
26
|
+
[on] what kind of exceptions it is required to catch (by default generic Exception class)
|
27
|
+
[matching] matching some error text by regular expression (by default any text /.*/)
|
28
|
+
[logger] custom logger, for instance Log4R (by default STDOUT)
|
29
|
+
[trace] should we output handled errors? (by default false)
|
30
|
+
[silent] should we generate exception after finishing? (by default false)
|
31
|
+
```
|
32
|
+
|
33
|
+
### Examples
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
retryable { raise "Some fake error" }
|
37
|
+
rp(tries: 10, silent: true) { raise "Some fake error" }
|
38
|
+
```
|
39
|
+
|
40
|
+
## Contributing
|
41
|
+
|
42
|
+
1. Fork it
|
43
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
44
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
45
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
46
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/repeater.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# = Repeater gem
|
2
|
+
|
3
|
+
module Kernel
|
4
|
+
class InvalidRetryableOptions < RuntimeError; end
|
5
|
+
|
6
|
+
##
|
7
|
+
# This method catches specified exceptions and retries to execute code block again
|
8
|
+
#
|
9
|
+
# The possible +options+ :
|
10
|
+
#
|
11
|
+
# [tries] count of tries (by default 1)
|
12
|
+
# [timeout] timeout (by default 0)
|
13
|
+
# [sleep] waiting for some sleep in seconds after each attempt (by default 0)
|
14
|
+
# [on] what kind of exceptions it is required to catch (by default generic Exception class)
|
15
|
+
# [matching] matching some error text by regular expression (by default any text /.*/)
|
16
|
+
# [logger] custom logger, for instance Log4R (by default STDOUT)
|
17
|
+
# [trace] should we output handled errors? (by default false)
|
18
|
+
# [silent] should we generate exception after finishing? (by default false)
|
19
|
+
#
|
20
|
+
# == Examples:
|
21
|
+
# <tt>retryable { raise "Some fake error" }<tt>
|
22
|
+
# <tt>rp(tries: 10, silent: true) { raise "Some fake error"}
|
23
|
+
|
24
|
+
def retryable(options = {}, &block)
|
25
|
+
opts = Retryable::merge_options(options)
|
26
|
+
|
27
|
+
return nil if opts[:tries] == 0
|
28
|
+
|
29
|
+
retry_exception = [ opts[:on] ].flatten
|
30
|
+
retries = 0
|
31
|
+
end_time = Time.now + opts[:timeout]
|
32
|
+
exception = nil
|
33
|
+
begin
|
34
|
+
begin
|
35
|
+
return block.call(retries)
|
36
|
+
rescue *retry_exception => e
|
37
|
+
exception = e
|
38
|
+
Retryable::log_or_raise(opts[:logger], e) unless e.message =~ opts[:matching]
|
39
|
+
Retryable::trace(opts[:logger], "#{e.class}: #{e.message}") if opts[:trace]
|
40
|
+
sleep opts[:sleep].respond_to?(:call) ? opts[:sleep].call(retries) : opts[:sleep]
|
41
|
+
if retries + 1 < opts[:tries]
|
42
|
+
retries += 1
|
43
|
+
retry if opts[:timeout] <= 0 || !Retryable::time_run_out?(end_time)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end until Retryable::time_run_out?(end_time)
|
47
|
+
Retryable::log_or_raise(opts[:logger], exception) unless exception.nil? || opts[:silent]
|
48
|
+
end
|
49
|
+
|
50
|
+
alias :rp :retryable
|
51
|
+
|
52
|
+
module Retryable # :nodoc: all
|
53
|
+
class << self
|
54
|
+
|
55
|
+
def time_run_out?(end_time)
|
56
|
+
Time.now >= end_time
|
57
|
+
end
|
58
|
+
|
59
|
+
def merge_options(options)
|
60
|
+
default_options = { tries: 1, timeout: 0, sleep: 0, on: Exception, matching: /.*/, logger: nil, trace: false, silent: false }
|
61
|
+
check_options(options, default_options)
|
62
|
+
default_options.merge!(options)
|
63
|
+
end
|
64
|
+
|
65
|
+
def check_options(custom_options, default_options)
|
66
|
+
invalid_options = default_options.merge(custom_options).keys - default_options.keys
|
67
|
+
unless invalid_options.empty?
|
68
|
+
Retryable::log_or_raise(opts[:logger], InvalidRetryableOptions.new("Invalid options: #{invalid_options.join(", ")}"))
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def trace(logger, msg)
|
73
|
+
if logger.nil?
|
74
|
+
puts "[WARN] Kernel.retryable: #{msg}"
|
75
|
+
else
|
76
|
+
logger.warn msg
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def log_or_raise(logger, *args)
|
81
|
+
if logger.nil?
|
82
|
+
raise *args
|
83
|
+
else
|
84
|
+
logger.send :error, *args
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
data/repeater.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/repeater/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Roman Parashchenko"]
|
6
|
+
gem.email = %w(romikoops1@gmail.com)
|
7
|
+
gem.description = %q{Flexible Ruby code re-execution}
|
8
|
+
gem.summary = %q{The gem allows to handle specific exceptions for some code, and try to execute code again }
|
9
|
+
gem.homepage = "https://github.com/romikoops/repeater"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "repeater"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Repeater::VERSION
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: repeater
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Roman Parashchenko
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-26 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Flexible Ruby code re-execution
|
15
|
+
email:
|
16
|
+
- romikoops1@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/repeater.rb
|
27
|
+
- lib/repeater/kernel/retryable.rb
|
28
|
+
- lib/repeater/version.rb
|
29
|
+
- repeater.gemspec
|
30
|
+
homepage: https://github.com/romikoops/repeater
|
31
|
+
licenses: []
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
requirements: []
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 1.8.11
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: The gem allows to handle specific exceptions for some code, and try to execute
|
54
|
+
code again
|
55
|
+
test_files: []
|