Permalink
Browse files

Set some reasonable default download timeouts so that server problems…

… don't block downloads for an extremely long time
  • Loading branch information...
FooBarWidget committed Jan 13, 2014
1 parent dd0265d commit 440818da48debbb7dbbce83b385fa9dcdfe8a74e
@@ -1,6 +1,6 @@
#!/usr/bin/env ruby
# Phusion Passenger - https://www.phusionpassenger.com/
# Copyright (c) 2010-2013 Phusion
# Copyright (c) 2010-2014 Phusion
#
# "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
#
@@ -64,27 +64,31 @@
# Initiate downloads
require 'phusion_passenger/utils/download'
require 'logger'
def download(name)
if !File.exist?(name)
url = "#{PhusionPassenger::BINARIES_URL_ROOT}/#{PhusionPassenger::VERSION_STRING}/#{name}"
cert = PhusionPassenger.binaries_ca_cert_path
puts "Attempting to download #{url} into #{Dir.pwd}"
File.unlink("#{name}.tmp") rescue nil
logger = Logger.new(STDOUT)
logger.level = Logger::WARN
logger.formatter = proc { |severity, datetime, progname, msg| "*** #{msg}\n" }
result = PhusionPassenger::Utils::Download.download(url, "#{name}.tmp",
:cacert => cert, :logger => logger)
if result
File.rename("#{name}.tmp", name)
else
File.unlink("#{name}.tmp") rescue nil
end
else
def download(name, options = {})
if File.exist?(name)
puts "#{Dir.pwd}/#{name} already exists"
return
end
url = "#{PhusionPassenger::BINARIES_URL_ROOT}/#{PhusionPassenger::VERSION_STRING}/#{name}"
cert = PhusionPassenger.binaries_ca_cert_path
puts "Attempting to download #{url} into #{Dir.pwd}"
File.unlink("#{name}.tmp") rescue nil
logger = Logger.new(STDOUT)
logger.level = Logger::WARN
logger.formatter = proc { |severity, datetime, progname, msg| "*** #{msg}\n" }
options = {
:cacert => cert,
:logger => logger
}.merge(options)
result = PhusionPassenger::Utils::Download.download(url, "#{name}.tmp", options)
if result
File.rename("#{name}.tmp", name)
else
File.unlink("#{name}.tmp") rescue nil
end
end
download "support-#{cxx_compat_id}.tar.gz"
download "nginx-#{PhusionPassenger::PREFERRED_NGINX_VERSION}-#{cxx_compat_id}.tar.gz"
download "rubyext-#{ruby_compat_id}.tar.gz"
download "rubyext-#{ruby_compat_id}.tar.gz", :total_timeout => 10
download "webhelper-#{PhusionPassenger::PREFERRED_NGINX_VERSION}-#{cxx_compat_id}.tar.gz", :total_timeout => 120
download "support-#{cxx_compat_id}.tar.gz", :total_timeout => 900
@@ -143,10 +143,11 @@ def download_binary_and_load
PhusionPassenger.require_passenger_lib 'platform_info/ruby'
PhusionPassenger.require_passenger_lib 'utils/tmpio'
PhusionPassenger.require_passenger_lib 'utils/download'
PhusionPassenger::Utils.mktmpdir("passenger-native-support-") do |dir|
Dir.chdir(dir) do
basename = "rubyext-#{archdir}.tar.gz"
if !download(basename, dir)
if !download(basename, dir, :total_timeout => 30)
return false
end
@@ -238,16 +239,18 @@ def installation_target_dirs
return target_dirs
end
def download(name, output_dir)
def download(name, output_dir, options = {})
url = "#{PhusionPassenger::BINARIES_URL_ROOT}/#{PhusionPassenger::VERSION_STRING}/#{name}"
filename = "#{output_dir}/#{name}"
logger = Logger.new(STDERR)
logger.level = Logger::WARN
logger.formatter = proc { |severity, datetime, progname, msg| " #{msg}\n" }
return PhusionPassenger::Utils::Download.download(url, filename,
options.merge!(
:cacert => PhusionPassenger.binaries_ca_cert_path,
:use_cache => true,
:logger => logger)
:logger => logger
)
return PhusionPassenger::Utils::Download.download(url, filename, options)
end
def mkdir(dir)
@@ -1,6 +1,6 @@
# encoding: utf-8
# Phusion Passenger - https://www.phusionpassenger.com/
# Copyright (c) 2013 Phusion
# Copyright (c) 2013-2014 Phusion
#
# "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
#
@@ -51,7 +51,20 @@ def self.included(klass)
# The default is to use the download tool's down CA database.
# use_cache: Whether to copy the file from the download cache, if available.
# Default: false.
# connect_timeout: The maximum amount of time to spend on DNS lookup
# and establishing the TCP connection. Set to nil to
# disable this timeout. Default: 4.
# idle_timeout: The maximum idle read time. Set to nil to set this timeout
# to the default wget value, 900. Set to nil to disable this
# timeout. Default: 5.
# total_timeout: The maximum amount of time spent on the whole download
# operation, including connection time. Only has effect on curl.
# Set to nil to disable this timeout. Default: nil.
def download(url, output, options = {})
options = {
:connect_timeout => 4,
:idle_timeout => 5
}.merge(options)
logger = options[:logger] || Logger.new(STDERR)
if options[:use_cache] && cache_dir = PhusionPassenger.download_cache_dir
@@ -90,6 +103,20 @@ def download_with_curl(logger, url, output, options)
command << "--cacert"
command << options[:cacert]
end
if options[:connect_timeout]
command << "--connect-timeout"
command << options[:connect_timeout].to_s
end
if options[:idle_timeout]
command << "--speed-time"
command << options[:idle_timeout].to_s
command << "--speed-limit"
command << "1"
end
if options[:total_timeout]
command << "--max-time"
command << options[:total_timeout].to_s
end
command << url
command_str = Shellwords.join(command)
logger.info("Invoking: #{command_str}")
@@ -147,13 +174,20 @@ def remove_curl_output_prefix(line)
end
def download_with_wget(logger, url, output, options)
command = ["wget", "--tries=3", "-O", output]
command = ["wget", "--tries=1", "-O", output]
if !options[:show_progress]
command << "-nv"
end
if options[:cacert]
command << "--ca-certificate=#{options[:cacert]}"
end
if options[:connect_timeout]
command << "--dns-timeout=#{options[:connect_timeout]}"
command << "--connect-timeout=#{options[:connect_timeout]}"
end
if options[:idle_timeout]
command << "--timeout=#{options[:idle_timeout]}"
end
command << url
command_str = Shellwords.join(command)
logger.info("Invoking: #{command_str}")

3 comments on commit 440818d

@aar0nr

This comment has been minimized.

Show comment
Hide comment
@aar0nr

aar0nr Mar 12, 2014

How can --connect-timeout be configured with passenger-install-nginx-module? 4 seconds isn't long enough for my Vagrant Ubuntu machine.

aar0nr replied Mar 12, 2014

How can --connect-timeout be configured with passenger-install-nginx-module? 4 seconds isn't long enough for my Vagrant Ubuntu machine.

@FooBarWidget

This comment has been minimized.

Show comment
Hide comment
@FooBarWidget

FooBarWidget Mar 17, 2014

Member

Even 4 seconds is not enough? What timeouts do you need? Have you considered pre-downloading the Nginx source tarball and passing it to passenger-install-nginx-module through parameters? See --help.

Member

FooBarWidget replied Mar 17, 2014

Even 4 seconds is not enough? What timeouts do you need? Have you considered pre-downloading the Nginx source tarball and passing it to passenger-install-nginx-module through parameters? See --help.

@aar0nr

This comment has been minimized.

Show comment
Hide comment
@aar0nr

aar0nr Mar 17, 2014

I could do that. However, it also times out when trying to download PCRE, so I'd have to install too. Just to test, I changed the timeout to 10 and it worked. I'm not suggesting bumping the default value, but possibly configure it some how. No worries... for the meantime, I'll just pre-install PCRE and pass in the tarball for Nginx. Not as pretty as one command, but it gets the job done 😄

aar0nr replied Mar 17, 2014

I could do that. However, it also times out when trying to download PCRE, so I'd have to install too. Just to test, I changed the timeout to 10 and it worked. I'm not suggesting bumping the default value, but possibly configure it some how. No worries... for the meantime, I'll just pre-install PCRE and pass in the tarball for Nginx. Not as pretty as one command, but it gets the job done 😄

Please sign in to comment.