Sha256: 18a75ced35a41593cf7d29e7576b0dcc99c3b9a10b78b970c73502a1b4708e96

Contents?: true

Size: 1.4 KB

Versions: 4

Compression:

Stored size: 1.4 KB

Contents

require 'httpimagestore/configuration/handler'

module Configuration
	class SourceFailoverAllFailedError < ConfigurationError
		attr_reader :sources, :errors

		def initialize(sources, errors)
			@sources = sources
			@errors = errors
			super "all sources failed: #{sources.zip(errors).map{|s, e| "#{s}(#{e.class.name}: #{e.message})"}.join(', ')}"
		end
	end

	class SourceFailover < Scope
		include ClassLogging

		def self.match(node)
			node.name == 'source_failover'
		end

		def self.parse(configuration, node)
			# support only sources
			handler_configuration = Struct.new(
				:global,
				:sources
			).new
			handler_configuration.global = configuration.global
			handler_configuration.sources = []

			failover = self.new(handler_configuration)
			configuration.sources << failover
			failover.parse(node)
		end

		def realize(request_state)
			errors = []
			@configuration.sources.each do |source|
				begin
					log.debug "trying source: #{source}"
					return source.realize(request_state) unless source.respond_to? :excluded? and source.excluded?(request_state)
				rescue => error
					errors << error
					log.warn "source #{source} failed; trying next source", error
				end
			end
			log.error "all sources: #{@configuration.sources.map(&:to_s).join(', ')} failed; giving up"
			raise SourceFailoverAllFailedError.new(@configuration.sources.to_a, errors)
		end
	end
	Handler::register_node_parser SourceFailover
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
httpimagestore-1.9.0 lib/httpimagestore/configuration/source_failover.rb
httpimagestore-1.8.1 lib/httpimagestore/configuration/source_failover.rb
httpimagestore-1.8.0 lib/httpimagestore/configuration/source_failover.rb
httpimagestore-1.7.0 lib/httpimagestore/configuration/source_failover.rb