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
+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;