edit db.ts for deployment

This commit is contained in:
ld3zian 2025-01-25 21:01:11 -07:00
parent f3c566b3ad
commit 854f527cd9

View file

@ -1,34 +1,104 @@
import Redis from 'ioredis'; import Redis from 'ioredis';
import type { FeedMapping } from './types'; import type { FeedMapping } from './types';
// Initialize Redis client const REDIS_URL = import.meta.env.REDIS_URL || process.env.REDIS_URL;
const redis = new Redis(process.env.REDIS_URL!); let redis: Redis | null = null;
try {
console.log('Environment check:', {
hasEnvVar: !!REDIS_URL,
envVarStart: REDIS_URL?.substring(0, 20) + '...',
nodeEnv: process.env.NODE_ENV
});
if (!REDIS_URL) {
throw new Error('REDIS_URL environment variable is not set');
}
console.log('Attempting to connect to Redis...');
redis = new Redis(REDIS_URL, {
retryStrategy(times) {
const delay = Math.min(times * 50, 2000);
console.log(`Retry attempt ${times} with delay ${delay}ms`);
return delay;
},
maxRetriesPerRequest: 3,
enableReadyCheck: true,
reconnectOnError(err) {
console.log('Reconnect on error:', err.message);
return true;
}
});
redis.on('error', (error) => {
console.error('Redis connection error:', error.message);
});
redis.on('connect', () => {
console.log('Successfully connected to Redis');
});
redis.on('ready', () => {
console.log('Redis client is ready');
});
redis.on('reconnecting', () => {
console.log('Redis client is reconnecting');
});
} catch (error) {
console.error('Failed to initialize Redis:', error);
if (error instanceof Error) {
console.error('Error details:', error.message);
console.error('Stack trace:', error.stack);
}
}
export async function storeFeedMapping(feedId: string, slugs: string[], lang: string): Promise<void> { export async function storeFeedMapping(feedId: string, slugs: string[], lang: string): Promise<void> {
if (!redis) {
console.error('Redis client not initialized');
throw new Error('Storage system unavailable');
}
const mapping = { const mapping = {
slugs, slugs,
lang, lang,
created_at: new Date().toISOString() created_at: new Date().toISOString()
}; };
// Store with 30 day expiration try {
await redis.set( await redis.set(
feedId, feedId,
JSON.stringify(mapping), JSON.stringify(mapping),
'EX', 'EX',
60 * 60 * 24 * 30 // 30 days in seconds 60 * 60 * 24 * 30 // 30 days in seconds
); );
} catch (error) {
console.error('Error storing feed mapping:', error);
throw new Error('Failed to store feed mapping');
}
} }
export async function getFeedMapping(feedId: string): Promise<FeedMapping | null> { export async function getFeedMapping(feedId: string): Promise<FeedMapping | null> {
const mapping = await redis.get(feedId); if (!redis) {
if (!mapping) return null; console.error('Redis client not initialized');
throw new Error('Storage system unavailable');
}
const data = JSON.parse(mapping); try {
return { const mapping = await redis.get(feedId);
slugs: data.slugs, if (!mapping) return null;
lang: data.lang
}; const data = JSON.parse(mapping);
return {
slugs: data.slugs,
lang: data.lang
};
} catch (error) {
console.error('Error retrieving feed mapping:', error);
throw new Error('Failed to retrieve feed mapping');
}
} }
// No need for manual cleanup as we're using TTL (expire) // No need for manual cleanup as we're using TTL (expire)