Sha256: ca8406a38448db4e7b05da5a50dc76c46e82d3d78c8e480485690e35596305bc

Contents?: true

Size: 1.88 KB

Versions: 5

Compression:

Stored size: 1.88 KB

Contents

require 'xmlrpc/client'

module OpenX

  unless defined? HTTPBroken
    # A module that captures all the possible Net::HTTP exceptions 
    # from http://pastie.org/pastes/145154
    module HTTPBroken; end
    
    [Timeout::Error, Errno::EINVAL, Errno::EPIPE, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, 
      Net::HTTPHeaderSyntaxError, Net::ProtocolError].each {|m| m.send(:include, HTTPBroken)}
  end

  class XmlrpcClient
    @uri = nil
    @server = nil
    
    @retry_on_http_error = true
    @timeout = 10 # seconds
    
    class << self
      attr_accessor :retry_on_http_error, :timeout
      
      def init_server(uri)
        server = XMLRPC::Client.new2(uri)
        server.timeout = self.timeout
        #server.instance_variable_get(:@http).set_debug_output($stderr)
        server
      end
      
      def new2(uri)
        server = init_server(uri)
        new(server, uri)
      end
    end
    
    def initialize(server, uri)
      @server = server
      @uri = uri
    end
    
    def call(method, *args)
      if args.first.is_a?(OpenX::Services::Session)
        session = args.shift()
        args.unshift(session.id)
        begin
          do_call(method, *args)
        rescue XMLRPC::FaultException => sess_id_err
          if sess_id_err.message.strip == 'Session ID is invalid'
            session.recreate!
            args.shift()
            args.unshift(session.id)
            do_call(method, *args)
          else
            raise sess_id_err
          end
        end
      else
        do_call(method, *args)
      end
    end
    
    def do_call(method, *args)
      begin
        @server.call(method, *args)
      rescue HTTPBroken => httpbe
        if self.class.retry_on_http_error
          @server = self.class.init_server(@uri)
          @server.call(method, *args)
        else
          raise httpbe
        end
      end
    end
    private :do_call
    
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
jjp-openx-1.1.6 lib/openx/xmlrpc_client.rb
jjp-openx-1.1.4 lib/openx/xmlrpc_client.rb
touchlocal-openx-1.1.2 lib/openx/xmlrpc_client.rb
touchlocal-openx-1.1.1 lib/openx/xmlrpc_client.rb
touchlocal-openx-1.1.0 lib/openx/xmlrpc_client.rb