Sha256: 9938c36cc7d30acdd27916fea660b75466f263069e7aca30d3bd450a943923bb

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

################################################################################
#
#      Author: Zachary Patten <zachary@jovelabs.net>
#   Copyright: Copyright (c) Jove Labs
#     License: Apache License, Version 2.0
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
################################################################################
module ZTK

  # ZTK::RescueRetry Error Class
  #
  # @author Zachary Patten <zachary@jovelabs.net>
  class RescueRetryError < Error; end

  # RescueRetry Class
  #
  # @author Zachary Patten <zachary@jovelabs.net>
  class RescueRetry

    class << self

      def try(options={}, &block)
        options = Base.build_config({
          :tries => 1,
          :on => Exception
        }.merge(options))
        options.logger.debug { "options(#{options.inspect})" }

        !block_given? and Base.log_and_raise(options.logger, RescueRetryError, "You must supply a block!")

        begin
          return block.call
        rescue options.on => e
          if ((options.tries -= 1) > 0)
            options.logger.warn { "Caught #{e.inspect}, we will give it #{options.tries} more tr#{options.tries > 1 ? 'ies' : 'y'}." }
            retry
          else
            options.logger.fatal { "Caught #{e.inspect}, sorry, we have to give up now." }
            raise e
          end
        end

      end

    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ztk-0.2.5 lib/ztk/rescue_retry.rb