mirror of
https://github.com/alexhopeoconnor/bom-local-service.git
synced 2026-10-04 03:28:11 +10:00
Fix: Improve cache stability and configuration consistency
Active Folder Exclusion Fixes: - Exclude active cache folders in GetCachedFrameAsync, GetCachedFramesAsync, and GetAllCacheFoldersAsync - Prevents TaskCanceledException when reading metadata from folders being written to - Matches pattern used in GetCachedRadarAsync for consistency Cache Configuration Fixes: - Standardize default CacheExpirationMinutes to 12.5 (matches appsettings.json) - Add cache configuration to ScrapingService (removes hardcoded values) - Fix check interval calculation bug for intervals that don't divide 60 evenly - Fix NextUpdateTime calculation to account for background check intervals - Add configuration validation to all service constructors All fixes verified - service running stably with 0 exceptions in recent logs
This commit is contained in:
@@ -19,7 +19,12 @@ public class CacheService : ICacheService
|
||||
_logger = logger;
|
||||
_configuration = configuration;
|
||||
_cacheDirectory = FilePathHelper.GetCacheDirectory(configuration);
|
||||
_cacheExpirationMinutes = configuration.GetValue<double>("CacheExpirationMinutes", 15.5);
|
||||
_cacheExpirationMinutes = configuration.GetValue<double>("CacheExpirationMinutes", 12.5);
|
||||
|
||||
if (_cacheExpirationMinutes <= 0)
|
||||
{
|
||||
throw new ArgumentException("CacheExpirationMinutes must be greater than 0", nameof(configuration));
|
||||
}
|
||||
|
||||
// Ensure cache directory exists
|
||||
Directory.CreateDirectory(_cacheDirectory);
|
||||
@@ -90,7 +95,10 @@ public class CacheService : ICacheService
|
||||
string state,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var (cacheFolderPath, _) = await GetCachedScreenshotWithMetadataAsync(suburb, state, CachedDataType.Radar, null, cancellationToken);
|
||||
// Exclude active cache folder (currently being written to) to avoid reading incomplete data
|
||||
var locationKey = LocationHelper.GetLocationKey(suburb, state);
|
||||
var excludeFolder = GetActiveCacheFolder(locationKey);
|
||||
var (cacheFolderPath, _) = await GetCachedScreenshotWithMetadataAsync(suburb, state, CachedDataType.Radar, excludeFolder, cancellationToken);
|
||||
|
||||
if (string.IsNullOrEmpty(cacheFolderPath) || !Directory.Exists(cacheFolderPath))
|
||||
{
|
||||
@@ -322,6 +330,10 @@ public class CacheService : ICacheService
|
||||
string state,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
// Exclude active cache folder (currently being written to) to avoid reading incomplete data
|
||||
var locationKey = LocationHelper.GetLocationKey(suburb, state);
|
||||
var excludeFolder = GetActiveCacheFolder(locationKey);
|
||||
|
||||
var pattern = FilePathHelper.GetCacheFolderPattern(suburb, state);
|
||||
var folders = Directory.GetDirectories(_cacheDirectory, pattern)
|
||||
.Select(f =>
|
||||
@@ -342,6 +354,13 @@ public class CacheService : ICacheService
|
||||
if (!Directory.Exists(folder.Folder))
|
||||
continue;
|
||||
|
||||
// Skip the folder if it's being excluded (currently being written to)
|
||||
if (!string.IsNullOrEmpty(excludeFolder) && Path.GetFullPath(folder.Folder).Equals(Path.GetFullPath(excludeFolder), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
_logger.LogDebug("Skipping excluded cache folder (being written to): {Folder}", folder.Folder);
|
||||
continue;
|
||||
}
|
||||
|
||||
var metadata = await LoadMetadataAsync(folder.Folder, cancellationToken);
|
||||
if (metadata == null)
|
||||
continue;
|
||||
@@ -621,9 +640,7 @@ public class CacheService : ICacheService
|
||||
{
|
||||
// Cache is invalid/missing - next update would be after background service check
|
||||
status.Message = status.CacheExists ? "Cache is stale" : "No cache exists";
|
||||
var now = DateTime.UtcNow;
|
||||
var minutesUntilNextCheck = cacheManagementCheckIntervalMinutes - (now.Minute % cacheManagementCheckIntervalMinutes);
|
||||
status.NextUpdateTime = now.AddMinutes(minutesUntilNextCheck);
|
||||
status.NextUpdateTime = ResponseBuilder.CalculateNextServiceCheck(cacheManagementCheckIntervalMinutes);
|
||||
}
|
||||
|
||||
return status;
|
||||
|
||||
Reference in New Issue
Block a user