Skip to content

Commit

Permalink
Merge pull request #14589 from MikeMcQuaid/api_auto_update_secs
Browse files Browse the repository at this point in the history
Add HOMEBREW_API_AUTO_UPDATE_SECS
  • Loading branch information
MikeMcQuaid authored Feb 11, 2023
2 parents 5cfb179 + 75a2068 commit 1cf53b4
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
21 changes: 18 additions & 3 deletions Library/Homebrew/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,31 @@ def fetch_json_api_file(endpoint, target:)
retry_count = 0
url = "#{Homebrew::EnvConfig.api_domain}/#{endpoint}"
default_url = "#{HOMEBREW_API_DEFAULT_DOMAIN}/#{endpoint}"

# TODO: consider using more of Utils::Curl
curl_args = %W[
--compressed
--speed-limit #{ENV.fetch("HOMEBREW_CURL_SPEED_LIMIT")}
--speed-time #{ENV.fetch("HOMEBREW_CURL_SPEED_TIME")}
]
curl_args.prepend("--silent") unless Context.current.debug?
curl_args << "--progress-bar" unless Context.current.verbose?
curl_args << "--verbose" if Homebrew::EnvConfig.curl_verbose?
curl_args << "--silent" unless $stdout.tty?

skip_download = target.exist? &&
!target.empty? &&
(Homebrew::EnvConfig.no_auto_update? ||
((Time.now - Homebrew::EnvConfig.api_auto_update_secs.to_i) < target.mtime))

begin
begin
args = curl_args.dup
args.prepend("--time-cond", target) if target.exist? && !target.empty?
# Disable retries here, we handle them ourselves below.
Utils::Curl.curl_download(*args, url, to: target, retries: 0, show_error: false)
unless skip_download
ohai "Downloading #{url}" if $stdout.tty?
# Disable retries here, we handle them ourselves below.
Utils::Curl.curl_download(*args, url, to: target, retries: 0, show_error: false)
end
rescue ErrorDuringExecution
if url == default_url
raise unless target.exist?
Expand All @@ -64,17 +76,20 @@ def fetch_json_api_file(endpoint, target:)
# This block will be executed only once, because we set `url` to `default_url`
url = default_url
target.unlink if target.exist? && target.empty?
skip_download = false

retry
end

opoo "#{target.basename}: update failed, falling back to cached version."
end

FileUtils.touch target
JSON.parse(target.read)
rescue JSON::ParserError
target.unlink
retry_count += 1
skip_download = false
odie "Cannot download non-corrupt #{url}!" if retry_count > Homebrew::EnvConfig.curl_retries.to_i

retry
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/brew.sh
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ auto-update() {
fi
fi

# Skip auto-update if the repository has been updated in the
# Skip auto-update if the Homebrew/brew repository has been checked in the
# last $HOMEBREW_AUTO_UPDATE_SECS.
repo_fetch_head="${HOMEBREW_REPOSITORY}/.git/FETCH_HEAD"
if [[ -f "${repo_fetch_head}" ]] &&
Expand Down
6 changes: 6 additions & 0 deletions Library/Homebrew/env_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ module EnvConfig
"to instead be downloaded from " \
"`http://localhost:8080/v2/homebrew/core/gettext/manifests/0.21`",
},
HOMEBREW_API_AUTO_UPDATE_SECS: {
description: "Check Homebrew's API for new formulae or cask data every " \
"`HOMEBREW_API_AUTO_UPDATE_SECS` seconds. Alternatively, disable API auto-update " \
"checks entirely with HOMEBREW_NO_AUTO_UPDATE.",
default: 1800,
},
HOMEBREW_AUTO_UPDATE_SECS: {
description: "Run `brew update` once every `HOMEBREW_AUTO_UPDATE_SECS` seconds before some commands, " \
"e.g. `brew install`, `brew upgrade` and `brew tap`. Alternatively, " \
Expand Down
5 changes: 5 additions & 0 deletions docs/Manpage.md
Original file line number Diff line number Diff line change
Expand Up @@ -1995,6 +1995,11 @@ example, run `export HOMEBREW_NO_INSECURE_REDIRECT=1` rather than just
- `HOMEBREW_ARTIFACT_DOMAIN`
<br>Prefix all download URLs, including those for bottles, with this value. For example, `HOMEBREW_ARTIFACT_DOMAIN=http://localhost:8080` will cause a formula with the URL `https://example.com/foo.tar.gz` to instead download from `http://localhost:8080/https://example.com/foo.tar.gz`. Bottle URLs however, have their domain replaced with this prefix. This results in e.g. `https://ghcr.io/v2/homebrew/core/gettext/manifests/0.21` to instead be downloaded from `http://localhost:8080/v2/homebrew/core/gettext/manifests/0.21`

- `HOMEBREW_API_AUTO_UPDATE_SECS`
<br>Check Homebrew's API for new formulae or cask data every `HOMEBREW_API_AUTO_UPDATE_SECS` seconds. Alternatively, disable API auto-update checks entirely with HOMEBREW_NO_AUTO_UPDATE.

*Default:* `1800`.

- `HOMEBREW_AUTO_UPDATE_SECS`
<br>Run `brew update` once every `HOMEBREW_AUTO_UPDATE_SECS` seconds before some commands, e.g. `brew install`, `brew upgrade` and `brew tap`. Alternatively, disable auto-update entirely with `HOMEBREW_NO_AUTO_UPDATE`.

Expand Down
9 changes: 9 additions & 0 deletions manpages/brew.1
Original file line number Diff line number Diff line change
Expand Up @@ -2849,6 +2849,15 @@ Linux only: Pass this value to a type name representing the compiler\'s \fB\-mar
Prefix all download URLs, including those for bottles, with this value\. For example, \fBHOMEBREW_ARTIFACT_DOMAIN=http://localhost:8080\fR will cause a formula with the URL \fBhttps://example\.com/foo\.tar\.gz\fR to instead download from \fBhttp://localhost:8080/https://example\.com/foo\.tar\.gz\fR\. Bottle URLs however, have their domain replaced with this prefix\. This results in e\.g\. \fBhttps://ghcr\.io/v2/homebrew/core/gettext/manifests/0\.21\fR to instead be downloaded from \fBhttp://localhost:8080/v2/homebrew/core/gettext/manifests/0\.21\fR
.
.TP
\fBHOMEBREW_API_AUTO_UPDATE_SECS\fR
.
.br
Check Homebrew\'s API for new formulae or cask data every \fBHOMEBREW_API_AUTO_UPDATE_SECS\fR seconds\. Alternatively, disable API auto\-update checks entirely with HOMEBREW_NO_AUTO_UPDATE\.
.
.IP
\fIDefault:\fR \fB1800\fR\.
.
.TP
\fBHOMEBREW_AUTO_UPDATE_SECS\fR
.
.br
Expand Down

0 comments on commit 1cf53b4

Please sign in to comment.