Skip to content

Instantly share code, notes, and snippets.

@roidrage
Last active December 17, 2015 04:48
Show Gist options
  • Select an option

  • Save roidrage/5552953 to your computer and use it in GitHub Desktop.

Select an option

Save roidrage/5552953 to your computer and use it in GitHub Desktop.
Patch to add retries to Bundler for Gem installations. Drop this bit in your Gemfile to test it out. Tested with Bundler 1.3.x only
require 'bundler/installer'
module ::Bundler
class Installer < Environment
MAX_RETRIES = 3
def install_gem_from_spec(spec, standalone = false)
retries = 1
begin
# Download the gem to get the spec, because some specs that are returned
# by rubygems.org are broken and wrong.
Bundler::Fetcher.fetch(spec) if spec.source.is_a?(Bundler::Source::Rubygems)
# Fetch the build settings, if there are any
settings = Bundler.settings["build.#{spec.name}"]
Bundler.rubygems.with_build_args [settings] do
spec.source.install(spec)
Bundler.ui.debug "from #{spec.loaded_from} "
end
# newline comes after installing, some gems say "with native extensions"
Bundler.ui.info ""
if Bundler.settings[:bin] && standalone
generate_standalone_bundler_executable_stubs(spec)
elsif Bundler.settings[:bin]
generate_bundler_executable_stubs(spec, :force => true)
end
FileUtils.rm_rf(Bundler.tmp)
rescue Gem::RemoteFetcher::FetchError => e
if retries <= MAX_RETRIES
Bundler.ui.warn "#{e.class}: #{e.message}"
Bundler.ui.warn "Installing #{spec.name} (#{spec.version}) failed."
Bundler.ui.warn "Retrying (#{retries}/#{MAX_RETRIES})"
retries += 1
sleep retries
retry
else
Bundler.ui.warn "Installing #{spec.name} (#{spec.version}) failed after #{retries} retries: #{e.message}."
Bundler.ui.warn "Giving up"
msg = "An error, most likely because of network issues, has occurred trying to install #{spec.name} (#{spec.version}), "
msg << "and Bundler cannot continue."
raise Bundler::InstallError, msg
end
end
rescue Exception => e
puts e.backtrace.join("\n")
# install hook failed
raise e if e.is_a?(Bundler::InstallHookError) || e.is_a?(Bundler::SecurityError)
# other failure, likely a native extension build failure
Bundler.ui.info ""
Bundler.ui.warn "#{e.class}: #{e.message}"
msg = "An error occurred while installing #{spec.name} (#{spec.version}),"
msg << " and Bundler cannot continue.\nMake sure that `gem install"
msg << " #{spec.name} -v '#{spec.version}'` succeeds before bundling."
Bundler.ui.debug e.backtrace.join("\n")
raise Bundler::InstallError, msg
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment