When upgrading Kavita, clear out existing Github cache.

This commit is contained in:
Joseph Milazzo 2025-05-10 11:29:51 -05:00
parent fd1a58f438
commit a2f6dfa7bf
2 changed files with 31 additions and 4 deletions

View file

@ -52,6 +52,7 @@ public interface IVersionUpdaterService
Task PushUpdate(UpdateNotificationDto update); Task PushUpdate(UpdateNotificationDto update);
Task<IList<UpdateNotificationDto>> GetAllReleases(int count = 0); Task<IList<UpdateNotificationDto>> GetAllReleases(int count = 0);
Task<int> GetNumberOfReleasesBehind(bool stableOnly = false); Task<int> GetNumberOfReleasesBehind(bool stableOnly = false);
void BustGithubCache();
} }
@ -384,7 +385,7 @@ public partial class VersionUpdaterService : IVersionUpdaterService
if (DateTime.UtcNow - fileInfo.LastWriteTimeUtc <= CacheDuration) if (DateTime.UtcNow - fileInfo.LastWriteTimeUtc <= CacheDuration)
{ {
var cachedData = await File.ReadAllTextAsync(_cacheLatestReleaseFilePath); var cachedData = await File.ReadAllTextAsync(_cacheLatestReleaseFilePath);
return System.Text.Json.JsonSerializer.Deserialize<UpdateNotificationDto>(cachedData); return JsonSerializer.Deserialize<UpdateNotificationDto>(cachedData);
} }
return null; return null;
@ -407,7 +408,7 @@ public partial class VersionUpdaterService : IVersionUpdaterService
{ {
try try
{ {
var json = System.Text.Json.JsonSerializer.Serialize(update, JsonOptions); var json = JsonSerializer.Serialize(update, JsonOptions);
await File.WriteAllTextAsync(_cacheLatestReleaseFilePath, json); await File.WriteAllTextAsync(_cacheLatestReleaseFilePath, json);
} }
catch (Exception ex) catch (Exception ex)
@ -446,6 +447,21 @@ public partial class VersionUpdaterService : IVersionUpdaterService
.Count(u => u.IsReleaseNewer); .Count(u => u.IsReleaseNewer);
} }
/// <summary>
/// Clears the Github cache
/// </summary>
public void BustGithubCache()
{
try
{
File.Delete(_cacheFilePath);
File.Delete(_cacheLatestReleaseFilePath);
} catch (Exception ex)
{
_logger.LogError(ex, "Failed to clear Github cache");
}
}
private UpdateNotificationDto? CreateDto(GithubReleaseMetadata? update) private UpdateNotificationDto? CreateDto(GithubReleaseMetadata? update)
{ {
if (update == null || string.IsNullOrEmpty(update.Tag_Name)) return null; if (update == null || string.IsNullOrEmpty(update.Tag_Name)) return null;

View file

@ -223,7 +223,7 @@ public class Startup
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IBackgroundJobClient backgroundJobs, IWebHostEnvironment env, public void Configure(IApplicationBuilder app, IBackgroundJobClient backgroundJobs, IWebHostEnvironment env,
IHostApplicationLifetime applicationLifetime, IServiceProvider serviceProvider, ICacheService cacheService, IHostApplicationLifetime applicationLifetime, IServiceProvider serviceProvider, ICacheService cacheService,
IDirectoryService directoryService, IUnitOfWork unitOfWork, IBackupService backupService, IImageService imageService) IDirectoryService directoryService, IUnitOfWork unitOfWork, IBackupService backupService, IImageService imageService, IVersionUpdaterService versionService)
{ {
var logger = serviceProvider.GetRequiredService<ILogger<Program>>(); var logger = serviceProvider.GetRequiredService<ILogger<Program>>();
@ -235,9 +235,10 @@ public class Startup
// Apply all migrations on startup // Apply all migrations on startup
var dataContext = serviceProvider.GetRequiredService<DataContext>(); var dataContext = serviceProvider.GetRequiredService<DataContext>();
logger.LogInformation("Running Migrations"); logger.LogInformation("Running Migrations");
#region Migrations
// v0.7.9 // v0.7.9
await MigrateUserLibrarySideNavStream.Migrate(unitOfWork, dataContext, logger); await MigrateUserLibrarySideNavStream.Migrate(unitOfWork, dataContext, logger);
@ -289,13 +290,23 @@ public class Startup
await ManualMigrateScrobbleSpecials.Migrate(dataContext, logger); await ManualMigrateScrobbleSpecials.Migrate(dataContext, logger);
await ManualMigrateScrobbleEventGen.Migrate(dataContext, logger); await ManualMigrateScrobbleEventGen.Migrate(dataContext, logger);
#endregion
// Update the version in the DB after all migrations are run // Update the version in the DB after all migrations are run
var installVersion = await unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.InstallVersion); var installVersion = await unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.InstallVersion);
var isVersionDifferent = installVersion.Value != BuildInfo.Version.ToString();
installVersion.Value = BuildInfo.Version.ToString(); installVersion.Value = BuildInfo.Version.ToString();
unitOfWork.SettingsRepository.Update(installVersion); unitOfWork.SettingsRepository.Update(installVersion);
await unitOfWork.CommitAsync(); await unitOfWork.CommitAsync();
logger.LogInformation("Running Migrations - complete"); logger.LogInformation("Running Migrations - complete");
if (isVersionDifferent)
{
// Clear the Github cache so update stuff shows correctly
versionService.BustGithubCache();
}
}).GetAwaiter() }).GetAwaiter()
.GetResult(); .GetResult();
} }