refactor: disable sequentialize & session

This commit is contained in:
vchikalkin 2025-08-25 11:07:51 +03:00
parent 44978c8b7f
commit 7a08f968f4
2 changed files with 2 additions and 17 deletions

View File

@ -4,17 +4,14 @@ import * as features from './features';
import { errorHandler } from './handlers/errors'; import { errorHandler } from './handlers/errors';
import { i18n } from './i18n'; import { i18n } from './i18n';
import * as middlewares from './middlewares'; import * as middlewares from './middlewares';
import { session } from './middlewares';
import { setCommands } from './settings/commands'; import { setCommands } from './settings/commands';
import { setInfo } from './settings/info'; import { setInfo } from './settings/info';
import { env } from '@/config/env'; import { env } from '@/config/env';
import { logger } from '@/utils/logger'; import { logger } from '@/utils/logger';
import { getRedisInstance } from '@/utils/redis'; import { getRedisInstance } from '@/utils/redis';
import { getSessionKey } from '@/utils/session';
import { autoChatAction } from '@grammyjs/auto-chat-action'; import { autoChatAction } from '@grammyjs/auto-chat-action';
import { hydrate } from '@grammyjs/hydrate'; import { hydrate } from '@grammyjs/hydrate';
import { limit } from '@grammyjs/ratelimiter'; import { limit } from '@grammyjs/ratelimiter';
import { sequentialize } from '@grammyjs/runner';
import { Bot } from 'grammy'; import { Bot } from 'grammy';
type Parameters_ = { type Parameters_ = {
@ -35,9 +32,7 @@ export function createBot({ apiRoot, token }: Parameters_) {
bot.use( bot.use(
limit({ limit({
keyGenerator: (ctx) => { keyGenerator: (ctx) => ctx.from?.id.toString(),
return ctx.from?.id.toString();
},
limit: env.RATE_LIMIT, limit: env.RATE_LIMIT,
onLimitExceeded: async (ctx) => { onLimitExceeded: async (ctx) => {
await ctx.reply(ctx.t('err-limit-exceeded')); await ctx.reply(ctx.t('err-limit-exceeded'));
@ -51,7 +46,6 @@ export function createBot({ apiRoot, token }: Parameters_) {
context.logger = logger.child({ context.logger = logger.child({
update_id: context.update.update_id, update_id: context.update.update_id,
}); });
await next(); await next();
}); });
@ -60,9 +54,6 @@ export function createBot({ apiRoot, token }: Parameters_) {
const protectedBot = bot.errorBoundary(errorHandler); const protectedBot = bot.errorBoundary(errorHandler);
protectedBot.use(sequentialize(getSessionKey));
protectedBot.use(session());
protectedBot.use(middlewares.updateLogger()); protectedBot.use(middlewares.updateLogger());
protectedBot.use(autoChatAction(bot.api)); protectedBot.use(autoChatAction(bot.api));
protectedBot.use(hydrate()); protectedBot.use(hydrate());

View File

@ -12,23 +12,19 @@ const bot = createBot({
bot.catch((error) => { bot.catch((error) => {
logger.error('Grammy bot error:'); logger.error('Grammy bot error:');
logger.error(`Message: ${error?.message}`); logger.error(`Message: ${error?.message}`);
logger.error(error.error); // собственно, ошибка logger.error(error.error);
}); });
const runner = run(bot); const runner = run(bot);
const redis = getRedisInstance(); const redis = getRedisInstance();
// Graceful shutdown function
async function gracefulShutdown(signal: string) { async function gracefulShutdown(signal: string) {
logger.info(`Received ${signal}, starting graceful shutdown...`); logger.info(`Received ${signal}, starting graceful shutdown...`);
try { try {
// Stop the bot
await runner.stop(); await runner.stop();
logger.info('Bot stopped'); logger.info('Bot stopped');
// Disconnect Redis
redis.disconnect(); redis.disconnect();
logger.info('Redis disconnected'); logger.info('Redis disconnected');
} catch (error) { } catch (error) {
@ -37,8 +33,6 @@ async function gracefulShutdown(signal: string) {
} }
} }
// Stopping the bot when the Node.js process
// is about to be terminated
process.once('SIGINT', () => gracefulShutdown('SIGINT')); process.once('SIGINT', () => gracefulShutdown('SIGINT'));
process.once('SIGTERM', () => gracefulShutdown('SIGTERM')); process.once('SIGTERM', () => gracefulShutdown('SIGTERM'));