forked from pinks/eris
1
0
Fork 0

perf: parallel tg update processing

This commit is contained in:
pinks 2023-10-13 18:10:16 +02:00
parent 0b85c99c92
commit f0a570c1b9
3 changed files with 14 additions and 4 deletions

View File

@ -1,7 +1,9 @@
import { Api, Bot, Context, RawApi, session, SessionFlavor } from "grammy"; import { Api, Bot, Context, RawApi, session, SessionFlavor } from "grammy";
import { FileFlavor, hydrateFiles } from "grammy_files"; import { FileFlavor, hydrateFiles } from "grammy_files";
import { hydrateReply, ParseModeFlavor } from "grammy_parse_mode"; import { hydrateReply, ParseModeFlavor } from "grammy_parse_mode";
import { run, sequentialize } from "grammy_runner";
import { getLogger } from "std/log/mod.ts"; import { getLogger } from "std/log/mod.ts";
import { sessions } from "../api/sessionsRoute.ts";
import { getConfig, setConfig } from "../app/config.ts"; import { getConfig, setConfig } from "../app/config.ts";
import { formatUserChat } from "../utils/formatUserChat.ts"; import { formatUserChat } from "../utils/formatUserChat.ts";
import { broadcastCommand } from "./broadcastCommand.ts"; import { broadcastCommand } from "./broadcastCommand.ts";
@ -10,7 +12,6 @@ import { img2imgCommand, img2imgQuestion } from "./img2imgCommand.ts";
import { pnginfoCommand, pnginfoQuestion } from "./pnginfoCommand.ts"; import { pnginfoCommand, pnginfoQuestion } from "./pnginfoCommand.ts";
import { queueCommand } from "./queueCommand.ts"; import { queueCommand } from "./queueCommand.ts";
import { txt2imgCommand, txt2imgQuestion } from "./txt2imgCommand.ts"; import { txt2imgCommand, txt2imgQuestion } from "./txt2imgCommand.ts";
import { sessions } from "../api/sessionsRoute.ts";
export const logger = () => getLogger(); export const logger = () => getLogger();
@ -47,6 +48,9 @@ export const bot = new Bot<ErisContext, ErisApi>(
); );
bot.use(hydrateReply); bot.use(hydrateReply);
bot.use(sequentialize((ctx) => ctx.chat?.id.toString()));
bot.use(session<SessionData, ErisContext>({ bot.use(session<SessionData, ErisContext>({
type: "multi", type: "multi",
chat: { chat: {
@ -171,7 +175,7 @@ bot.command("pause", async (ctx) => {
bot.command("resume", async (ctx) => { bot.command("resume", async (ctx) => {
if (!ctx.from?.username) return; if (!ctx.from?.username) return;
let config = await getConfig(); const config = await getConfig();
if (!config.adminUsernames.includes(ctx.from.username)) return; if (!config.adminUsernames.includes(ctx.from.username)) return;
if (config.pausedReason == null) return ctx.reply("Already running"); if (config.pausedReason == null) return ctx.reply("Already running");
await setConfig({ pausedReason: null }); await setConfig({ pausedReason: null });
@ -182,3 +186,8 @@ bot.command("resume", async (ctx) => {
bot.command("crash", () => { bot.command("crash", () => {
throw new Error("Crash command used"); throw new Error("Crash command used");
}); });
export async function runBot() {
const runner = run(bot);
await runner.task();
}

View File

@ -25,6 +25,7 @@
"serve_spa": "https://deno.land/x/serve_spa@v0.2.0/mod.ts", "serve_spa": "https://deno.land/x/serve_spa@v0.2.0/mod.ts",
"reroute": "https://deno.land/x/reroute@v0.1.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": "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_types": "https://lib.deno.dev/x/grammy_types@3/mod.ts",
"grammy_autoquote": "https://lib.deno.dev/x/grammy_autoquote@1/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", "grammy_parse_mode": "https://lib.deno.dev/x/grammy_parse_mode@1/mod.ts",

View File

@ -3,7 +3,7 @@ import { ConsoleHandler } from "std/log/handlers.ts";
import { setup } from "std/log/mod.ts"; import { setup } from "std/log/mod.ts";
import { serveUi } from "./api/mod.ts"; import { serveUi } from "./api/mod.ts";
import { runAllTasks } from "./app/mod.ts"; import { runAllTasks } from "./app/mod.ts";
import { bot } from "./bot/mod.ts"; import { runBot } from "./bot/mod.ts";
// setup logging // setup logging
setup({ setup({
@ -17,7 +17,7 @@ setup({
// run parts of the app // run parts of the app
await Promise.all([ await Promise.all([
bot.start(), runBot(),
runAllTasks(), runAllTasks(),
serveUi(), serveUi(),
]); ]);