mirror of
https://github.com/ld3z/new-manga.git
synced 2026-11-09 20:33:44 -07:00
replicating fix
This commit is contained in:
parent
8e6ea73460
commit
33db20e16b
1 changed files with 54 additions and 23 deletions
|
|
@ -284,49 +284,80 @@ 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
|
||||||
for (const [url, client] of replicaClients.entries()) {
|
if (!foundMapping) {
|
||||||
if (client.status === 'ready') {
|
for (const [url, client] of replicaClients.entries()) {
|
||||||
try {
|
if (client.status === 'ready') {
|
||||||
await client.ping();
|
try {
|
||||||
console.log(`Trying to get key from ${getRedisName(url)}`);
|
await client.ping();
|
||||||
const mapping = await client.get(feedId);
|
console.log(`Trying to get key from ${getRedisName(url)}`);
|
||||||
if (mapping) {
|
const mapping = await client.get(feedId);
|
||||||
await client.expire(feedId, DEFAULT_CACHE_TTL);
|
if (mapping) {
|
||||||
const data = JSON.parse(mapping);
|
await client.expire(feedId, DEFAULT_CACHE_TTL);
|
||||||
return {
|
foundMapping = mapping;
|
||||||
slugs: data.slugs,
|
sourceRedisName = getRedisName(url);
|
||||||
lang: data.lang
|
break; // Exit the loop once we find the mapping
|
||||||
};
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.warn(`${getRedisName(url)} connection failed while retrieving mapping:`, error);
|
||||||
|
// Continue to next replica
|
||||||
}
|
}
|
||||||
} catch (error) {
|
|
||||||
console.warn(`${getRedisName(url)} connection failed while retrieving mapping:`, error);
|
|
||||||
// Continue to next replica
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue