refactor: implement Redis singleton pattern for improved connection management; enhance graceful shutdown handling in bot
This commit is contained in:
parent
463bdfd089
commit
ae48a23896
@ -2,7 +2,7 @@
|
|||||||
import { type Context } from '../context';
|
import { type Context } from '../context';
|
||||||
import { logHandle } from '../helpers/logging';
|
import { logHandle } from '../helpers/logging';
|
||||||
import { TTL } from '@/config/redis';
|
import { TTL } from '@/config/redis';
|
||||||
import { createRedisInstance } from '@/utils/redis';
|
import { getRedisInstance } from '@/utils/redis';
|
||||||
import { validateTikTokUrl } from '@/utils/urls';
|
import { validateTikTokUrl } from '@/utils/urls';
|
||||||
import { Downloader } from '@tobyg74/tiktok-api-dl';
|
import { Downloader } from '@tobyg74/tiktok-api-dl';
|
||||||
import { Composer, InputFile } from 'grammy';
|
import { Composer, InputFile } from 'grammy';
|
||||||
@ -11,7 +11,7 @@ const composer = new Composer<Context>();
|
|||||||
|
|
||||||
const feature = composer.chatType('private');
|
const feature = composer.chatType('private');
|
||||||
|
|
||||||
const redis = createRedisInstance();
|
const redis = getRedisInstance();
|
||||||
|
|
||||||
feature.on('message:text', logHandle('download-message'), async (context) => {
|
feature.on('message:text', logHandle('download-message'), async (context) => {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@ -1,16 +1,37 @@
|
|||||||
import { createBot } from './bot';
|
import { createBot } from './bot';
|
||||||
import { env as environment } from './config/env';
|
import { env as environment } from './config/env';
|
||||||
import { logger } from './utils/logger';
|
import { logger } from './utils/logger';
|
||||||
|
import { getRedisInstance } from './utils/redis';
|
||||||
|
|
||||||
const bot = createBot({
|
const bot = createBot({
|
||||||
apiRoot: environment.TELEGRAM_API_ROOT,
|
apiRoot: environment.TELEGRAM_API_ROOT,
|
||||||
token: environment.BOT_TOKEN,
|
token: environment.BOT_TOKEN,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const redis = getRedisInstance();
|
||||||
|
|
||||||
|
// Graceful shutdown function
|
||||||
|
async function gracefulShutdown(signal: string) {
|
||||||
|
logger.info(`Received ${signal}, starting graceful shutdown...`);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Stop the bot
|
||||||
|
await bot.stop();
|
||||||
|
logger.info('Bot stopped');
|
||||||
|
|
||||||
|
// Disconnect Redis
|
||||||
|
redis.disconnect();
|
||||||
|
logger.info('Redis disconnected');
|
||||||
|
} catch (error) {
|
||||||
|
const err_ = error as Error;
|
||||||
|
logger.error('Error during graceful shutdown:' + err_.message || '');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Stopping the bot when the Node.js process
|
// Stopping the bot when the Node.js process
|
||||||
// is about to be terminated
|
// is about to be terminated
|
||||||
process.once('SIGINT', () => bot.stop());
|
process.once('SIGINT', () => gracefulShutdown('SIGINT'));
|
||||||
process.once('SIGTERM', () => bot.stop());
|
process.once('SIGTERM', () => gracefulShutdown('SIGTERM'));
|
||||||
|
|
||||||
bot.start({
|
bot.start({
|
||||||
onStart: ({ username }) => logger.info(`Bot ${username} started`),
|
onStart: ({ username }) => logger.info(`Bot ${username} started`),
|
||||||
|
|||||||
@ -2,7 +2,15 @@ import { env } from '@/config/env';
|
|||||||
import { logger } from '@/utils/logger';
|
import { logger } from '@/utils/logger';
|
||||||
import Redis from 'ioredis';
|
import Redis from 'ioredis';
|
||||||
|
|
||||||
export function createRedisInstance() {
|
const instance: Redis = createRedisInstance();
|
||||||
|
|
||||||
|
export function getRedisInstance() {
|
||||||
|
if (!instance) return createRedisInstance();
|
||||||
|
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
function createRedisInstance() {
|
||||||
const redis = new Redis({
|
const redis = new Redis({
|
||||||
host: env.REDIS_HOST,
|
host: env.REDIS_HOST,
|
||||||
password: env.REDIS_PASSWORD,
|
password: env.REDIS_PASSWORD,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user