miltubot devel

This commit is contained in:
2025-08-08 11:51:27 +09:00
parent ce28f29c69
commit 423ebf625b
4 changed files with 37 additions and 10 deletions

View File

@@ -0,0 +1,28 @@
/*M!999999\- enable the sandbox mode */
-- MariaDB dump 10.19-11.6.2-MariaDB, for debian-linux-gnu (x86_64)
--
-- Host: 127.0.0.1 Database: tg_autopost
-- ------------------------------------------------------
-- Server version 11.6.2-MariaDB-ubu2404-log
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*M!100616 SET @OLD_NOTE_VERBOSITY=@@NOTE_VERBOSITY, NOTE_VERBOSITY=0 */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*M!100616 SET NOTE_VERBOSITY=@OLD_NOTE_VERBOSITY */;
-- Dump completed on 2025-08-08 11:37:37

View File

@@ -1,7 +1,8 @@
# bot/management/commands/runbots.py
import asyncio import asyncio
import logging import logging
import signal import signal
from typing import Sequence
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from bot.models import TelegramBot from bot.models import TelegramBot
from bot.bot_factory import build_application_for_bot from bot.bot_factory import build_application_for_bot
@@ -13,27 +14,27 @@ class Command(BaseCommand):
def handle(self, *args, **options): def handle(self, *args, **options):
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s: %(message)s") logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s: %(message)s")
asyncio.run(self._amain())
async def _amain(self): # ORM здесь, в синхронном контексте
bots = list(TelegramBot.objects.filter(is_active=True)) bots: Sequence[TelegramBot] = list(TelegramBot.objects.filter(is_active=True))
if not bots: if not bots:
self.stderr.write(self.style.ERROR("Нет активных ботов (is_active=True).")) self.stderr.write(self.style.ERROR("Нет активных ботов (is_active=True)."))
return return
asyncio.run(self._amain(bots))
async def _amain(self, bots: Sequence[TelegramBot]):
apps = [] apps = []
try: try:
# Инициализация и старт polling для каждого бота
for tb in bots: for tb in bots:
app, allowed_updates = build_application_for_bot(tb) app, allowed_updates = build_application_for_bot(tb)
await app.initialize() await app.initialize()
await app.start() await app.start()
# в 22.x polling запускается через updater
await app.updater.start_polling(allowed_updates=allowed_updates) await app.updater.start_polling(allowed_updates=allowed_updates)
apps.append(app) apps.append(app)
log.info("Bot started: %s (@%s)", tb.name, tb.username or "") log.info("Bot started: %s (@%s)", tb.name, tb.username or "")
# Ожидание сигнала остановки # Ждём SIGINT/SIGTERM
stop_event = asyncio.Event() stop_event = asyncio.Event()
def _stop(*_): def _stop(*_):
@@ -44,13 +45,11 @@ class Command(BaseCommand):
try: try:
loop.add_signal_handler(sig, _stop) loop.add_signal_handler(sig, _stop)
except NotImplementedError: except NotImplementedError:
# Windows pass # Windows
pass
await stop_event.wait() await stop_event.wait()
finally: finally:
# Корректная остановка всех приложений
for app in reversed(apps): for app in reversed(apps):
try: try:
await app.updater.stop() await app.updater.stop()

Binary file not shown.

Binary file not shown.