Skip to content

Commit

Permalink
added more logs
Browse files Browse the repository at this point in the history
  • Loading branch information
TheodoreKrypton committed Apr 29, 2024
1 parent d2142d7 commit e67a524
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/api/client/file-desc-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export class FileDescApi extends MessageApi {
fd: TGFSFile,
messageId?: number,
): Promise<number> {
Logger.debug(
`sendFileDesc ${JSON.stringify(fd.toObject())}, messageId=${messageId}`,
);
if (!messageId) {
return await this.sendText(JSON.stringify(fd.toObject()));
}
Expand Down
3 changes: 3 additions & 0 deletions src/api/client/message-api/file-uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ export abstract class FileUploader<T extends GeneralFileMessage> {
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`,
Expand Down
13 changes: 13 additions & 0 deletions src/api/impl/gramjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -106,6 +107,7 @@ export class GramJSApi implements ITDLibClient {
return res;
}

@retry()
public async getMessages(
req: types.GetMessagesReq,
): Promise<types.GetMessagesResp> {
Expand All @@ -115,6 +117,7 @@ export class GramJSApi implements ITDLibClient {
return GramJSApi.transformMessages(rsp);
}

@retry()
public async sendText(req: types.SendTextReq): Promise<types.Message> {
const rsp = await this.client.sendMessage(req.chatId, {
message: req.text,
Expand All @@ -124,6 +127,7 @@ export class GramJSApi implements ITDLibClient {
};
}

@retry()
public async editMessageText(
req: types.EditMessageTextReq,
): Promise<types.Message> {
Expand All @@ -136,6 +140,7 @@ export class GramJSApi implements ITDLibClient {
};
}

@retry()
public async editMessageMedia(
req: types.EditMessageMediaReq,
): Promise<types.Message> {
Expand All @@ -153,6 +158,7 @@ export class GramJSApi implements ITDLibClient {
};
}

@retry()
public async searchMessages(
req: types.SearchMessagesReq,
): Promise<types.GetMessagesResp> {
Expand All @@ -163,6 +169,7 @@ export class GramJSApi implements ITDLibClient {
return GramJSApi.transformMessages(rsp);
}

@retry()
public async getPinnedMessages(
req: types.GetPinnedMessagesReq,
): Promise<types.GetMessagesResp> {
Expand All @@ -173,10 +180,12 @@ export class GramJSApi implements ITDLibClient {
return GramJSApi.transformMessages(rsp);
}

@retry()
public async pinMessage(req: types.PinMessageReq): Promise<void> {
await this.client.pinMessage(req.chatId, req.messageId);
}

@retry()
public async saveBigFilePart(
req: types.SaveBigFilePartReq,
): Promise<types.SaveFilePartResp> {
Expand All @@ -193,6 +202,7 @@ export class GramJSApi implements ITDLibClient {
};
}

@retry()
public async saveFilePart(
req: types.SaveFilePartReq,
): Promise<types.SaveFilePartResp> {
Expand All @@ -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({
Expand All @@ -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({
Expand All @@ -237,6 +249,7 @@ export class GramJSApi implements ITDLibClient {
};
}

@retry()
public async downloadFile(
req: types.DownloadFileReq,
): Promise<types.DownloadFileResp> {
Expand Down
3 changes: 2 additions & 1 deletion src/api/ops/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
}
};
2 changes: 1 addition & 1 deletion src/api/ops/navigate-to-dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/api/ops/remove-dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/api/ops/remove-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
};
3 changes: 2 additions & 1 deletion src/api/ops/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions src/api/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ export const saveToFile = async (
const content = await saveToBuffer(generator);
writeFileSync(path, content);
};

30 changes: 30 additions & 0 deletions src/utils/retry.ts
Original file line number Diff line number Diff line change
@@ -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;
};
}

0 comments on commit e67a524

Please sign in to comment.