Project

stubbornly

0.0
Repository is archived
No release in over 3 years
Low commit activity in last 3 years
Stubbornly retries a given block until the maximum number of attempts or timeout has been reached.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

 Project Readme

Stubbornly

Build Status

Stubbornly retries a given block until the maximum number of attempts or a timeout has been reached.

The timeout is enforced right before an attempt, but if that blocks longer than the given timeout it will not be interrupted. Instead of using the dangerous Timeout module, use a library that fails fast and retry multiple times using Stubbornly.

Examples

Timeout

require 'stubbornly'
require 'http'

class Checker
  def initialize(url)
    @url = url
    @stubbornly = Stubbornly.new
  end

  def up?
    @stubbornly.retry(timeout: 3) do
      HTTP.get(@url)
    end
  end
end

begin
  Checker.new('http://localhost:8765').up?
rescue => e
  warn "Error: #{e.message}"
end

Maximum number of attempts

require 'stubbornly'
require 'http'

class Checker
  def initialize(url)
    @url = url
    @stubbornly = Stubbornly.new
  end

  def up?
    @stubbornly.retry(attempts: 3) do
      response = HTTP.get(@url)
      puts "The site at #{@url} is up 👍"
    end
  end
end

begin
  Checker.new('http://localhost:8765').up?
rescue => e
  warn "Error: #{e.message}"
end

Alternatives

License

The gem is available as open source under the terms of the MIT License.