From e8c0b32da56cdb7fae3f1dd176fc9219cb32c946 Mon Sep 17 00:00:00 2001 From: Alex Komoroske Date: Sun, 12 Nov 2023 11:04:16 -0700 Subject: [PATCH] Update fetchSimilarCard to automatically try again after a few seconds if the embedding is stale. This means that right after a card is updated, before the embedding has been updated, it will get the new similarity soon after it's available, no more than DELAY_FOR_STALE milliseconds after it's available. Part of #646. Part of #670. --- src/actions/similarity.ts | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/actions/similarity.ts b/src/actions/similarity.ts index 373cb15c..be95309f 100644 --- a/src/actions/similarity.ts +++ b/src/actions/similarity.ts @@ -90,6 +90,9 @@ const similarCards = async (cardID : CardID, lastUpdated : MillisecondsSinceEpoc return result.data; }; +const TIME_TO_WAIT_FOR_STALE : MillisecondsSinceEpoch = 10 * 60 * 1000; +const DELAY_FOR_STALE : MillisecondsSinceEpoch = 2.5 * 1000; + const fetchSimilarCards = (cardID : CardID, lastUpdated: MillisecondsSinceEpoch) : ThunkSomeAction => async (dispatch) => { if (!cardID) return; @@ -97,11 +100,18 @@ const fetchSimilarCards = (cardID : CardID, lastUpdated: MillisecondsSinceEpoch) if (result.success == false) { - //TODO: if it failed because of `stale-embedding`, then try again... as - //long as it's been under 10 minutes since the card was updated, at - //which point we just give up. - - console.warn(`similarCards failed: ${result.error}`); + if (result.code == 'stale-embedding') { + //This error happens when there might be a new one coming + const timeSinceUpdated = Date.now() - lastUpdated; + if (timeSinceUpdated < TIME_TO_WAIT_FOR_STALE) { + //Wait a bit and try again + console.log(`The card was stale, but it was last updated recently enough that we'll wait ${DELAY_FOR_STALE} ms and try again`); + setTimeout(() => dispatch(fetchSimilarCards(cardID, lastUpdated)), DELAY_FOR_STALE); + return; + } + } + + console.warn(`similarCards failed: ${result.code}: ${result.error}`); dispatch({ type: UPDATE_CARD_SIMILARITY, card_id: cardID,