Merge pull request 'Add a 30s timeout to the Gitea version check.' (#84) from version_timeout into master
All checks were successful
Publish Docker image / Build and push Docker image to Gitea Registry (push) Successful in 4m50s
Publish release zip / build (push) Successful in 56s

Reviewed-on: #84
This commit is contained in:
sim0n00ps 2025-12-01 22:47:50 +00:00
commit f75fa4e04c
2 changed files with 52 additions and 33 deletions

View File

@ -6,14 +6,15 @@ namespace OF_DL.Helpers;
public static class VersionHelper public static class VersionHelper
{ {
public static string? GetLatestReleaseTag() private static readonly HttpClient httpClient = new HttpClient();
private const string url = "https://git.ofdl.tools/api/v1/repos/sim0n00ps/OF-DL/releases/latest";
public static async Task<string?> GetLatestReleaseTag(CancellationToken cancellationToken = default)
{ {
Log.Debug("Calling GetLatestReleaseTag"); Log.Debug("Calling GetLatestReleaseTag");
try try
{ {
HttpClient client = new(); var response = await httpClient.GetAsync(url, cancellationToken);
HttpRequestMessage request = new(HttpMethod.Get, "https://git.ofdl.tools/api/v1/repos/sim0n00ps/OF-DL/releases/latest");
using var response = client.Send(request);
if (!response.IsSuccessStatusCode) if (!response.IsSuccessStatusCode)
{ {
@ -21,7 +22,7 @@ public static class VersionHelper
return null; return null;
} }
var body = response.Content.ReadAsStringAsync().Result; var body = await response.Content.ReadAsStringAsync();
Log.Debug("GetLatestReleaseTag API Response: "); Log.Debug("GetLatestReleaseTag API Response: ");
Log.Debug(body); Log.Debug(body);
@ -36,6 +37,10 @@ public static class VersionHelper
return versionCheckResponse.TagName; return versionCheckResponse.TagName;
} }
catch (OperationCanceledException)
{
throw; // Rethrow timeout exceptions to be handled by the caller
}
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine("Exception caught: {0}\n\nStackTrace: {1}", ex.Message, ex.StackTrace); Console.WriteLine("Exception caught: {0}\n\nStackTrace: {1}", ex.Message, ex.StackTrace);

View File

@ -526,7 +526,21 @@ public class Program
// Only run the version check if not in DEBUG mode // Only run the version check if not in DEBUG mode
#if !DEBUG #if !DEBUG
Version localVersion = Assembly.GetEntryAssembly()?.GetName().Version; //Only tested with numeric values. Version localVersion = Assembly.GetEntryAssembly()?.GetName().Version; //Only tested with numeric values.
String? latestReleaseTag = VersionHelper.GetLatestReleaseTag();
// Create a cancellation token with 30 second timeout
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));
String? latestReleaseTag = null;
try
{
latestReleaseTag = await VersionHelper.GetLatestReleaseTag(cts.Token);
}
catch (OperationCanceledException)
{
AnsiConsole.Markup("[yellow]Version check timed out after 30 seconds.\n[/]");
Log.Warning("Version check timed out after 30 seconds");
latestReleaseTag = null;
}
if (latestReleaseTag == null) if (latestReleaseTag == null)
{ {
@ -543,7 +557,7 @@ public class Program
{ {
// The version on GitHub is more up to date than this local release. // The version on GitHub is more up to date than this local release.
AnsiConsole.Markup("[red]You are running OF-DL version " + $"{localVersion.Major}.{localVersion.Minor}.{localVersion.Build}\n[/]"); AnsiConsole.Markup("[red]You are running OF-DL version " + $"{localVersion.Major}.{localVersion.Minor}.{localVersion.Build}\n[/]");
AnsiConsole.Markup("[red]Please update to the current release, " + $"{latestGiteaRelease.Major}.{latestGiteaRelease.Minor}.{latestGiteaRelease.Build}: [link]https://git.ofdl.tools/sim0n00ps/OF-DL/releases[/]\n[/]"); AnsiConsole.Markup("[red]Please update to the current release, " + $"{latestGiteaRelease.Major}.{latestGiteaRelease.Minor}.{latestGiteaRelease.Build}: [link=https://git.ofdl.tools/sim0n00ps/OF-DL/releases]https://git.ofdl.tools/sim0n00ps/OF-DL/releases[/]\n[/]");
Log.Debug("Detected outdated client running version " + $"{localVersion.Major}.{localVersion.Minor}.{localVersion.Build}"); Log.Debug("Detected outdated client running version " + $"{localVersion.Major}.{localVersion.Minor}.{localVersion.Build}");
Log.Debug("Latest release version " + $"{latestGiteaRelease.Major}.{latestGiteaRelease.Minor}.{latestGiteaRelease.Build}"); Log.Debug("Latest release version " + $"{latestGiteaRelease.Major}.{latestGiteaRelease.Minor}.{latestGiteaRelease.Build}");
} }