可重试的 FTP &使用 Typhoeus 读取 HTTP URI? [英] Retryable FTP & HTTP URI reading with Typhoeus?

查看:41
本文介绍了可重试的 FTP &使用 Typhoeus 读取 HTTP URI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ruby 的 'open_uri' 是否在读取或失败后可靠地关闭套接字?",我想更深入地研究这一点.

Having discussed some failure handling in "Does Ruby's 'open_uri' reliably close sockets after read or on fail?", I wanted to dig into this a little deeper.

我想尝试从 FTP 服务器拉取数据,如果失败,请尝试从 http 服务器拉取数据.如果这些都失败了,我想循环并尝试重试几次,两次尝试之间有短暂的停顿(可能是 1 秒)

I'd like to attempt to pull data from an FTP server, then if that fails, attempt a pull from an http server. If these both fail, I'd like to cycle around and attempt a retry several times with a short pause in between the attempts (perhaps 1 second)

我在在 Ruby 中重试代码块(在异常情况下,无论如何)",但是 retryable-rb 可能更健壮.

I read about the "retryable" method in "Retrying code blocks in Ruby (on exceptions, whatever)", however retryable-rb may be more robust.

很高兴在这种情况下看到一个来自旧帽子的例子,因为我需要一个可靠的机制来从我拥有的几个半不可靠的来源中剔除数据.如另一个线程中所述,Typhoeus 似乎可以为该解决方案提供强大的组件.>

Would appreciate seeing an example from an old hat at this scenario as I need a reliable mechanism in place for culling data from a couple of semi-unreliable sources I have. As noted in the other thread, it seems that Typhoeus could offer a robust component to this solution.

推荐答案

使用其中一个 gem 可能是个好主意,但没有它们也很简单:

Using one of those gems might be a good idea but it''s pretty straightforward without them:

data = nil
until data
# or 5.times do
    data = open(ftp_url){|f| f.read} rescue nil
    data ||= open(http_url){|f| f.read} rescue nil
    break if data
    sleep 1
end

这篇关于可重试的 FTP &使用 Typhoeus 读取 HTTP URI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆