add a check for queue status to avoid hanging downloads

This commit is contained in:
Stanley Dimant
2023-01-18 11:04:21 +01:00
parent ed3562b170
commit f8ca9fed67
2 changed files with 37 additions and 9 deletions

Submodule MareAPI updated: 5ac8a753dd...9fea4c30da

View File

@@ -60,24 +60,52 @@ public partial class ApiController
return requestId; return requestId;
} }
private async Task WaitForDownloadReady(DownloadFileTransfer downloadFileTransfer, Guid requestId, CancellationToken ct) private async Task WaitForDownloadReady(DownloadFileTransfer downloadFileTransfer, Guid requestId, CancellationToken downloadCt)
{ {
bool alreadyCancelled = false; bool alreadyCancelled = false;
try try
{ {
while (!ct.IsCancellationRequested && _downloadReady.TryGetValue(requestId, out bool isReady) && !isReady) CancellationTokenSource localTimeoutCts = new CancellationTokenSource();
localTimeoutCts.CancelAfter(TimeSpan.FromSeconds(5));
CancellationTokenSource composite = CancellationTokenSource.CreateLinkedTokenSource(downloadCt, localTimeoutCts.Token);
while (_downloadReady.TryGetValue(requestId, out bool isReady) && !isReady)
{ {
Logger.Verbose($"Waiting for {requestId} to become ready for download"); try
await Task.Delay(250, ct).ConfigureAwait(false); {
await Task.Delay(250, composite.Token).ConfigureAwait(false);
}
catch (TaskCanceledException)
{
if (downloadCt.IsCancellationRequested) throw;
var req = await SendRequestAsync<object>(HttpMethod.Get, MareFiles.RequestCheckQueueFullPath(downloadFileTransfer.DownloadUri, requestId, downloadFileTransfer.Hash), downloadCt).ConfigureAwait(false);
try
{
req.EnsureSuccessStatusCode();
localTimeoutCts.Dispose();
composite.Dispose();
localTimeoutCts = new CancellationTokenSource();
localTimeoutCts.CancelAfter(TimeSpan.FromSeconds(5));
composite = CancellationTokenSource.CreateLinkedTokenSource(downloadCt, localTimeoutCts.Token);
}
catch (HttpRequestException)
{
throw;
}
}
} }
localTimeoutCts.Dispose();
composite.Dispose();
Logger.Debug($"Download {requestId} ready"); Logger.Debug($"Download {requestId} ready");
} }
catch (TaskCanceledException) catch (TaskCanceledException)
{ {
try try
{ {
await SendRequestAsync<object>(HttpMethod.Get, MareFiles.RequestCancelFullPath(downloadFileTransfer.DownloadUri, requestId), ct).ConfigureAwait(false); await SendRequestAsync<object>(HttpMethod.Get, MareFiles.RequestCancelFullPath(downloadFileTransfer.DownloadUri, requestId), downloadCt).ConfigureAwait(false);
alreadyCancelled = true; alreadyCancelled = true;
} }
catch { } catch { }
@@ -86,11 +114,11 @@ public partial class ApiController
} }
finally finally
{ {
if (ct.IsCancellationRequested && !alreadyCancelled) if (downloadCt.IsCancellationRequested && !alreadyCancelled)
{ {
try try
{ {
await SendRequestAsync<object>(HttpMethod.Get, MareFiles.RequestCancelFullPath(downloadFileTransfer.DownloadUri, requestId), ct).ConfigureAwait(false); await SendRequestAsync<object>(HttpMethod.Get, MareFiles.RequestCancelFullPath(downloadFileTransfer.DownloadUri, requestId), downloadCt).ConfigureAwait(false);
} }
catch { } catch { }
} }
@@ -184,7 +212,7 @@ public partial class ApiController
{ {
requestMessage.Content = JsonContent.Create(content); requestMessage.Content = JsonContent.Create(content);
} }
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Authorization); requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", this.Authorization);
if (content != default) if (content != default)
{ {