Ruby on rails net/http挂起请求导致失败-RoR
我有一些代码可以检查URL列表的响应代码并将它们显示出来-我遇到了一些URL挂起的问题,这导致应用程序根本无法加载。如何在30秒后请求放弃并检查下一个URL,将跳过的URL标记为失败 下面是我目前的代码; (Model/status.rb) (controllers/welcome_controller.rb)Ruby on rails net/http挂起请求导致失败-RoR,ruby-on-rails,ruby,net-http,Ruby On Rails,Ruby,Net Http,我有一些代码可以检查URL列表的响应代码并将它们显示出来-我遇到了一些URL挂起的问题,这导致应用程序根本无法加载。如何在30秒后请求放弃并检查下一个URL,将跳过的URL标记为失败 下面是我目前的代码; (Model/status.rb) (controllers/welcome_controller.rb) class WelcomeController
class WelcomeController
得到了答案
将以下内容添加到我的“def get_状态”
这记录了错误,并转到下一个URL尝试在请求之前将
http.read\u timeout=30
放入response
方法。
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
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
def get_status
begin
response.code
rescue Exception => e
Rails.logger.info("Error #{e}")
end
end