【问题标题】:net/http hanging requests causing failure - RoRnet/http 挂起请求导致失败 - RoR
【发布时间】:2013-09-23 18:51:22
【问题描述】:

我有一些代码可以检查 URL 列表的响应代码并将它们返回 - 我遇到了一些挂起的 URL 问题,导致应用程序根本无法加载。如何在 30 秒后请求放弃并检查下一个 URL,将跳过的 URL 标记为失败。

以下是我当前的代码; (型号/status.rb)

 require "net/http"
 require "uri"

 class Status
def initialize(url)
    @url = url
end

def get_status
    response.code
end

def active?
    ["200","203","302"].include?(get_status) ? true : false
end

private

def lookup
    URI.parse(@url)
end
def http
    Net::HTTP.new(lookup.host, lookup.port)
end
def request
    Net::HTTP::Get.new(lookup.request_uri)
end
def response
    http.request(request)
end
end

(控制器/welcome_controller.rb)

class WelcomeController < ApplicationController
def index
@syndication = [
["http://v1.syndication.u01.example.uk/organisations?apikey=bbccdd", "U.INT 01"],
["http://v1.syndication.u02.example.uk/organisations?apikey=bbccdd", "U.INT 02"],


].collect { |url| logger.info("Boom #{url[0]}"); ["#{url[1]} (#{url[0]})", Status.new(url[0]).active?] }

 end

 end

【问题讨论】:

  • 尝试在请求之前将http.read_timeout = 30放入response方法中。

标签: ruby-on-rails ruby net-http


【解决方案1】:

找到答案了..

将以下内容添加到我的“def get_status”中

def get_status 
    begin 
        response.code
    rescue Exception => e
        Rails.logger.info("Error #{e}")
    end
end

这会记录错误并转到下一个 URL

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-12
    • 2015-02-04
    相关资源
    最近更新 更多