Merge pull request 'Add a 30s timeout to the Gitea version check.' (#84) from version_timeout into master
Reviewed-on: #84
This commit is contained in:
commit
f75fa4e04c
@ -6,14 +6,15 @@ namespace OF_DL.Helpers;
|
||||
|
||||
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");
|
||||
try
|
||||
{
|
||||
HttpClient client = new();
|
||||
HttpRequestMessage request = new(HttpMethod.Get, "https://git.ofdl.tools/api/v1/repos/sim0n00ps/OF-DL/releases/latest");
|
||||
using var response = client.Send(request);
|
||||
var response = await httpClient.GetAsync(url, cancellationToken);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
@ -21,7 +22,7 @@ public static class VersionHelper
|
||||
return null;
|
||||
}
|
||||
|
||||
var body = response.Content.ReadAsStringAsync().Result;
|
||||
var body = await response.Content.ReadAsStringAsync();
|
||||
|
||||
Log.Debug("GetLatestReleaseTag API Response: ");
|
||||
Log.Debug(body);
|
||||
@ -36,6 +37,10 @@ public static class VersionHelper
|
||||
|
||||
return versionCheckResponse.TagName;
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
throw; // Rethrow timeout exceptions to be handled by the caller
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Exception caught: {0}\n\nStackTrace: {1}", ex.Message, ex.StackTrace);
|
||||
|
||||
@ -526,36 +526,50 @@ public class Program
|
||||
// Only run the version check if not in DEBUG mode
|
||||
#if !DEBUG
|
||||
Version localVersion = Assembly.GetEntryAssembly()?.GetName().Version; //Only tested with numeric values.
|
||||
String? latestReleaseTag = VersionHelper.GetLatestReleaseTag();
|
||||
|
||||
if (latestReleaseTag == null)
|
||||
{
|
||||
AnsiConsole.Markup("[yellow]Failed to verify that OF-DL is up-to-date.\n[/]");
|
||||
Log.Error("Failed to get the latest release tag.");
|
||||
}
|
||||
else
|
||||
{
|
||||
Version latestGiteaRelease = new Version(latestReleaseTag.Replace("OFDLV", ""));
|
||||
// Create a cancellation token with 30 second timeout
|
||||
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));
|
||||
String? latestReleaseTag = null;
|
||||
|
||||
// Compare the Versions
|
||||
int versionComparison = localVersion.CompareTo(latestGiteaRelease);
|
||||
if (versionComparison < 0)
|
||||
{
|
||||
// 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]Please update to the current release, " + $"{latestGiteaRelease.Major}.{latestGiteaRelease.Minor}.{latestGiteaRelease.Build}: [link]https://git.ofdl.tools/sim0n00ps/OF-DL/releases[/]\n[/]");
|
||||
Log.Debug("Detected outdated client running version " + $"{localVersion.Major}.{localVersion.Minor}.{localVersion.Build}");
|
||||
Log.Debug("Latest release version " + $"{latestGiteaRelease.Major}.{latestGiteaRelease.Minor}.{latestGiteaRelease.Build}");
|
||||
}
|
||||
else
|
||||
{
|
||||
// This local version is greater than the release version on GitHub.
|
||||
AnsiConsole.Markup("[green]You are running OF-DL version " + $"{localVersion.Major}.{localVersion.Minor}.{localVersion.Build}\n[/]");
|
||||
AnsiConsole.Markup("[green]Latest Release version: " + $"{latestGiteaRelease.Major}.{latestGiteaRelease.Minor}.{latestGiteaRelease.Build}\n[/]");
|
||||
Log.Debug("Detected client running version " + $"{localVersion.Major}.{localVersion.Minor}.{localVersion.Build}");
|
||||
Log.Debug("Latest release version " + $"{latestGiteaRelease.Major}.{latestGiteaRelease.Minor}.{latestGiteaRelease.Build}");
|
||||
}
|
||||
}
|
||||
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)
|
||||
{
|
||||
AnsiConsole.Markup("[yellow]Failed to verify that OF-DL is up-to-date.\n[/]");
|
||||
Log.Error("Failed to get the latest release tag.");
|
||||
}
|
||||
else
|
||||
{
|
||||
Version latestGiteaRelease = new Version(latestReleaseTag.Replace("OFDLV", ""));
|
||||
|
||||
// Compare the Versions
|
||||
int versionComparison = localVersion.CompareTo(latestGiteaRelease);
|
||||
if (versionComparison < 0)
|
||||
{
|
||||
// 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]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("Latest release version " + $"{latestGiteaRelease.Major}.{latestGiteaRelease.Minor}.{latestGiteaRelease.Build}");
|
||||
}
|
||||
else
|
||||
{
|
||||
// This local version is greater than the release version on GitHub.
|
||||
AnsiConsole.Markup("[green]You are running OF-DL version " + $"{localVersion.Major}.{localVersion.Minor}.{localVersion.Build}\n[/]");
|
||||
AnsiConsole.Markup("[green]Latest Release version: " + $"{latestGiteaRelease.Major}.{latestGiteaRelease.Minor}.{latestGiteaRelease.Build}\n[/]");
|
||||
Log.Debug("Detected client running version " + $"{localVersion.Major}.{localVersion.Minor}.{localVersion.Build}");
|
||||
Log.Debug("Latest release version " + $"{latestGiteaRelease.Major}.{latestGiteaRelease.Minor}.{latestGiteaRelease.Build}");
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
AnsiConsole.Markup("[yellow]Running in Debug/Local mode. Version check skipped.\n[/]");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user