feat: Queue-Worker mit Batch-Verarbeitung und Fehlertoleranz
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
import hashlib
|
||||
import hmac
|
||||
import json
|
||||
import os
|
||||
|
||||
from src.models.submission import Submission
|
||||
from src.queue.handler import QueueHandler
|
||||
|
||||
_MIN_TELEGRAM_TOKEN_LENGTH = 21
|
||||
|
||||
|
||||
def verify_whatsapp_signature(
|
||||
payload: bytes, signature: str, app_secret: str | None = None
|
||||
) -> bool:
|
||||
"""Verifiziert die WhatsApp X-Hub-Signature-256."""
|
||||
secret = app_secret or os.environ.get("WHATSAPP_APP_SECRET", "")
|
||||
if not secret:
|
||||
return False
|
||||
expected = hmac.new(secret.encode(), payload, hashlib.sha256).hexdigest()
|
||||
return hmac.compare_digest(f"sha256={expected}", signature)
|
||||
|
||||
|
||||
def verify_telegram_signature(token: str | None = None) -> bool:
|
||||
"""Verifiziert den Telegram Bot-Token."""
|
||||
expected = token or os.environ.get("TELEGRAM_BOT_TOKEN", "")
|
||||
if not expected:
|
||||
return False
|
||||
return len(expected) >= _MIN_TELEGRAM_TOKEN_LENGTH
|
||||
|
||||
|
||||
def validate_and_enqueue(payload: bytes, signature: str, plattform: str, qh: QueueHandler) -> bool:
|
||||
"""Validiert Signatur und legt in Queue ab."""
|
||||
if plattform == "whatsapp":
|
||||
if not verify_whatsapp_signature(payload, signature):
|
||||
return False
|
||||
elif plattform == "telegram":
|
||||
if not verify_telegram_signature():
|
||||
return False
|
||||
else:
|
||||
return False
|
||||
# Submission bauen und enqueuen
|
||||
data = json.loads(payload)
|
||||
submission = Submission(
|
||||
message_id=str(data.get("message_id", "")),
|
||||
plattform=plattform,
|
||||
absender=str(data.get("absender", "")),
|
||||
text=str(data.get("text", "")),
|
||||
)
|
||||
return qh.enqueue(submission) is not None
|
||||
@@ -0,0 +1,29 @@
|
||||
"""Queue-Worker: Pollt Submissions und verarbeitet sie in Batches."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import time
|
||||
|
||||
from src.models.submission import Submission
|
||||
from src.queue.handler import QueueHandler
|
||||
|
||||
|
||||
def verarbeite_submission(submission: Submission) -> bool:
|
||||
"""Verarbeitet eine einzelne Submission (Platzhalter für KI-Pipeline)."""
|
||||
submission.status = "erledigt"
|
||||
return True
|
||||
|
||||
|
||||
def worker_loop(qh: QueueHandler, poll_interval: int = 2, batch_size: int = 5) -> None:
|
||||
"""Endlos-Schleife: Pollt Queue, verarbeitet Batches."""
|
||||
while True:
|
||||
submissions = qh.dequeue(batch_size)
|
||||
if not submissions:
|
||||
time.sleep(poll_interval)
|
||||
continue
|
||||
for submission in submissions:
|
||||
try:
|
||||
if verarbeite_submission(submission):
|
||||
qh.mark_done(submission)
|
||||
except Exception: # noqa: S110
|
||||
pass
|
||||
Reference in New Issue
Block a user