modulePod classSource classAggregate # Aggregate 聚合的所有 source attr_reader:sources definitialize(sources) raise"Cannot initialize an aggregate with a nil source: (#{sources})"if sources.include?(nil) @sources = sources end # 查找依赖 defsearch(dependency) found_sources = sources.select { |s| s.search(dependency) } unless found_sources.empty? Specification::Set.new(dependency.root_name, found_sources) end end end end end
download_task = download_typhoeus_impl_async(file_remote_url, etag).thendo |response| case response.response_code when301, 302 # 使用 CDN 分发的重定向位置,重新请求 redirect_location = response.headers['location'] debug "CDN: #{name} Redirecting from #{file_remote_url} to #{redirect_location}" download_and_save_with_retries_async(partial_url, redirect_location, etag) when304 # 根据 etag 判断远端 txt 文件未修改,则使用本地文件 debug "CDN: #{name} Relative path not modified: #{partial_url}" # We need to update the file modification date, as it is later used for freshness # optimization. See #initialize for more information. FileUtils.touch path partial_url when200 # 获取远端最新 txt 文件,并更新 etag 文件内容 File.open(path, 'w') { |f| f.write(response.response_body.force_encoding('UTF-8')) }
etag_new = response.headers['etag'] unless response.headers.nil? debug "CDN: #{name} Relative path downloaded: #{partial_url}, save ETag: #{etag_new}" File.open(etag_path, 'w') { |f| f.write(etag_new) } unless etag_new.nil? partial_url when404 debug "CDN: #{name} Relative path couldn't be downloaded: #{partial_url} Response: #{response.response_code}" nil when502, 503, 504 #服务器出错,重试 if retries <= 1 raiseInformative, "CDN: #{name} URL couldn't be downloaded: #{file_remote_url} Response: #{response.response_code}#{response.response_body}" else debug "CDN: #{name} URL couldn't be downloaded: #{file_remote_url} Response: #{response.response_code}#{response.response_body}, retries: #{retries - 1}" exponential_backoff_async(retries).thendo download_and_save_with_retries_async(partial_url, file_remote_url, etag, retries - 1) end end when0 # 网络层错误,重试 if retries <= 1 raiseInformative, "CDN: #{name} URL couldn't be downloaded: #{file_remote_url} Response: #{response.return_message}" else debug "CDN: #{name} URL couldn't be downloaded: #{file_remote_url} Response: #{response.return_message}, retries: #{retries - 1}" exponential_backoff_async(retries).thendo download_and_save_with_retries_async(partial_url, file_remote_url, etag, retries - 1) end end else raiseInformative, "CDN: #{name} URL couldn't be downloaded: #{file_remote_url} Response: #{response.response_code}#{response.response_body}" end end
预览: