fix: Telegram-Polling-Task sauber canceln + WhatsApp CancelledError beim Shutdown unterdrücken
This commit is contained in:
@@ -21,6 +21,7 @@ class TelegramChannel(BaseChannel):
|
|||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self._app: Application | None = None
|
self._app: Application | None = None
|
||||||
|
self._polling_task: asyncio.Task | None = None
|
||||||
self._inbound_callback: Callable[[dict[str, Any]], Awaitable[None]] | None = None
|
self._inbound_callback: Callable[[dict[str, Any]], Awaitable[None]] | None = None
|
||||||
|
|
||||||
def set_inbound_callback(self, cb: Callable[[dict[str, Any]], Awaitable[None]]) -> None:
|
def set_inbound_callback(self, cb: Callable[[dict[str, Any]], Awaitable[None]]) -> None:
|
||||||
@@ -67,14 +68,21 @@ class TelegramChannel(BaseChannel):
|
|||||||
|
|
||||||
await self._app.initialize()
|
await self._app.initialize()
|
||||||
await self._app.start()
|
await self._app.start()
|
||||||
# Long-Polling als Background-Task
|
# Long-Polling als Background-Task (Referenz speichern für sauberes Cancel)
|
||||||
asyncio.create_task(self._polling_loop(), name="telegram-polling")
|
self._polling_task = asyncio.create_task(self._polling_loop(), name="telegram-polling")
|
||||||
logger.info("Telegram channel started (long-polling)")
|
logger.info("Telegram channel started (long-polling)")
|
||||||
|
|
||||||
async def stop(self) -> None:
|
async def stop(self) -> None:
|
||||||
|
# Polling-Task sauber canceln
|
||||||
|
if self._polling_task and not self._polling_task.done():
|
||||||
|
self._polling_task.cancel()
|
||||||
|
try:
|
||||||
|
await self._polling_task
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
pass
|
||||||
|
self._polling_task = None
|
||||||
|
|
||||||
if self._app:
|
if self._app:
|
||||||
# Korrekte PTB v20+ Shutdown-Reihenfolge:
|
|
||||||
# Erst Updater stoppen, dann Application stoppen/shutdown
|
|
||||||
try:
|
try:
|
||||||
if self._app.updater.running:
|
if self._app.updater.running:
|
||||||
await self._app.updater.stop()
|
await self._app.updater.stop()
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
from typing import Any, Callable, Awaitable
|
from typing import Any, Callable, Awaitable
|
||||||
|
|
||||||
@@ -98,6 +99,8 @@ class WhatsAppChannel(BaseChannel):
|
|||||||
await self._process_notification(body)
|
await self._process_notification(body)
|
||||||
if receipt_id:
|
if receipt_id:
|
||||||
await self._delete_notification(receipt_id)
|
await self._delete_notification(receipt_id)
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
raise
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
logger.error("WhatsApp poll error: %s", exc)
|
logger.error("WhatsApp poll error: %s", exc)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user