您可以创建另一个线程来查看下载进度并在下载出现无响应时使应用程序崩溃。由于 Net::SFTP 允许您将自定义处理程序传递给该download!
方法,因此您可以像这样设置观察者线程:
class CustomHandler
def extend_time
@crash_time = Time.now + 30
end
# called when downloading has started
def on_open(downloader, file)
extend_time
downloader_thread = Thread.current
@watcher_thread = Thread.new{
while true do
if Time.now > @crash_time
downloader_thread.raise "Downloading appears unresponsive. Network disconnected?"
end
sleep 5
end
}
end
# called when new bytes are downloaded
def on_get(downloader, file, offset, data)
extend_time
end
# called when downloading is completed
def on_close(downloader, file)
@watcher_thread.exit
end
end
并且不要忘记像这样传入自定义处理程序:
sftp.download!(remote_path, local_path, :progress => CustomHandler.new)