Merge pull request #14221 from MikeMcQuaid/api_formula_handle_corruption
api/formula: handle JSON file corruption.
This commit is contained in:
commit
69ef011985
@ -10,6 +10,8 @@ module Homebrew
|
|||||||
class << self
|
class << self
|
||||||
extend T::Sig
|
extend T::Sig
|
||||||
|
|
||||||
|
MAX_RETRIES = 3
|
||||||
|
|
||||||
sig { returns(String) }
|
sig { returns(String) }
|
||||||
def formula_api_path
|
def formula_api_path
|
||||||
"formula"
|
"formula"
|
||||||
@ -29,13 +31,24 @@ module Homebrew
|
|||||||
sig { returns(Hash) }
|
sig { returns(Hash) }
|
||||||
def all_formulae
|
def all_formulae
|
||||||
@all_formulae ||= begin
|
@all_formulae ||= begin
|
||||||
curl_args = %w[--compressed --silent https://formulae.brew.sh/api/formula.json]
|
retry_count = 0
|
||||||
|
|
||||||
|
url = "https://formulae.brew.sh/api/formula.json"
|
||||||
|
json_formulae = begin
|
||||||
|
curl_args = %W[--compressed --silent #{url}]
|
||||||
if cached_formula_json_file.exist? && !cached_formula_json_file.empty?
|
if cached_formula_json_file.exist? && !cached_formula_json_file.empty?
|
||||||
curl_args.prepend("--time-cond", cached_formula_json_file)
|
curl_args.prepend("--time-cond", cached_formula_json_file)
|
||||||
end
|
end
|
||||||
curl_download(*curl_args, to: cached_formula_json_file, max_time: 5)
|
curl_download(*curl_args, to: cached_formula_json_file, max_time: 5)
|
||||||
|
|
||||||
json_formulae = JSON.parse(cached_formula_json_file.read)
|
JSON.parse(cached_formula_json_file.read)
|
||||||
|
rescue JSON::ParserError
|
||||||
|
cached_formula_json_file.unlink
|
||||||
|
retry_count += 1
|
||||||
|
odie "Cannot download non-corrupt #{url}!" if retry_count > MAX_RETRIES
|
||||||
|
|
||||||
|
retry
|
||||||
|
end
|
||||||
|
|
||||||
@all_aliases = {}
|
@all_aliases = {}
|
||||||
json_formulae.to_h do |json_formula|
|
json_formulae.to_h do |json_formula|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user