Module: TryAgain

Defined in:
lib/try_again.rb

Overview

Version:

  • 2

Defined Under Namespace

Classes: InvalidOutput

Class Method Summary collapse

Class Method Details

.retry(sleep: 3, attempts: 3, error: StandardError, kill: false, output: nil, &block) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/try_again.rb', line 7

def self.retry( sleep: 3, attempts: 3, error: StandardError, kill: false, output: nil, &block )
    tries||=0
    tries+=1
    out      = output
    if out and !out.respond_to? :puts
        raise InvalidOutput
    end
    attempted = 0
    failed    = false

    begin
        yield
    rescue error => e
        attempted += 1
        out.puts "#{ error.to_s } for the #{tries}# attempt" if out
        if attempted < attempts
            sleep sleep
            retry
        end
        out.puts "#Giving up on #{ error.to_s }, too many attempts" if out
        raise e if kill
        failed = true
    end
    if !failed and out
        out.puts "#{ error.to_s } took #{tries} attempts to pass"
    end
    return !failed
end