From 73b596a99e970be7ba80ded78f065413ec71b1ca Mon Sep 17 00:00:00 2001 From: Tim Pilius Date: Fri, 3 May 2024 11:15:58 -0400 Subject: [PATCH] When a request fails it will be retried with the nocache url parameter, which will make the cache redownload a request and overwrite the data currently saved on disk. --- EpicPrefill/Handlers/DownloadHandler.cs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/EpicPrefill/Handlers/DownloadHandler.cs b/EpicPrefill/Handlers/DownloadHandler.cs index 796c2f8..91b186f 100644 --- a/EpicPrefill/Handlers/DownloadHandler.cs +++ b/EpicPrefill/Handlers/DownloadHandler.cs @@ -51,11 +51,12 @@ await _ansiConsole.CreateSpectreProgress(TransferSpeedUnit.Bits).StartAsync(asyn failedRequests = await AttemptDownloadAsync(ctx, "Downloading..", queuedRequests, new Uri(allManifestUrls.First().ManifestDownloadUrl)); // Handle any failed requests - while (failedRequests.Any() && retryCount < 3) + while (failedRequests.Any() && retryCount < 2) { retryCount++; await Task.Delay(2000 * retryCount); - failedRequests = await AttemptDownloadAsync(ctx, $"Retrying {retryCount}..", failedRequests.ToList(), new Uri(allManifestUrls.First().ManifestDownloadUrl)); + var upstreamCdn = new Uri(allManifestUrls.First().ManifestDownloadUrl); + failedRequests = await AttemptDownloadAsync(ctx, $"Retrying {retryCount}..", failedRequests.ToList(), upstreamCdn, forceRecache: true); } }); @@ -70,13 +71,15 @@ await _ansiConsole.CreateSpectreProgress(TransferSpeedUnit.Bits).StartAsync(asyn return false; } - + //TODO I don't like the number of parameters here, should maybe rethink the way this is written. /// - /// Attempts to download the specified requests. Returns a list of any requests that have failed. + /// Attempts to download the specified requests. Returns a list of any requests that have failed for any reason. /// + /// When specified, will cause the cache to delete the existing cached data for a request, and redownload it again. /// A list of failed requests [SuppressMessage("Reliability", "CA2016:Forward the 'CancellationToken' parameter to methods", Justification = "Don't have a need to cancel")] - private async Task> AttemptDownloadAsync(ProgressContext ctx, string taskTitle, List requestsToDownload, Uri upstreamCdn) + private async Task> AttemptDownloadAsync(ProgressContext ctx, string taskTitle, List requestsToDownload, + Uri upstreamCdn, bool forceRecache = false) { double requestTotalSize = requestsToDownload.Sum(e => (long)e.DownloadSizeBytes); var progressTask = ctx.AddTask(taskTitle, new ProgressTaskSettings { MaxValue = requestTotalSize }); @@ -89,6 +92,10 @@ private async Task> AttemptDownloadAsync(ProgressCo try { var url = Path.Join($"http://{_lancacheAddress}", chunk.DownloadUrl); + if (forceRecache) + { + url += "?nocache=1"; + } using var requestMessage = new HttpRequestMessage(HttpMethod.Get, url); requestMessage.Headers.Host = upstreamCdn.Host; @@ -102,9 +109,10 @@ private async Task> AttemptDownloadAsync(ProgressCo { } } - catch + catch (Exception e) { failedRequests.Add(chunk); + FileLogger.LogExceptionNoStackTrace($"Request {chunk.DownloadUrl}", e); } progressTask.Increment(chunk.DownloadSizeBytes); });