Skip to content

Commit

Permalink
Update fetchSimilarCard to automatically try again after a few second…
Browse files Browse the repository at this point in the history
…s 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.
  • Loading branch information
jkomoros committed Nov 12, 2023
1 parent deda69d commit e8c0b32
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/actions/similarity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,28 @@ 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;

const result = await similarCards(cardID, lastUpdated);

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,
Expand Down

0 comments on commit e8c0b32

Please sign in to comment.