41 lines
1.0 KiB
Python
41 lines
1.0 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
from typing import TYPE_CHECKING
|
|
|
|
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
|
from apscheduler.triggers.interval import IntervalTrigger
|
|
|
|
if TYPE_CHECKING:
|
|
from channels.whatsapp_channel import WhatsAppChannel
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
_scheduler: AsyncIOScheduler | None = None
|
|
_whatsapp: "WhatsAppChannel | None" = None
|
|
|
|
|
|
def build_scheduler(whatsapp: "WhatsAppChannel") -> AsyncIOScheduler:
|
|
global _scheduler, _whatsapp
|
|
_whatsapp = whatsapp
|
|
|
|
_scheduler = AsyncIOScheduler(timezone="UTC")
|
|
|
|
# WhatsApp-Polling alle 10 Sekunden (coalesce=True überspringt verpasste Läufe)
|
|
_scheduler.add_job(
|
|
_poll_whatsapp,
|
|
trigger=IntervalTrigger(seconds=10),
|
|
id="whatsapp-poll",
|
|
name="WhatsApp incoming messages",
|
|
max_instances=1,
|
|
coalesce=True,
|
|
misfire_grace_time=5,
|
|
)
|
|
|
|
return _scheduler
|
|
|
|
|
|
async def _poll_whatsapp() -> None:
|
|
if _whatsapp:
|
|
await _whatsapp.poll_incoming()
|