From e67a52427a42d15526b7fed17722b848c59e67ab Mon Sep 17 00:00:00 2001 From: Wheat Carrier Date: Mon, 29 Apr 2024 21:02:28 +0800 Subject: [PATCH] added more logs --- src/api/client/file-desc-api.ts | 3 +++ src/api/client/message-api/file-uploader.ts | 3 +++ src/api/impl/gramjs.ts | 13 +++++++++ src/api/ops/list.ts | 3 ++- src/api/ops/navigate-to-dir.ts | 2 +- src/api/ops/remove-dir.ts | 2 +- src/api/ops/remove-file.ts | 2 +- src/api/ops/upload.ts | 3 ++- src/api/utils/index.ts | 1 + src/utils/retry.ts | 30 +++++++++++++++++++++ 10 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 src/utils/retry.ts diff --git a/src/api/client/file-desc-api.ts b/src/api/client/file-desc-api.ts index d8292ce..4f2921c 100644 --- a/src/api/client/file-desc-api.ts +++ b/src/api/client/file-desc-api.ts @@ -11,6 +11,9 @@ export class FileDescApi extends MessageApi { fd: TGFSFile, messageId?: number, ): Promise { + Logger.debug( + `sendFileDesc ${JSON.stringify(fd.toObject())}, messageId=${messageId}`, + ); if (!messageId) { return await this.sendText(JSON.stringify(fd.toObject())); } diff --git a/src/api/client/message-api/file-uploader.ts b/src/api/client/message-api/file-uploader.ts index 34d85fe..f5201f7 100644 --- a/src/api/client/message-api/file-uploader.ts +++ b/src/api/client/message-api/file-uploader.ts @@ -92,6 +92,9 @@ export abstract class FileUploader { filePart, bytes: chunk, }); + Logger.debug( + `[worker ${workerId}] uploaded chunk ${filePart} success=${rsp.success}`, + ); if (!rsp.success) { throw new TechnicalError( `File chunk ${filePart} of ${this.fileName} failed to upload`, diff --git a/src/api/impl/gramjs.ts b/src/api/impl/gramjs.ts index 8b1c0ba..1b06d22 100644 --- a/src/api/impl/gramjs.ts +++ b/src/api/impl/gramjs.ts @@ -10,6 +10,7 @@ import { ITDLibClient } from 'src/api/interface'; import * as types from 'src/api/types'; import { Config } from 'src/config'; import { Logger } from 'src/utils/logger'; +import { retry } from 'src/utils/retry'; import { sleep } from 'src/utils/sleep'; type AuthDetails = { @@ -106,6 +107,7 @@ export class GramJSApi implements ITDLibClient { return res; } + @retry() public async getMessages( req: types.GetMessagesReq, ): Promise { @@ -115,6 +117,7 @@ export class GramJSApi implements ITDLibClient { return GramJSApi.transformMessages(rsp); } + @retry() public async sendText(req: types.SendTextReq): Promise { const rsp = await this.client.sendMessage(req.chatId, { message: req.text, @@ -124,6 +127,7 @@ export class GramJSApi implements ITDLibClient { }; } + @retry() public async editMessageText( req: types.EditMessageTextReq, ): Promise { @@ -136,6 +140,7 @@ export class GramJSApi implements ITDLibClient { }; } + @retry() public async editMessageMedia( req: types.EditMessageMediaReq, ): Promise { @@ -153,6 +158,7 @@ export class GramJSApi implements ITDLibClient { }; } + @retry() public async searchMessages( req: types.SearchMessagesReq, ): Promise { @@ -163,6 +169,7 @@ export class GramJSApi implements ITDLibClient { return GramJSApi.transformMessages(rsp); } + @retry() public async getPinnedMessages( req: types.GetPinnedMessagesReq, ): Promise { @@ -173,10 +180,12 @@ export class GramJSApi implements ITDLibClient { return GramJSApi.transformMessages(rsp); } + @retry() public async pinMessage(req: types.PinMessageReq): Promise { await this.client.pinMessage(req.chatId, req.messageId); } + @retry() public async saveBigFilePart( req: types.SaveBigFilePartReq, ): Promise { @@ -193,6 +202,7 @@ export class GramJSApi implements ITDLibClient { }; } + @retry() public async saveFilePart( req: types.SaveFilePartReq, ): Promise { @@ -208,6 +218,7 @@ export class GramJSApi implements ITDLibClient { }; } + @retry() public async sendBigFile(req: types.SendFileReq) { const rsp = await this.client.sendFile(req.chatId, { file: new Api.InputFileBig({ @@ -222,6 +233,7 @@ export class GramJSApi implements ITDLibClient { }; } + @retry() public async sendSmallFile(req: types.SendFileReq) { const rsp = await this.client.sendFile(req.chatId, { file: new Api.InputFile({ @@ -237,6 +249,7 @@ export class GramJSApi implements ITDLibClient { }; } + @retry() public async downloadFile( req: types.DownloadFileReq, ): Promise { diff --git a/src/api/ops/list.ts b/src/api/ops/list.ts index 148f26d..4152fca 100644 --- a/src/api/ops/list.ts +++ b/src/api/ops/list.ts @@ -27,7 +27,8 @@ export const list = if (nextFile) { return nextFile; } else { - throw new FileOrDirectoryDoesNotExistError(path.toString()); + const pathStr = path.toString(); + throw new FileOrDirectoryDoesNotExistError(pathStr, `list ${pathStr}`); } } }; diff --git a/src/api/ops/navigate-to-dir.ts b/src/api/ops/navigate-to-dir.ts index 3196916..8a6fb34 100644 --- a/src/api/ops/navigate-to-dir.ts +++ b/src/api/ops/navigate-to-dir.ts @@ -12,7 +12,7 @@ export const navigateToDir = (client: Client) => async (path: string) => { for (const pathPart of pathParts) { const directory = currentDirectory.findChildren([pathPart])[0]; if (!directory) { - throw new FileOrDirectoryDoesNotExistError(path); + throw new FileOrDirectoryDoesNotExistError(path, `navigate to ${path}`); } currentDirectory = directory; diff --git a/src/api/ops/remove-dir.ts b/src/api/ops/remove-dir.ts index 3520ee4..c778905 100644 --- a/src/api/ops/remove-dir.ts +++ b/src/api/ops/remove-dir.ts @@ -13,7 +13,7 @@ export const removeDir = if (child) { await client.deleteEmptyDirectory(child); } else { - throw new FileOrDirectoryDoesNotExistError(path); + throw new FileOrDirectoryDoesNotExistError(path, `remove dir ${path}`); } } else { const nextDir = name ? dir.findChildren([name])[0] : dir; diff --git a/src/api/ops/remove-file.ts b/src/api/ops/remove-file.ts index fbe023d..4e573ab 100644 --- a/src/api/ops/remove-file.ts +++ b/src/api/ops/remove-file.ts @@ -11,6 +11,6 @@ export const removeFile = (client: Client) => async (path: string) => { if (fileRef) { await client.deleteFile(fileRef); } else { - throw new FileOrDirectoryDoesNotExistError(path); + throw new FileOrDirectoryDoesNotExistError(path, `remove file ${path}`); } }; diff --git a/src/api/ops/upload.ts b/src/api/ops/upload.ts index 92dabe6..5a27319 100644 --- a/src/api/ops/upload.ts +++ b/src/api/ops/upload.ts @@ -14,7 +14,8 @@ export const uploadFromLocal = const dir = await navigateToDir(client)(basePath); if (!fs.existsSync(local.toString())) { - throw new FileOrDirectoryDoesNotExistError(local.toString()); + const path = local.toString(); + throw new FileOrDirectoryDoesNotExistError(path, `upload from ${path}`); } return await client.uploadFile( diff --git a/src/api/utils/index.ts b/src/api/utils/index.ts index 7c0c375..48b06dd 100644 --- a/src/api/utils/index.ts +++ b/src/api/utils/index.ts @@ -27,3 +27,4 @@ export const saveToFile = async ( const content = await saveToBuffer(generator); writeFileSync(path, content); }; + diff --git a/src/utils/retry.ts b/src/utils/retry.ts new file mode 100644 index 0000000..15de4dc --- /dev/null +++ b/src/utils/retry.ts @@ -0,0 +1,30 @@ +import { Logger } from 'src/utils/logger'; + +export function retry( + retries: number = 3, + backoff: number = 500, +): MethodDecorator { + return function ( + _target: Object, + propertyKey: string | symbol, + descriptor: PropertyDescriptor, + ) { + const originalMethod = descriptor.value; + descriptor.value = async function (...args: any[]) { + for (let i = 0; i <= retries; i++) { + try { + const result = await originalMethod.apply(this, args); + return result; + } catch (error) { + if (i === retries) throw error; + const waitTime = Math.pow(2, i) * backoff; + Logger.error( + `Method ${String(propertyKey)}: Attempt ${i + 1} failed. Retrying in ${waitTime}ms...`, + ); + await new Promise((resolve) => setTimeout(resolve, waitTime)); + } + } + }; + return descriptor; + }; +}