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