Sha256: 9dc57bd122a54d17e162bb366deac73367c65396cf0736e23fb03dcf820a4a89

Contents?: true

Size: 910 Bytes

Versions: 9

Compression:

Stored size: 910 Bytes

Contents

module Houston

  def self.try(options, *rescue_from)
    options = { max_tries: options } if options.is_a?(Fixnum)
    options = {} unless options.is_a?(Hash)
    max_tries = options.fetch :max_tries, 3
    base = options.fetch :base, 2
    ignore = options.fetch :ignore, false
    rescue_from = [StandardError] if rescue_from.empty?

    tries = 1
    begin
      yield tries
    rescue *rescue_from
      if tries > max_tries
        return if ignore
        raise
      end
      Rails.logger.warn "\e[31m[try] \e[1m#{$!.class}\e[0;31m: #{$!.message}\e[0m"
      sleep base ** tries
      tries += 1
      retry
    end
  end

  def self.reconnect(options={})
    max_tries = options.fetch(:max_tries, 2)
    tries = 1
    begin
      yield
    rescue exceptions_wrapping(PG::ConnectionBad)
      ActiveRecord::Base.connection.reconnect!
      retry unless (tries += 1) > 2
      raise
    end
  end

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
houston-core-0.9.2 lib/houston/try.rb
houston-core-0.9.1 lib/houston/try.rb
houston-core-0.9.0 lib/houston/try.rb
houston-core-0.9.0.rc1 lib/houston/try.rb
houston-core-0.8.4 lib/houston/try.rb
houston-core-0.8.3 lib/houston/try.rb
houston-core-0.8.2 lib/houston/try.rb
houston-core-0.8.1 lib/houston/try.rb
houston-core-0.8.0 lib/houston/try.rb