mainline to use timeouts

This commit is contained in:
minicx 2023-02-16 19:24:25 +03:00
parent 0718801b76
commit 0b84e14557
4 changed files with 29 additions and 27 deletions

View file

@ -10,6 +10,7 @@ export async function verification(link: string, hero: Hero) {
await hero.goto(link, { timeoutMs: 120_000 });
await hero.waitForLoad("AllContentLoaded", { timeoutMs: 120_000 });
} catch (err) {
console.log(`Account not verificated`);
console.log(err)
return
}

View file

@ -63,7 +63,7 @@ export async function processWorkers(dockerContainer: wrappedContainer, workerCo
}
continue mainLoop;
}
await sleep(Math.random() * 50_000);
// await sleep(Math.random() * 50_000);
if ((await telegramWorker.getMessagesByEntity(botEntity)).length == 0) { // first time activation
if (worker.refferal !== null) {

View file

@ -85,11 +85,11 @@ export function randomInRange(a: number, b: number) {
return Math.floor(Math.random() * (b - a + 1) + b);
}
export class TimeoutError extends Error {}
export async function timeout(func: Promise<any>,timeoutMS=20_000) {
await Promise.race([
export async function timeout(func: Promise<any>,timeoutMS=20_000,errorToThrow: Error=new TimeoutError(`Timeout Error after waiting ${timeoutMS}`)) {
return await Promise.race([
new Promise((_, reject) => {
setTimeout(() => {
reject(new TimeoutError(`Timeout Error after waiting ${timeoutMS}`));
reject(errorToThrow);
}, timeoutMS);
}),
func

View file

@ -5,7 +5,7 @@ import { BadRequestError, TimedOutError } from "telegram/errors";
import { IWorker } from "./IWorker";
import { LogLevel } from "telegram/extensions/Logger";
import { settings } from "../Databases/JSONDatabase";
import { UnknownError } from "../Helpers/utils";
import { timeout, UnknownError } from "../Helpers/utils";
import { db } from "../Databases/SQLDatabase";
import { sleep, TotalList } from "telegram/Helpers";
@ -57,29 +57,30 @@ export class TelegramWorker extends telegram.TelegramClient {
}
async waitForNewMessages(entity: EntityLike, idOfLastMessage: number, waitFor: number | string = 1, timeoutMS = 20 * 1_000) {
const start_time = (new Date()).getTime();
if (isString(waitFor)) {
while ((await this.getMessagesByEntity(entity, { minId: idOfLastMessage, limit: 2147483647 })).filter((message) => {
return message.message.includes(waitFor)
}).length === 0) {
if (((new Date()).getTime() - start_time) >= timeoutMS) {
throw new NoNewMessagesError("No new messages");
async waitForNewMessages(entity: EntityLike, idOfLastMessage: number, waitFor: number | string = 1, timeoutMS = 20 * 1_000): Promise<telegram.Api.Message[]> {
return await timeout(
(async ()=>{
if (isString(waitFor)) {
while ((await this.getMessagesByEntity(entity, { minId: idOfLastMessage, limit: 2147483647 })).filter((message) => {
return message.message.includes(waitFor)
}).length === 0) {
await sleep(500);
}
return (await this.getMessagesByEntity(entity, { minId: idOfLastMessage, limit: 2147483647 })).filter((message) => {
return message.message.includes(waitFor)
})
} else {
while ((await this.getMessagesByEntity(entity, { minId: idOfLastMessage, limit: 2147483647 })).length < waitFor) {
await sleep(500);
}
return (await this.getMessagesByEntity(entity, { minId: idOfLastMessage, limit: 2147483647 })).slice(0, waitFor)
}
await sleep(500);
}
return (await this.getMessagesByEntity(entity, { minId: idOfLastMessage, limit: 2147483647 })).filter((message) => {
return message.message.includes(waitFor)
})
} else {
while ((await this.getMessagesByEntity(entity, { minId: idOfLastMessage, limit: 2147483647 })).length < waitFor) {
if (((new Date()).getTime() - start_time) >= timeoutMS) {
throw new NoNewMessagesError("No new messages");
}
await sleep(500);
}
return (await this.getMessagesByEntity(entity, { minId: idOfLastMessage, limit: 2147483647 })).slice(0, waitFor)
}
})(),
timeoutMS,
new NoNewMessagesError("No new messages")
)
}
async isBotBanned(entity: Required<Pick<telegram.Api.User, "username">>) {
const blockedUsers = (