Skip to content

Commit

Permalink
Resolve eternal TODO with ASF API update routine
Browse files Browse the repository at this point in the history
  • Loading branch information
JustArchi committed Oct 12, 2024
1 parent f427b89 commit f32fafd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
6 changes: 5 additions & 1 deletion ArchiSteamFarm/Core/ASF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
using System.Threading.Tasks;
using ArchiSteamFarm.Helpers;
using ArchiSteamFarm.IPC;
using ArchiSteamFarm.IPC.Controllers.Api;
using ArchiSteamFarm.Localization;
using ArchiSteamFarm.NLog;
using ArchiSteamFarm.Plugins;
Expand Down Expand Up @@ -894,11 +895,14 @@ private static async Task UpdateAndRestart() {

if (kestrelWasRunning) {
// We disable ArchiKestrel here as the update process moves the core files and might result in IPC crash
// TODO: It might fail if the update was triggered from the API, this should be something to improve in the future, by changing the structure into request -> return response -> finish update
ASFController.PendingVersionUpdate = newVersion;

try {
await ArchiKestrel.Stop().ConfigureAwait(false);
} catch (Exception e) {
ArchiLogger.LogGenericWarningException(e);
} finally {
ASFController.PendingVersionUpdate = null;
}
}

Expand Down
28 changes: 27 additions & 1 deletion ArchiSteamFarm/IPC/Controllers/Api/ASFController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,22 @@
using ArchiSteamFarm.Steam.Interaction;
using ArchiSteamFarm.Storage;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Hosting;

namespace ArchiSteamFarm.IPC.Controllers.Api;

[Route("Api/ASF")]
public sealed class ASFController : ArchiController {
internal static Version? PendingVersionUpdate { get; set; }

private readonly IHostApplicationLifetime ApplicationLifetime;

public ASFController(IHostApplicationLifetime applicationLifetime) {
ArgumentNullException.ThrowIfNull(applicationLifetime);

ApplicationLifetime = applicationLifetime;
}

/// <summary>
/// Encrypts data with ASF encryption mechanisms using provided details.
/// </summary>
Expand Down Expand Up @@ -185,7 +196,22 @@ public async Task<ActionResult<GenericResponse<string>>> UpdatePost([FromBody] U
return BadRequest(new GenericResponse(false, Strings.FormatErrorIsInvalid(nameof(request.Channel))));
}

(bool success, string? message, Version? version) = await Actions.Update(request.Channel, request.Forced).ConfigureAwait(false);
// Update process can result in kestrel shutdown request, just before patching the files
// In this case, we have very little opportunity to do anything, especially we will not have access to the return value of the action
// That's because update action will synchronously stop the kestrel, and wait for it before proceeding with an update, and that'll wait for us finishing the request, never happening
// Therefore, we'll allow this action to proceed while listening for application shutdown request, if it happens, we'll do our best by getting alternative signal that update is proceeding
bool success;
string? message = null;
Version? version;

try {
(success, message, version) = await Task.Run(() => Actions.Update(request.Channel, request.Forced), ApplicationLifetime.ApplicationStopping).ConfigureAwait(false);
} catch (TaskCanceledException e) {

Check warning on line 209 in ArchiSteamFarm/IPC/Controllers/Api/ASFController.cs

View workflow job for this annotation

GitHub Actions / main (Debug, macos-latest)

The variable 'e' is declared but never used

Check warning on line 209 in ArchiSteamFarm/IPC/Controllers/Api/ASFController.cs

View workflow job for this annotation

GitHub Actions / main (Debug, macos-latest)

The variable 'e' is declared but never used

Check warning on line 209 in ArchiSteamFarm/IPC/Controllers/Api/ASFController.cs

View workflow job for this annotation

GitHub Actions / main (Debug, ubuntu-latest)

The variable 'e' is declared but never used

Check warning on line 209 in ArchiSteamFarm/IPC/Controllers/Api/ASFController.cs

View workflow job for this annotation

GitHub Actions / main (Debug, ubuntu-latest)

The variable 'e' is declared but never used

Check warning on line 209 in ArchiSteamFarm/IPC/Controllers/Api/ASFController.cs

View workflow job for this annotation

GitHub Actions / main (Debug, windows-latest)

The variable 'e' is declared but never used

Check warning on line 209 in ArchiSteamFarm/IPC/Controllers/Api/ASFController.cs

View workflow job for this annotation

GitHub Actions / main (Debug, windows-latest)

The variable 'e' is declared but never used

Check failure on line 209 in ArchiSteamFarm/IPC/Controllers/Api/ASFController.cs

View workflow job for this annotation

GitHub Actions / main (Release, macos-latest)

The variable 'e' is declared but never used

Check failure on line 209 in ArchiSteamFarm/IPC/Controllers/Api/ASFController.cs

View workflow job for this annotation

GitHub Actions / main (Release, macos-latest)

The variable 'e' is declared but never used

Check failure on line 209 in ArchiSteamFarm/IPC/Controllers/Api/ASFController.cs

View workflow job for this annotation

GitHub Actions / main (Release, ubuntu-latest)

The variable 'e' is declared but never used

Check failure on line 209 in ArchiSteamFarm/IPC/Controllers/Api/ASFController.cs

View workflow job for this annotation

GitHub Actions / main (Release, ubuntu-latest)

The variable 'e' is declared but never used

Check failure on line 209 in ArchiSteamFarm/IPC/Controllers/Api/ASFController.cs

View workflow job for this annotation

GitHub Actions / main (Release, windows-latest)

The variable 'e' is declared but never used

Check failure on line 209 in ArchiSteamFarm/IPC/Controllers/Api/ASFController.cs

View workflow job for this annotation

GitHub Actions / main (Release, windows-latest)

The variable 'e' is declared but never used

Check failure on line 209 in ArchiSteamFarm/IPC/Controllers/Api/ASFController.cs

View workflow job for this annotation

GitHub Actions / publish-asf (ubuntu-latest, generic)

The variable 'e' is declared but never used

Check failure on line 209 in ArchiSteamFarm/IPC/Controllers/Api/ASFController.cs

View workflow job for this annotation

GitHub Actions / publish-asf (ubuntu-latest, linux-arm)

The variable 'e' is declared but never used

Check failure on line 209 in ArchiSteamFarm/IPC/Controllers/Api/ASFController.cs

View workflow job for this annotation

GitHub Actions / publish-asf (ubuntu-latest, linux-arm64)

The variable 'e' is declared but never used

Check failure on line 209 in ArchiSteamFarm/IPC/Controllers/Api/ASFController.cs

View workflow job for this annotation

GitHub Actions / publish-asf (ubuntu-latest, linux-x64)

The variable 'e' is declared but never used

Check failure on line 209 in ArchiSteamFarm/IPC/Controllers/Api/ASFController.cs

View workflow job for this annotation

GitHub Actions / publish-asf (macos-latest, osx-arm64)

The variable 'e' is declared but never used

Check failure on line 209 in ArchiSteamFarm/IPC/Controllers/Api/ASFController.cs

View workflow job for this annotation

GitHub Actions / publish-asf (macos-latest, osx-x64)

The variable 'e' is declared but never used

Check failure on line 209 in ArchiSteamFarm/IPC/Controllers/Api/ASFController.cs

View workflow job for this annotation

GitHub Actions / publish-asf (windows-latest, win-arm64)

The variable 'e' is declared but never used

Check failure on line 209 in ArchiSteamFarm/IPC/Controllers/Api/ASFController.cs

View workflow job for this annotation

GitHub Actions / publish-asf (windows-latest, win-x64)

The variable 'e' is declared but never used

Check warning on line 209 in ArchiSteamFarm/IPC/Controllers/Api/ASFController.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Unused local variable

Local variable 'e' is never used
// It's almost guaranteed that this is the result of update process requesting kestrel shutdown
// However, we're still going to check PendingVersionUpdate, which should be set by the update process as alternative way to inform us about pending update
version = PendingVersionUpdate;
success = version != null;
}

if (string.IsNullOrEmpty(message)) {
message = success ? Strings.Success : Strings.WarningFailed;
Expand Down

0 comments on commit f32fafd

Please sign in to comment.