Last active
July 31, 2020 14:01
-
-
Save adamhooper/d32dcc7d4fd54ff493936ef9a925d1dc to your computer and use it in GitHub Desktop.
2016-07-31 Code that avoids Timeout::timeout()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/http' | |
#require 'timeout' | |
require 'mysql2' # https://github.com/brianmario/mysql2 | |
def download_to_database(url, sql_statement) | |
res = Net::HTTP.start( | |
url.host, | |
url.port, | |
use_ssl: url.scheme == 'https', | |
open_timeout: 5, | |
read_timeout: 5, | |
ssl_timeout: 5 | |
) { |http| http.request(Net::HTTP::Get.new(url)) } | |
sql_statement.execute(url.to_s, res.code, res.body) | |
end | |
client = Mysql2::Client.new(read_timeout: 3, write_timeout: 3, ...) | |
statement = client.prepare( | |
"INSERT INTO http_responses (url, status_code, body) VALUES (?, ?, ?)" | |
) | |
download_to_database(URI('http://example.com'), statement) | |
download_to_database(URI('http://example.org'), statement) | |
# ... et cetera |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
#download_to_database
appears to have a bug - should read as:note replacing
uri
withurl
andreq
tourl
. As originally written I couldn't get anything to work with it.