diff --git a/SS14.Watchdog/Components/ServerManagement/ServerInstance.Actor.cs b/SS14.Watchdog/Components/ServerManagement/ServerInstance.Actor.cs index 884ecbf..88a6ab1 100644 --- a/SS14.Watchdog/Components/ServerManagement/ServerInstance.Actor.cs +++ b/SS14.Watchdog/Components/ServerManagement/ServerInstance.Actor.cs @@ -416,16 +416,36 @@ private async Task StartRunUpdate(CancellationToken cancel) if (_updateProvider == null) return; - var hasUpdate = await _updateProvider.CheckForUpdateAsync(_currentRevision, cancel); - _logger.LogDebug("Update available: {available}.", hasUpdate); + bool hasUpdate; + try + { + _logger.LogTrace("Checking for update..."); + hasUpdate = await _updateProvider.CheckForUpdateAsync(_currentRevision, cancel); + _logger.LogDebug("Update available: {available}.", hasUpdate); + } + catch (Exception e) + { + _logger.LogError(e, "Error while checking for update!"); + return; + } if (!hasUpdate) return; - var newRevision = await _updateProvider.RunUpdateAsync( - _currentRevision, - Path.Combine(InstanceDir, "bin"), - cancel); + _logger.LogTrace("Starting update..."); + string? newRevision; + try + { + newRevision = await _updateProvider.RunUpdateAsync( + _currentRevision, + Path.Combine(InstanceDir, "bin"), + cancel); + } + catch (Exception e) + { + _logger.LogError(e, "Uncaught error while updating server"); + return; + } if (newRevision != null) { diff --git a/SS14.Watchdog/Components/Updates/UpdateProviderManifest.cs b/SS14.Watchdog/Components/Updates/UpdateProviderManifest.cs index 817e187..68154b6 100644 --- a/SS14.Watchdog/Components/Updates/UpdateProviderManifest.cs +++ b/SS14.Watchdog/Components/Updates/UpdateProviderManifest.cs @@ -90,33 +90,36 @@ public override async Task CheckForUpdateAsync( _logger.LogTrace("Downloading server binary from {Download} to {TempFile}", downloadUrl, tempFile.Name); - // Download to file... - var timeout = Task.Delay(TimeSpan.FromSeconds(DownloadTimeoutSeconds), cancel); - var downloadTask = Task.Run(async () => + var timeoutTcs = CancellationTokenSource.CreateLinkedTokenSource(cancel); + timeoutTcs.CancelAfter(TimeSpan.FromSeconds(DownloadTimeoutSeconds)); + + try { using var resp = await _httpClient.SendAsync( MakeAuthenticatedGet(downloadUrl), HttpCompletionOption.ResponseHeadersRead, - cancel); + timeoutTcs.Token); + + _logger.LogTrace("Received HTTP response, starting download"); resp.EnsureSuccessStatusCode(); - await resp.Content.CopyToAsync(tempFile, cancel); - }, cancel); + await resp.Content.CopyToAsync(tempFile, timeoutTcs.Token); - if (await Task.WhenAny(downloadTask, timeout) == timeout) + _logger.LogTrace("Update file downloaded to disk"); + } + catch (OperationCanceledException) { - await timeout; // Throws cancellation if cancellation requested. _logger.LogError("Timeout while downloading: {Timeout} seconds", DownloadTimeoutSeconds); - return null; + throw; } - await downloadTask; + // Download to file... // Verify hash because why not? - using var hash = SHA256.Create(); + _logger.LogTrace("Verifying hash of download file..."); tempFile.Seek(0, SeekOrigin.Begin); - var hashOutput = await hash.ComputeHashAsync(tempFile, cancel); + var hashOutput = await SHA256.HashDataAsync(tempFile, cancel); if (!downloadHash.AsSpan().SequenceEqual(hashOutput)) {