pooling update handlers, add overlay bot module
This commit is contained in:
@@ -1,17 +1,25 @@
|
||||
import logging
|
||||
import traceback
|
||||
from multiprocessing.pool import ThreadPool
|
||||
|
||||
import sentry_sdk
|
||||
from django.core.cache import cache
|
||||
from django.core.management import BaseCommand
|
||||
from telegram import TelegramError
|
||||
from telegram import TelegramError, Update
|
||||
from telegram.error import TimedOut
|
||||
from telegram.ext import CallbackContext
|
||||
|
||||
from bots.models import TelegramBot
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
def handle(self, *args, **options):
|
||||
pool = ThreadPool(8)
|
||||
|
||||
def error_handler(update: Update, ctx: CallbackContext):
|
||||
sentry_sdk.capture_exception(ctx.error)
|
||||
logging.exception('Exception while processing update', exc_info=ctx.error)
|
||||
|
||||
dispatchers = []
|
||||
while True:
|
||||
try:
|
||||
@@ -20,7 +28,7 @@ class Command(BaseCommand):
|
||||
dispatchers = []
|
||||
for bot in TelegramBot.objects.filter(active=True):
|
||||
try:
|
||||
dispatcher = bot.build_dispatcher()
|
||||
dispatcher = bot.build_dispatcher(error_handler)
|
||||
dispatcher.last_update_id = 0
|
||||
dispatchers.append(dispatcher)
|
||||
except TelegramError:
|
||||
@@ -38,15 +46,11 @@ class Command(BaseCommand):
|
||||
updates = []
|
||||
|
||||
for update in updates:
|
||||
try:
|
||||
dispatcher.process_update(update)
|
||||
except KeyboardInterrupt:
|
||||
return
|
||||
except Exception as e:
|
||||
sentry_sdk.capture_exception(e)
|
||||
traceback.print_exc()
|
||||
pool.apply_async(dispatcher.process_update, (update,))
|
||||
dispatcher.last_update_id = update.update_id + 1
|
||||
except KeyboardInterrupt:
|
||||
pool.terminate()
|
||||
pool.join()
|
||||
return
|
||||
except Exception as e:
|
||||
sentry_sdk.capture_exception(e)
|
||||
|
Reference in New Issue
Block a user