replicating fix

This commit is contained in:
ld3zian 2025-03-03 16:12:25 -07:00
parent 8e6ea73460
commit 33db20e16b

View file

@ -284,28 +284,29 @@ export async function checkCacheStatus(slugs: string[], lang: string): Promise<s
} }
export async function getFeedMapping(feedId: string): Promise<FeedMapping | null> { export async function getFeedMapping(feedId: string): Promise<FeedMapping | null> {
let foundMapping: string | null = null;
let sourceRedisName: string = "";
// First try primary // First try primary
try { try {
// Check primary database if available
if (primaryClient?.status === 'ready') { if (primaryClient?.status === 'ready') {
try { try {
await primaryClient.ping(); await primaryClient.ping();
const mapping = await primaryClient.get(feedId); const mapping = await primaryClient.get(feedId);
if (mapping) { if (mapping) {
await primaryClient.expire(feedId, DEFAULT_CACHE_TTL); await primaryClient.expire(feedId, DEFAULT_CACHE_TTL);
const data = JSON.parse(mapping); foundMapping = mapping;
return { sourceRedisName = "Primary";
slugs: data.slugs,
lang: data.lang
};
} }
// If key not found in primary, we'll fall through to try replicas
} catch (error) { } catch (error) {
console.warn('Primary Redis connection failed while retrieving mapping:', error); console.warn('Primary Redis connection failed while retrieving mapping:', error);
// Fall through to try replicas // Fall through to try replicas
} }
} }
// Try replicas one by one // If not found in primary, check replicas
if (!foundMapping) {
for (const [url, client] of replicaClients.entries()) { for (const [url, client] of replicaClients.entries()) {
if (client.status === 'ready') { if (client.status === 'ready') {
try { try {
@ -314,11 +315,9 @@ export async function getFeedMapping(feedId: string): Promise<FeedMapping | null
const mapping = await client.get(feedId); const mapping = await client.get(feedId);
if (mapping) { if (mapping) {
await client.expire(feedId, DEFAULT_CACHE_TTL); await client.expire(feedId, DEFAULT_CACHE_TTL);
const data = JSON.parse(mapping); foundMapping = mapping;
return { sourceRedisName = getRedisName(url);
slugs: data.slugs, break; // Exit the loop once we find the mapping
lang: data.lang
};
} }
} catch (error) { } catch (error) {
console.warn(`${getRedisName(url)} connection failed while retrieving mapping:`, error); console.warn(`${getRedisName(url)} connection failed while retrieving mapping:`, error);
@ -326,6 +325,38 @@ export async function getFeedMapping(feedId: string): Promise<FeedMapping | null
} }
} }
} }
}
// If we found the mapping in any database, re-synchronize to all other databases
if (foundMapping) {
console.log(`Found mapping in ${sourceRedisName}. Re-synchronizing to all Redis instances...`);
// Parse data for returning
const data = JSON.parse(foundMapping);
const result = {
slugs: data.slugs,
lang: data.lang
};
// Fire-and-forget re-sync to all databases
(async () => {
try {
await writeToAllRedis(async (redis) => {
await redis.set(
feedId,
foundMapping!,
'EX',
DEFAULT_CACHE_TTL
);
});
console.log(`Successfully re-synchronized mapping for ${feedId} to all Redis instances`);
} catch (syncError) {
console.error('Error during Redis re-synchronization:', syncError);
}
})();
return result;
}
// If we get here, the key wasn't found in any database // If we get here, the key wasn't found in any database
return null; return null;