feat: enhance file upload process with temporary file handling, improved error management, and metrics logging

This commit is contained in:
MythEclipse
2026-05-22 00:29:54 +07:00
parent fd5eb98586
commit 56b929ffb1
7 changed files with 176 additions and 75 deletions
+9 -6
View File
@@ -71,11 +71,14 @@ export const fileInfoCache = new Cache<{
}>(3600);
// Cleanup expired cache entries every 5 minutes
setInterval(() => {
const removed = fileInfoCache.cleanup();
if (removed > 0) {
console.log(`Cleaned up ${removed} expired cache entries`);
}
}, 5 * 60 * 1000);
setInterval(
() => {
const removed = fileInfoCache.cleanup();
if (removed > 0) {
console.log(`Cleaned up ${removed} expired cache entries`);
}
},
5 * 60 * 1000,
);
export { Cache };
+12 -9
View File
@@ -109,14 +109,17 @@ class MetricsCollector {
export const metricsCollector = new MetricsCollector();
// Log metrics every 5 minutes
setInterval(() => {
const snapshot = metricsCollector.getSnapshot();
logger.info('Metrics snapshot', {
uploadLatency: snapshot.uploadLatency,
uploadThroughput: snapshot.uploadThroughput.toFixed(2),
errorRate: snapshot.errorRate.toFixed(2),
cacheHitRate: snapshot.cacheHitRate.toFixed(2),
});
}, 5 * 60 * 1000);
setInterval(
() => {
const snapshot = metricsCollector.getSnapshot();
logger.info('Metrics snapshot', {
uploadLatency: snapshot.uploadLatency,
uploadThroughput: snapshot.uploadThroughput.toFixed(2),
errorRate: snapshot.errorRate.toFixed(2),
cacheHitRate: snapshot.cacheHitRate.toFixed(2),
});
},
5 * 60 * 1000,
);
export { MetricsCollector };
+17 -25
View File
@@ -7,12 +7,12 @@ const botTokens = Array.from(new Set([config.botToken, ...config.additionalBotTo
const bots = botTokens.map((token) => new Telegraf(token));
let currentBotIndex = 0;
let nextBotIndex = 0;
const rotateBot = (): { previousIndex: number; nextIndex: number } => {
const previousIndex = currentBotIndex;
currentBotIndex = (currentBotIndex + 1) % bots.length;
return { previousIndex, nextIndex: currentBotIndex };
const claimBotIndex = (): number => {
const botIndex = nextBotIndex;
nextBotIndex = (nextBotIndex + 1) % bots.length;
return botIndex;
};
const sleep = (seconds: number): Promise<void> => {
@@ -24,7 +24,8 @@ const executeWithBotRetry = async <T>(
retries = 5,
attemptedBots = 0,
): Promise<T> => {
const currentBot = bots[currentBotIndex];
const botIndex = claimBotIndex();
const currentBot = bots[botIndex];
try {
return await action(currentBot);
} catch (error: unknown) {
@@ -32,17 +33,16 @@ const executeWithBotRetry = async <T>(
const match = errorStr.match(/retry after (\d+)/i);
if (match) {
const { previousIndex, nextIndex } = rotateBot();
attemptedBots++;
const nextIndex = nextBotIndex;
const nextAttemptedBots = attemptedBots + 1;
if (attemptedBots < bots.length) {
if (nextAttemptedBots < bots.length) {
logger.info(
`Bot Index ${previousIndex} hit 429. Instantly rotating to Bot Index ${nextIndex}...`,
`Bot Index ${botIndex} hit 429. Instantly rotating to Bot Index ${nextIndex}...`,
);
return executeWithBotRetry(action, retries, attemptedBots);
return executeWithBotRetry(action, retries, nextAttemptedBots);
}
// If all bots in the pool have been tried and hit 429, sleep
if (retries > 0) {
const seconds = parseInt(match[1], 10);
logger.warn(`All bots in the pool are rate-limited. Sleeping for ${seconds} seconds...`, {
@@ -159,13 +159,10 @@ export const forwardToStorage = async (
const sendMethod = sendMethodMap[fileType] || 'sendDocument';
const payload = buildSendPayload(fileType, fileName);
const uploadResult = await executeWithBotRetry((activeBot) => {
return executeWithBotRetry((activeBot) => {
const telegram = activeBot.telegram as unknown as Record<string, SendMethod>;
return telegram[sendMethod](config.storageChatId, filePayload, payload);
});
currentBotIndex = (currentBotIndex + 1) % bots.length;
return uploadResult;
});
const uploadedFile = extractUploadedFile(result, fileType);
@@ -202,16 +199,13 @@ export const forwardMediaGroupToStorage = async (
const result = await enqueueUpload(async (): Promise<TelegramMessageResult[]> => {
const mediaGroup = buildMediaGroup(items);
const uploadResult = await executeWithBotRetry((activeBot) => {
return executeWithBotRetry((activeBot) => {
const sendMediaGroup = activeBot.telegram.sendMediaGroup as unknown as (
chatId: number,
media: MediaGroupPayloadItem[],
) => Promise<TelegramMessageResult[]>;
return sendMediaGroup(config.storageChatId, mediaGroup);
});
currentBotIndex = (currentBotIndex + 1) % bots.length;
return uploadResult;
});
const messages = Array.isArray(result) ? result : [result];
@@ -239,9 +233,7 @@ export const forwardMediaGroupToStorage = async (
}
};
export const getFileInfo = async (
telegramFileId: string,
): Promise<TelegramFileInfo> => {
export const getFileInfo = async (telegramFileId: string): Promise<TelegramFileInfo> => {
try {
const result = await executeWithBotRetry((activeBot) =>
activeBot.telegram.getFile(telegramFileId),
@@ -261,6 +253,6 @@ export const getFileInfo = async (
}
};
export const getBot = (): Telegraf => bots[currentBotIndex];
export const getBot = (): Telegraf => bots[nextBotIndex];
export const getCurrentBotIndex = (): number => currentBotIndex;
export const getCurrentBotIndex = (): number => nextBotIndex;
+1 -7
View File
@@ -1,14 +1,8 @@
import PQueue from 'p-queue';
import logger from './logger';
// Create queue with concurrency limit matching bot pool size
// Concurrency: 4-8 uploads in parallel
// Interval: 1 second window for rate limiting
// IntervalCap: Max 10 tasks per second
const uploadQueue = new PQueue({
concurrency: 4,
interval: 1000,
intervalCap: 10,
concurrency: Number.POSITIVE_INFINITY,
});
// Monitor queue events