发现经常有”超时“的错误信息,如/usr/lib/ruby/1.8/timeout.rb:54:in `rbuf_fill': execution expired (Timeout::Error),恩,应该是网络不稳定或者是服务器响应太慢的结果,需要捕获下这个异常并做些处理,记录如下:
需要注意的是,Timeout::Error不是StandardError的子类, 而是继承至 Interrupt class,所以捕获的时候,需要格外注意,演示如下:
require 'net/pop3'
begin
Net::POP3.auth_only(@server, @port, @username, @password)
rescue => e
write_error_to_logfile(e)
do_something_sensible
end
看上面的这段代码,当POP3服务器不能及时响应的时候,所触发的异常并不能被下面捕获到,原因就是上面说的,再看正确的处理代码:
require 'net/pop3'
begin
Net::POP3.auth_only(@server, @port, @username, @password)
rescue => e
write_error_to_logfile(e)
do_something_sensible
rescue Timeout::Error => e
write_error_to_logfile(e)
do_something_sensible_for_timeout
end
这段代码可以正常工作,并按照我们的意愿来处理了。
如果您知道对方的服务器会比较慢的响应,或者你知道网络状态不好,你可以单独设置这个TimeOut的时间,代码如下:
require 'timeout'
...
...
begin
timeout(60) do
resp, body=3Dh.get('/index.html')
puts body
end
rescue TimeoutError
puts "Timed Out"
end
或者这样:(来源:http://textsnippets.com/posts/show/868)
http = Net::HTTP.new(url.host, url.port)
http.read_timeout=time_out
分享到:
相关推荐
Rails异常处理程序 从版本1升级? 这是Rails的灵活异常处理程序,适用于希望创建自己的错误跟踪服务的人员。 它面向经验丰富的Rails开发人员,这些开发人员不仅要管理几个Rails应用程序,而且还要管理多个应用程序。...
基本上,我们捕获 Rack::Timeout 引发的 Exception 并将其转换为 StandardError 以便 Puma 工作人员不会被杀死。 有关问题的更好描述,请参阅 。 基本用法 Rails 应用程序 # Gemfile gem "rack-timeout-puma" 这...
本资源是参照rails敏捷开发第四版书中的例子,rails的版本是rails3.2.6
您可以通过设置RACK_TIMEOUT_SERVICE_TIMEOUT环境变量来进行修改。 还有一些其他设置,请继续阅读以了解详细信息。 Rack :: Timeout不能解决长时间运行的请求问题,它是一种调试和修复工具。 应用程序开发人员应...
Exception Notification - Rails的异常通知插件。能够在Rack/Rails应用发生错误时发送通知。
Simple Form - 轻松处理Rails表单
rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails ...
安装将此行添加到您的应用程序的Gemfile中: gem 'auto-session-timeout' 然后执行: $ bundle或将其自己安装为: $ gem install auto-session-timeout用法安装后,告诉您的应用程序控制器使用自动超时: class ...
Ruby和Rails高效的Kafka处理框架_Ruby_下载.zip
Ajax、Prototype和Scriptaculous等JavaScript代码库和RJS,Session管理、用户登录和认证系统,XML和ActiveResource,后台处理和ActionMaile,测试和specs(包括RSpec on Rails和Selenium),安装、管理、编写插件,...
rails-timeago提供了一个timeago_tag助手来创建可用于插件的时间标签。 安装 将此行添加到您的应用程序的Gemfile : gem 'rails-timeago' , '~> 2.0' 然后执行: $ bundle 或将其自己安装为: $ gem install ...
[Pragmatic Bookshelf] Crafting Rails Applications Expert Practices for Everyday Rails Development (E-Book) ☆ 图书概要:☆ Rails 3 is a huge step forward. You can now easily extend the framework, ...
Ruby on Rails Guides v2 - Ruby on Rails 4.2.5
《Ruby on Rails Tutorial》中文版(原书第2版,涵盖 Rails 4) Ruby 是一门很美的计算机语言,其设计原则就是“让编程人员快乐”。David Heinemeier Hansson 就是看重了这一点,才在开发 Rails 框架时选择了 Ruby...
Rails 4 introduces a number of user-facing changes, and the ebook has been updated to match all the latest changes and new best practices in Rails. This includes full support for Ruby 2.0, controller ...
一个用Ruby on Rails搭建的图片分享的网站项目.完整源代码
This edition now gives new Ruby and Rails users more information on the Ruby language and takes more time to explain key concepts throughout. Best practices on how to apply Rails continue to change, ...
adminlte-rails, AdminLTE Rails gem 将AdminLTE主题与 Rails 资产管道集成 AdminLTE Rails gem AdminLTE 是后端的高级 Bootstrap 主题。英镑 AdminLTE Rails gem 与 Rails 资产管道集成了英镑AdminLTE主题。安装将...
Bootstrap 3 和 Rails 4(样例用的是Ruby 2.1.1,Rails 4.1.4) Table of Contents Preface 1 Chapter 1: Introducing Web Application Development in Rails 7 Why Bootstrap with Rails? 8 Setting up a Todo ...