From f0a570c1b947d7dfb339a63f326075946450bc90 Mon Sep 17 00:00:00 2001 From: pinks Date: Fri, 13 Oct 2023 18:10:16 +0200 Subject: [PATCH] perf: parallel tg update processing --- bot/mod.ts | 13 +++++++++++-- deno.json | 1 + main.ts | 4 ++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/bot/mod.ts b/bot/mod.ts index c70e6a9..46e0220 100644 --- a/bot/mod.ts +++ b/bot/mod.ts @@ -1,7 +1,9 @@ import { Api, Bot, Context, RawApi, session, SessionFlavor } from "grammy"; import { FileFlavor, hydrateFiles } from "grammy_files"; import { hydrateReply, ParseModeFlavor } from "grammy_parse_mode"; +import { run, sequentialize } from "grammy_runner"; import { getLogger } from "std/log/mod.ts"; +import { sessions } from "../api/sessionsRoute.ts"; import { getConfig, setConfig } from "../app/config.ts"; import { formatUserChat } from "../utils/formatUserChat.ts"; import { broadcastCommand } from "./broadcastCommand.ts"; @@ -10,7 +12,6 @@ import { img2imgCommand, img2imgQuestion } from "./img2imgCommand.ts"; import { pnginfoCommand, pnginfoQuestion } from "./pnginfoCommand.ts"; import { queueCommand } from "./queueCommand.ts"; import { txt2imgCommand, txt2imgQuestion } from "./txt2imgCommand.ts"; -import { sessions } from "../api/sessionsRoute.ts"; export const logger = () => getLogger(); @@ -47,6 +48,9 @@ export const bot = new Bot( ); bot.use(hydrateReply); + +bot.use(sequentialize((ctx) => ctx.chat?.id.toString())); + bot.use(session({ type: "multi", chat: { @@ -171,7 +175,7 @@ bot.command("pause", async (ctx) => { bot.command("resume", async (ctx) => { if (!ctx.from?.username) return; - let config = await getConfig(); + const config = await getConfig(); if (!config.adminUsernames.includes(ctx.from.username)) return; if (config.pausedReason == null) return ctx.reply("Already running"); await setConfig({ pausedReason: null }); @@ -182,3 +186,8 @@ bot.command("resume", async (ctx) => { bot.command("crash", () => { throw new Error("Crash command used"); }); + +export async function runBot() { + const runner = run(bot); + await runner.task(); +} diff --git a/deno.json b/deno.json index 92f715d..34d5777 100644 --- a/deno.json +++ b/deno.json @@ -25,6 +25,7 @@ "serve_spa": "https://deno.land/x/serve_spa@v0.2.0/mod.ts", "reroute": "https://deno.land/x/reroute@v0.1.0/mod.ts", "grammy": "https://lib.deno.dev/x/grammy@1/mod.ts", + "grammy_runner": "https://lib.deno.dev/x/grammy_runner@2/mod.ts", "grammy_types": "https://lib.deno.dev/x/grammy_types@3/mod.ts", "grammy_autoquote": "https://lib.deno.dev/x/grammy_autoquote@1/mod.ts", "grammy_parse_mode": "https://lib.deno.dev/x/grammy_parse_mode@1/mod.ts", diff --git a/main.ts b/main.ts index dc8f551..bb9f816 100644 --- a/main.ts +++ b/main.ts @@ -3,7 +3,7 @@ import { ConsoleHandler } from "std/log/handlers.ts"; import { setup } from "std/log/mod.ts"; import { serveUi } from "./api/mod.ts"; import { runAllTasks } from "./app/mod.ts"; -import { bot } from "./bot/mod.ts"; +import { runBot } from "./bot/mod.ts"; // setup logging setup({ @@ -17,7 +17,7 @@ setup({ // run parts of the app await Promise.all([ - bot.start(), + runBot(), runAllTasks(), serveUi(), ]);