Skip to content

Instantly share code, notes, and snippets.

@adamhooper
Last active July 31, 2020 14:01
Show Gist options
  • Save adamhooper/d32dcc7d4fd54ff493936ef9a925d1dc to your computer and use it in GitHub Desktop.
Save adamhooper/d32dcc7d4fd54ff493936ef9a925d1dc to your computer and use it in GitHub Desktop.
2016-07-31 Code that avoids Timeout::timeout()
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
@adamhooper
Copy link
Author

Thanks! Fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment