lib/attempt.rb in attempt-0.1.2 vs lib/attempt.rb in attempt-0.2.0
- old
+ new
@@ -1,10 +1,19 @@
require 'timeout'
+require 'structured_warnings'
+# The Attempt class encapsulates methods related to multiple attempts at
+# running the same method before actually failing.
class Attempt
- VERSION = '0.1.2'
+ # The version of the attempt library.
+ VERSION = '0.2.0'
+
+ # Warning raised if an attempt fails before the maximum number of tries
+ # has been reached.
+ class Warning < StandardWarning; end
+
# Number of attempts to make before failing. The default is 3.
attr_accessor :tries
# Number of seconds to wait between attempts. The default is 60.
attr_accessor :interval
@@ -44,10 +53,16 @@
@warnings = true # Errors sent to STDERR as warnings if true
yield self if block_given?
end
+ # Attempt to perform the operation in the provided block up to +tries+
+ # times, sleeping +interval+ between each try.
+ #
+ # You will not typically use this method directly, but the Kernel#attempt
+ # method instead.
+ #
def attempt
count = 1
begin
if @timeout
Timeout.timeout(@timeout){ yield }
@@ -57,10 +72,10 @@
rescue @level => error
@tries -= 1
if @tries > 0
msg = "Error on attempt # #{count}: #{error}; retrying"
count += 1
- warn msg if @warnings
+ warn Warning, msg if @warnings
@log.puts msg if @log
@interval += @increment if @increment
sleep @interval
retry
end