Class: Respawn::Try

Inherits:
Object
  • Object
show all
Defined in:
lib/respawn/try.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*exceptions, **options) ⇒ Try

Returns a new instance of Try.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/respawn/try.rb', line 9

def initialize(*exceptions, **options)
  options.keys.each { OPTIONS.keys.try!(it) }

  self.setup = options.fetch(:setup, Setup.new(**options))

  self.notifier = options.fetch(:notifier, setup.notifier)
  self.predicate = options.fetch(:predicate, setup.predicate)

  exceptions = setup.ex if exceptions.empty?

  self.exceptions = parse_exceptions(exceptions) + [PredicateError]
  self.tries = options.fetch(:tries, setup.tries)
  self.onfail = ONFAIL.try! options.fetch(:onfail, setup.onfail)
  self.wait = options.fetch(:wait, setup.wait)
  self.env = options.fetch(:env, Environment.default)

  self.handler = Handler.new(onfail)
end

Class Method Details

.callObject



5
6
7
# File 'lib/respawn/try.rb', line 5

def self.call(*, **, &)
  new(*, **).call(&)
end

Instance Method Details

#callObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/respawn/try.rb', line 28

def call
  yield(handler)
    .tap(&method(:check_predicates))
rescue *exceptions => e
  self.tries = tries - 1
  handler.retry_number += 1

  if tries.positive?
    Kernel.sleep(wait) unless env.test?
    retry
  end

  perform_fail(e)
end