From 5fb60b069f9ddf8424eb373e9a0de9f40ba4aa97 Mon Sep 17 00:00:00 2001 From: JamesFlare1212 Date: Wed, 8 Apr 2026 12:07:51 -0400 Subject: [PATCH] fix(cache): preserve local data on remote 5xx errors Only update cache when remote returns HTTP 200. On 5xx errors or timeouts, preserve existing local cache instead of overwriting with empty/error data. --- engage-api/get-activity.ts | 17 ++++++++++++++- services/cache-manager.ts | 42 +++++++++++++++++++++++++++----------- 2 files changed, 46 insertions(+), 13 deletions(-) diff --git a/engage-api/get-activity.ts b/engage-api/get-activity.ts index ce0f3d2..1a6b982 100644 --- a/engage-api/get-activity.ts +++ b/engage-api/get-activity.ts @@ -45,6 +45,7 @@ async function getCompleteCookies(userName: string, userPwd: string): Promise= 500 && error.response.status < 600) { + logger.error(`Server error ${error.response.status} - preserving local cache, not updating.`); + } } if (attempt === maxRetries - 1) { logger.error(`All ${maxRetries} retries failed for activity ${activityId}.`); diff --git a/services/cache-manager.ts b/services/cache-manager.ts index f6e85ca..feb3f89 100644 --- a/services/cache-manager.ts +++ b/services/cache-manager.ts @@ -72,9 +72,10 @@ let skippedCount = 0; /** * Process and cache a single activity * @param activityId - The activity ID to process + * @param forceUpdate - If true, update cache even on fetch failure (default: false) * @returns The processed activity data */ -async function processAndCacheActivity(activityId: string): Promise { +async function processAndCacheActivity(activityId: string, forceUpdate: boolean = false): Promise { logger.debug(`Processing activity ID: ${activityId}`); try { if (!USERNAME || !PASSWORD) { @@ -92,11 +93,21 @@ async function processAndCacheActivity(activityId: string): Promise