Skip to content

Commit

Permalink
More logging and error resilience to updating system
Browse files Browse the repository at this point in the history
Also code cleanup
  • Loading branch information
PJB3005 committed Aug 26, 2024
1 parent 4126b85 commit 0f28774
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
32 changes: 26 additions & 6 deletions SS14.Watchdog/Components/ServerManagement/ServerInstance.Actor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
27 changes: 15 additions & 12 deletions SS14.Watchdog/Components/Updates/UpdateProviderManifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,33 +90,36 @@ public override async Task<bool> 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))
{
Expand Down

0 comments on commit 0f28774

Please sign in to comment.