try_again 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +28 -0
- data/lib/try_again.rb +37 -0
- metadata +56 -0
data/README
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
This method is usefull for when you want to attempt to get a resource on a network and it may fail.
|
2
|
+
Or maybe you know that there's a possibility that the databases haven't sync yet and you want to try a few times till they do.
|
3
|
+
|
4
|
+
This is one of the first few pieces of code I ever wrote in Ruby, it was intented to be help on random failure we had on automated testes where we were testing big applications that included many machines and databases whose sync was not always perfect (or our tests were too fast for the sync).
|
5
|
+
|
6
|
+
It's really easy to use
|
7
|
+
Just do
|
8
|
+
TryAgain.retry do
|
9
|
+
some code
|
10
|
+
end
|
11
|
+
|
12
|
+
By default it attempts your code 3 times with a 3 seconds sleep in between IF your code raises a StandardError.
|
13
|
+
I SERIOUSLY recomend your code to raise some other error other than StandardError, or you may have some unexpected results when something that shouldn't fail does.
|
14
|
+
|
15
|
+
You have a few options that you can pass:
|
16
|
+
:attempts => (int) number of times to try run block till give up; defaults 3
|
17
|
+
:sleep => (int) seconds between each attempt; defaults 3
|
18
|
+
:error => (Constant) error to listen to; defaults StandardError
|
19
|
+
:output => (IO Object) It's kinda like a debug mode, but you can send an object like $stderr our $stdout or even a file so it's output doesn't get in the way of your normal output; defaults nil
|
20
|
+
:kill => (Boolean) If true after the last attempt it will raise your error back to you; defaults false
|
21
|
+
|
22
|
+
You pass them as an hash like so:
|
23
|
+
|
24
|
+
TryAgain.retry(:error => MyCustomErrorClass) do
|
25
|
+
raise MyCustomErrorClass if something
|
26
|
+
end
|
27
|
+
|
28
|
+
Finally, if :kill => false this method returns a Boolean whatever it passed or not.
|
data/lib/try_again.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
#@version 1
|
2
|
+
#@author Arthur
|
3
|
+
module TryAgain
|
4
|
+
#@param options [Hash] (:sleep => number, :attempts => number, :error => Error, :kill => boolean)
|
5
|
+
#@param block [Block] block of code to be attempted
|
6
|
+
#@return [Boolean]
|
7
|
+
def self.retry( options={}, &block )
|
8
|
+
defaults = { :sleep => 3, :attempts => 3, :error => StandardError, :kill => false, :output => nil }
|
9
|
+
options = defaults.merge options
|
10
|
+
out = options[:output]
|
11
|
+
if out and !out.is_a? IO
|
12
|
+
raise InvalidOutput
|
13
|
+
end
|
14
|
+
attempts = 0
|
15
|
+
failed = false
|
16
|
+
|
17
|
+
begin
|
18
|
+
yield
|
19
|
+
rescue options[:error]
|
20
|
+
attempts += 1
|
21
|
+
out.puts "#{ options[:error].to_s } for the #{attempts}# attempt" if out
|
22
|
+
if attempts < options[:attempts]
|
23
|
+
sleep options[:sleep]
|
24
|
+
retry
|
25
|
+
end
|
26
|
+
out.puts "#Giving up on #{ options[:error].to_s }, too many attempts" if out
|
27
|
+
raise options[:error] if options[:kill]
|
28
|
+
failed = true
|
29
|
+
end
|
30
|
+
if !failed and out
|
31
|
+
out.puts "#{ options[:error].to_s } took #{attempts + 1} attempts to pass"
|
32
|
+
end
|
33
|
+
return !failed
|
34
|
+
end
|
35
|
+
|
36
|
+
class InvalidOutput < StandardError; end
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: try_again
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 1.0.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Arthur Silva
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2012-09-03 00:00:00 Z
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: " This method is usefull for when you want to attempt to get a resource on a network and it may fail \n\n Or maybe you know that there's a possibility that the databases haven't sync yet and you want to try a few times till they do\n"
|
17
|
+
email: awls99@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- lib/try_again.rb
|
26
|
+
- README
|
27
|
+
homepage: https://github.com/awls99/Try-Again
|
28
|
+
licenses: []
|
29
|
+
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: "0"
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
requirements: []
|
48
|
+
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 1.8.15
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: A helping method to to retry a block a few times with a sleep in between
|
54
|
+
test_files: []
|
55
|
+
|
56
|
+
has_rdoc:
|