v0.8.5.2 - Hotfix (#3608)

This commit is contained in:
Joe Milazzo 2025-03-09 11:21:43 -05:00 committed by GitHub
parent 56e84fc235
commit 401fa3e611
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 524 additions and 23 deletions

View file

@ -40,8 +40,7 @@ public class UpdateNotificationDto
/// <summary>
/// Date of the publish
/// </summary>
public required string PublishDate { get; set
; }
public required string PublishDate { get; set; }
/// <summary>
/// Is the server on a nightly within this release
/// </summary>

View file

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using API.DTOs.Update;
@ -68,13 +69,19 @@ public partial class VersionUpdaterService : IVersionUpdaterService
[GeneratedRegex(@"^\n*(.*?)\n+#{1,2}\s", RegexOptions.Singleline)]
private static partial Regex BlogPartRegex();
private readonly string _cacheFilePath;
/// <summary>
/// The latest release cache
/// </summary>
private readonly string _cacheLatestReleaseFilePath;
private static readonly TimeSpan CacheDuration = TimeSpan.FromHours(1);
private static readonly JsonSerializerOptions JsonOptions = new() { WriteIndented = true };
public VersionUpdaterService(ILogger<VersionUpdaterService> logger, IEventHub eventHub, IDirectoryService directoryService)
{
_logger = logger;
_eventHub = eventHub;
_cacheFilePath = Path.Combine(directoryService.LongTermCacheDirectory, "github_releases_cache.json");
_cacheLatestReleaseFilePath = Path.Combine(directoryService.LongTermCacheDirectory, "github_latest_release_cache.json");
FlurlConfiguration.ConfigureClientForUrl(GithubLatestReleasesUrl);
FlurlConfiguration.ConfigureClientForUrl(GithubAllReleasesUrl);
@ -86,9 +93,21 @@ public partial class VersionUpdaterService : IVersionUpdaterService
/// <returns>Latest update</returns>
public async Task<UpdateNotificationDto?> CheckForUpdate()
{
// Attempt to fetch from cache
var cachedRelease = await TryGetCachedLatestRelease();
if (cachedRelease != null)
{
return cachedRelease;
}
var update = await GetGithubRelease();
var dto = CreateDto(update);
if (dto != null)
{
await CacheLatestReleaseAsync(dto);
}
return dto;
}
@ -273,6 +292,13 @@ public partial class VersionUpdaterService : IVersionUpdaterService
var updateDtos = query.ToList();
// Sometimes a release can be 0.8.5.0 on disk, but 0.8.5 from Github
var versionParts = updateDtos[0].UpdateVersion.Split('.');
if (versionParts.Length < 4)
{
updateDtos[0].UpdateVersion += ".0"; // Append missing parts
}
// If we're on a nightly build, enrich the information
if (updateDtos.Count != 0 && BuildInfo.Version > new Version(updateDtos[0].UpdateVersion))
{
@ -321,11 +347,25 @@ public partial class VersionUpdaterService : IVersionUpdaterService
return null;
}
private async Task<UpdateNotificationDto?> TryGetCachedLatestRelease()
{
if (!File.Exists(_cacheLatestReleaseFilePath)) return null;
var fileInfo = new FileInfo(_cacheLatestReleaseFilePath);
if (DateTime.UtcNow - fileInfo.LastWriteTimeUtc <= CacheDuration)
{
var cachedData = await File.ReadAllTextAsync(_cacheLatestReleaseFilePath);
return System.Text.Json.JsonSerializer.Deserialize<UpdateNotificationDto>(cachedData);
}
return null;
}
private async Task CacheReleasesAsync(IList<UpdateNotificationDto> updates)
{
try
{
var json = System.Text.Json.JsonSerializer.Serialize(updates, new System.Text.Json.JsonSerializerOptions { WriteIndented = true });
var json = System.Text.Json.JsonSerializer.Serialize(updates, JsonOptions);
await File.WriteAllTextAsync(_cacheFilePath, json);
}
catch (Exception ex)
@ -334,6 +374,19 @@ public partial class VersionUpdaterService : IVersionUpdaterService
}
}
private async Task CacheLatestReleaseAsync(UpdateNotificationDto update)
{
try
{
var json = System.Text.Json.JsonSerializer.Serialize(update, JsonOptions);
await File.WriteAllTextAsync(_cacheLatestReleaseFilePath, json);
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to cache latest release");
}
}
private static bool IsVersionEqualToBuildVersion(Version updateVersion)
@ -346,7 +399,10 @@ public partial class VersionUpdaterService : IVersionUpdaterService
public async Task<int> GetNumberOfReleasesBehind()
{
var updates = await GetAllReleases();
return updates.TakeWhile(update => update.UpdateVersion != update.CurrentVersion).Count();
return updates
.Where(update => !update.IsPrerelease)
.TakeWhile(update => update.UpdateVersion != update.CurrentVersion)
.Count();
}
private UpdateNotificationDto? CreateDto(GithubReleaseMetadata? update)
@ -370,6 +426,7 @@ public partial class VersionUpdaterService : IVersionUpdaterService
PublishDate = update.Published_At,
IsReleaseEqual = IsVersionEqualToBuildVersion(updateVersion),
IsReleaseNewer = BuildInfo.Version < updateVersion,
IsPrerelease = false,
Added = parsedSections.TryGetValue("Added", out var added) ? added : [],
Removed = parsedSections.TryGetValue("Removed", out var removed) ? removed : [],