Fix background fetch to not overwrite cache with empty data
- Only trigger background refresh if cache is incomplete (less than max records) - Only update cache if background fetch returns records (don't overwrite on failure) - Prevents test failures when background fetch fails to connect to Sonarr/Radarr - Fixes integration test 'includes imported and failed records, excludes grabbed'
This commit is contained in:
@@ -40,8 +40,8 @@ async function fetchSonarrHistory(since) {
|
||||
const cacheKey = 'history:sonarr';
|
||||
const cached = cache.get(cacheKey);
|
||||
if (cached) {
|
||||
// Trigger background refresh if cache is stale or incomplete
|
||||
if (!backgroundFetchState.sonarr.inProgress) {
|
||||
// Only trigger background refresh if cache is incomplete (less than max records)
|
||||
if (!backgroundFetchState.sonarr.inProgress && cached.length < MAX_TOTAL_RECORDS) {
|
||||
triggerBackgroundSonarrFetch(since);
|
||||
}
|
||||
return cached;
|
||||
@@ -141,13 +141,15 @@ async function triggerBackgroundSonarrFetch(since) {
|
||||
|
||||
const allRecords = results.flat();
|
||||
|
||||
// Update cache with all records
|
||||
cache.set('history:sonarr', allRecords, HISTORY_CACHE_TTL);
|
||||
|
||||
// Emit SSE event for history update
|
||||
emitHistoryUpdate('sonarr');
|
||||
|
||||
console.log(`[HistoryFetcher] Background fetch complete: ${allRecords.length} Sonarr records`);
|
||||
// Only update cache if we got records (don't overwrite with empty data on failure)
|
||||
if (allRecords.length > 0) {
|
||||
cache.set('history:sonarr', allRecords, HISTORY_CACHE_TTL);
|
||||
|
||||
// Emit SSE event for history update
|
||||
emitHistoryUpdate('sonarr');
|
||||
|
||||
console.log(`[HistoryFetcher] Background fetch complete: ${allRecords.length} Sonarr records`);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('[HistoryFetcher] Background Sonarr fetch error:', err.message);
|
||||
} finally {
|
||||
@@ -166,8 +168,8 @@ async function fetchRadarrHistory(since) {
|
||||
const cacheKey = 'history:radarr';
|
||||
const cached = cache.get(cacheKey);
|
||||
if (cached) {
|
||||
// Trigger background refresh if cache is stale or incomplete
|
||||
if (!backgroundFetchState.radarr.inProgress) {
|
||||
// Only trigger background refresh if cache is incomplete (less than max records)
|
||||
if (!backgroundFetchState.radarr.inProgress && cached.length < MAX_TOTAL_RECORDS) {
|
||||
triggerBackgroundRadarrFetch(since);
|
||||
}
|
||||
return cached;
|
||||
@@ -265,13 +267,15 @@ async function triggerBackgroundRadarrFetch(since) {
|
||||
|
||||
const allRecords = results.flat();
|
||||
|
||||
// Update cache with all records
|
||||
cache.set('history:radarr', allRecords, HISTORY_CACHE_TTL);
|
||||
|
||||
// Emit SSE event for history update
|
||||
emitHistoryUpdate('radarr');
|
||||
|
||||
console.log(`[HistoryFetcher] Background fetch complete: ${allRecords.length} Radarr records`);
|
||||
// Only update cache if we got records (don't overwrite with empty data on failure)
|
||||
if (allRecords.length > 0) {
|
||||
cache.set('history:radarr', allRecords, HISTORY_CACHE_TTL);
|
||||
|
||||
// Emit SSE event for history update
|
||||
emitHistoryUpdate('radarr');
|
||||
|
||||
console.log(`[HistoryFetcher] Background fetch complete: ${allRecords.length} Radarr records`);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('[HistoryFetcher] Background Radarr fetch error:', err.message);
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user