reretryable 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +68 -0
- data/lib/reretryable.rb +28 -0
- data/reretryable.gemspec +31 -0
- metadata +76 -0
data/README.markdown
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
This fork is available as a gem under the name 'reretryable'.
|
2
|
+
|
3
|
+
# Retryable#retryable
|
4
|
+
|
5
|
+
## Description
|
6
|
+
|
7
|
+
Runs a code block, and retries it when an exception occurs. It's great when
|
8
|
+
working with flakey webservices (for example).
|
9
|
+
|
10
|
+
It's configured using four optional parameters `:tries`, `:on`, `:matching` and `:sleep`, and
|
11
|
+
runs the passed block. Should an exception occur, it'll retry for (tries-1) times.
|
12
|
+
|
13
|
+
Should the number of retries be reached without success, the last exception
|
14
|
+
will be raised.
|
15
|
+
|
16
|
+
|
17
|
+
## Examples
|
18
|
+
|
19
|
+
Open an URL, retry up to two times when an `OpenURI::HTTPError` occurs.
|
20
|
+
|
21
|
+
require "retryable"
|
22
|
+
require "open-uri"
|
23
|
+
|
24
|
+
include Retryable
|
25
|
+
|
26
|
+
retryable( :tries => 3, :on => OpenURI::HTTPError ) do
|
27
|
+
xml = open( "http://example.com/test.xml" ).read
|
28
|
+
end
|
29
|
+
|
30
|
+
Do _something_, retry up to four times for either `ArgumentError` or
|
31
|
+
`TimeoutError` exceptions.
|
32
|
+
|
33
|
+
require "retryable"
|
34
|
+
include Retryable
|
35
|
+
|
36
|
+
retryable( :tries => 5, :on => [ ArgumentError, TimeoutError ] ) do
|
37
|
+
# some crazy code
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
|
42
|
+
Do _something_, retry up to three times for `ArgumentError` exceptions
|
43
|
+
which smell like "Bacon", but have a nap between tries.
|
44
|
+
|
45
|
+
require "retryable"
|
46
|
+
include Retryable
|
47
|
+
|
48
|
+
retryable( :tries => 3,
|
49
|
+
:on => ArgumentError,
|
50
|
+
:matching => /Bacon/,
|
51
|
+
:sleep => 3) do
|
52
|
+
|
53
|
+
# some crazy code about bacon
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
## Defaults
|
60
|
+
|
61
|
+
:tries => 1, :on => Exception, :matching => /.*/, :sleep => 0
|
62
|
+
|
63
|
+
|
64
|
+
## Thanks
|
65
|
+
|
66
|
+
Many thanks to [Chu Yeow for this nifty piece of code](http://blog.codefront.net/2008/01/14/retrying-code-blocks-in-ruby-on-exceptions-whatever/). Look, I liked it
|
67
|
+
enough to enhance it a little bit and build a gem from it! :)
|
68
|
+
|
data/lib/reretryable.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
module Retryable
|
2
|
+
def retryable(options = {}, &block)
|
3
|
+
opts = { :tries => 1,
|
4
|
+
:on => StandardError,
|
5
|
+
:sleep => 0,
|
6
|
+
:matching => /.*/ }.merge(options)
|
7
|
+
|
8
|
+
return nil if opts[:tries] == 0
|
9
|
+
|
10
|
+
retry_exception = [opts[:on]].flatten
|
11
|
+
tries = opts[:tries]
|
12
|
+
message_pattern = opts[:matching]
|
13
|
+
sleep_time = opts[:sleep]
|
14
|
+
|
15
|
+
begin
|
16
|
+
return yield
|
17
|
+
rescue *retry_exception => exception
|
18
|
+
raise unless exception.message =~ message_pattern
|
19
|
+
|
20
|
+
if (tries -= 1) > 0
|
21
|
+
sleep sleep_time
|
22
|
+
retry
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
yield
|
27
|
+
end
|
28
|
+
end
|
data/reretryable.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{retryable}
|
5
|
+
s.version = "0.1.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Craig 'The Craif' Mackenzie and Niko Felger"]
|
9
|
+
s.date = %q{2010-04-07}
|
10
|
+
s.email = %q{developers@songkick.com}
|
11
|
+
s.extra_rdoc_files = ["README.markdown"]
|
12
|
+
s.files = ["README.markdown", "retryable.gemspec", "spec", "lib/retryable.rb"]
|
13
|
+
s.homepage = %q{http://www.songkick.com}
|
14
|
+
s.rdoc_options = ["--main", "README.markdown"]
|
15
|
+
s.require_paths = ["lib"]
|
16
|
+
s.rubygems_version = %q{1.3.5}
|
17
|
+
s.summary = %q{Retrying code blocks on specific errors}
|
18
|
+
|
19
|
+
if s.respond_to? :specification_version then
|
20
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
21
|
+
s.specification_version = 3
|
22
|
+
|
23
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
24
|
+
s.add_development_dependency(%q<rspec>, [">= 0"])
|
25
|
+
else
|
26
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
27
|
+
end
|
28
|
+
else
|
29
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
30
|
+
end
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: reretryable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Craig 'The Craif' Mackenzie and Niko Felger
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-07-15 00:00:00 +10:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
type: :development
|
31
|
+
version_requirements: *id001
|
32
|
+
description:
|
33
|
+
email: developers@songkick.com
|
34
|
+
executables: []
|
35
|
+
|
36
|
+
extensions: []
|
37
|
+
|
38
|
+
extra_rdoc_files:
|
39
|
+
- README.markdown
|
40
|
+
files:
|
41
|
+
- README.markdown
|
42
|
+
- reretryable.gemspec
|
43
|
+
- lib/reretryable.rb
|
44
|
+
has_rdoc: true
|
45
|
+
homepage: http://www.songkick.com
|
46
|
+
licenses: []
|
47
|
+
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options:
|
50
|
+
- --main
|
51
|
+
- README.markdown
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
segments:
|
66
|
+
- 0
|
67
|
+
version: "0"
|
68
|
+
requirements: []
|
69
|
+
|
70
|
+
rubyforge_project:
|
71
|
+
rubygems_version: 1.3.6
|
72
|
+
signing_key:
|
73
|
+
specification_version: 3
|
74
|
+
summary: What this thing does
|
75
|
+
test_files: []
|
76
|
+
|