From 7022f981e26eb5c59c00c0cc40d9ceedbbcf0206 Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Thu, 10 Jan 2019 15:23:59 -0800 Subject: [PATCH] Check response code, retry when downloading docs (#26386) When downloading the ObjC/Java API docs, check the HTTP response status code and if not 200, attempt up to 5 times before giving up. --- dev/tools/java_and_objc_doc.dart | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/dev/tools/java_and_objc_doc.dart b/dev/tools/java_and_objc_doc.dart index 4ae5bf93f1..34f128b88a 100644 --- a/dev/tools/java_and_objc_doc.dart +++ b/dev/tools/java_and_objc_doc.dart @@ -4,6 +4,7 @@ import 'dart:async'; import 'dart:io'; +import 'dart:math'; import 'package:archive/archive.dart'; import 'package:http/http.dart' as http; @@ -22,10 +23,34 @@ Future main(List args) async { generateDocs(objcdocUrl, 'objcdoc', 'Classes/FlutterViewController.html'); } -Future generateDocs(String url, String docName, String checkFile) async { - final http.Response response = await http.get(url); +/// Fetches the zip archive at the specified url. +/// +/// Returns null if the archive fails to download after [maxTries] attempts. +Future fetchArchive(String url, int maxTries) async { + List responseBytes; + for (int i = 0; i < maxTries; i++) { + final http.Response response = await http.get(url); + if (response.statusCode == 200) { + responseBytes = response.bodyBytes; + break; + } + stderr.writeln('Failed attempt ${i+1} to fetch $url.'); - final Archive archive = ZipDecoder().decodeBytes(response.bodyBytes); + // On failure print a short snipped from the body in case it's helpful. + final int bodyLength = min(80, response.body.length); + stderr.writeln('Response status code ${response.statusCode}. Body: ' + response.body.substring(0, bodyLength)); + sleep(const Duration(seconds: 1)); + } + return responseBytes == null ? null : ZipDecoder().decodeBytes(responseBytes); +} + +Future generateDocs(String url, String docName, String checkFile) async { + const int maxTries = 5; + final Archive archive = await fetchArchive(url, maxTries); + if (archive == null) { + stderr.writeln('Failed to fetch zip archive from: $url after $maxTries attempts. Giving up.'); + exit(1); + } final Directory output = Directory('$kDocRoot/$docName'); print('Extracting $docName to ${output.path}');