Class: Rescue::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/rescue/handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Handler

Returns a new instance of Handler.



7
8
9
10
11
12
13
# File 'lib/rescue/handler.rb', line 7

def initialize(options = {})
  @errors = options[:errors] || []
  @max_attempts = options[:max_attempts] || 3
  @delay = options[:delay] || :none
  @logger = options[:logger] || ::Logger.new($stdout)
  @attempt = 1
end

Instance Attribute Details

#attemptObject (readonly)

Returns the value of attribute attempt.



5
6
7
# File 'lib/rescue/handler.rb', line 5

def attempt
  @attempt
end

#delayObject (readonly)

Returns the value of attribute delay.



5
6
7
# File 'lib/rescue/handler.rb', line 5

def delay
  @delay
end

#errorsObject (readonly)

Returns the value of attribute errors.



5
6
7
# File 'lib/rescue/handler.rb', line 5

def errors
  @errors
end

#loggerObject (readonly)

Returns the value of attribute logger.



5
6
7
# File 'lib/rescue/handler.rb', line 5

def logger
  @logger
end

#max_attemptsObject (readonly)

Returns the value of attribute max_attempts.



5
6
7
# File 'lib/rescue/handler.rb', line 5

def max_attempts
  @max_attempts
end

Instance Method Details

#call(&block) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/rescue/handler.rb', line 15

def call(&block)
  block.call
rescue *errors => e
  log_attempt(e)
  raise if max_attempts?
  @attempt += 1
  sleep delay_retry(attempt) unless delay == :none
  retry
end