-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.py
More file actions
49 lines (37 loc) · 1.52 KB
/
Copy pathtasks.py
File metadata and controls
49 lines (37 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import asyncio
from datetime import datetime, timedelta, timezone
from lnbits.core.models import Payment
from lnbits.settings import settings
from lnbits.tasks import register_invoice_listener
from loguru import logger
from .crud import delete_empty_chats_before
from .services import payment_received_for_client_data, process_due_notification_jobs
async def wait_for_paid_invoices():
invoice_queue = asyncio.Queue()
register_invoice_listener(invoice_queue, "ext_chat")
while True:
payment = await invoice_queue.get()
await on_invoice_paid(payment)
async def on_invoice_paid(payment: Payment) -> None:
if payment.extra.get("tag") != "chat":
return
logger.info(f"Invoice paid for chat: {payment.payment_hash}")
try:
await payment_received_for_client_data(payment)
except Exception as e:
logger.error(f"Error processing payment for chat: {e}")
async def cleanup_empty_chats() -> None:
while settings.lnbits_running:
cutoff = int((datetime.now(timezone.utc) - timedelta(minutes=20)).timestamp())
try:
await delete_empty_chats_before(cutoff)
except Exception as e:
logger.warning(f"Error cleaning empty chats: {e}")
await asyncio.sleep(60)
async def dispatch_notification_jobs() -> None:
while settings.lnbits_running:
try:
await process_due_notification_jobs()
except Exception as e:
logger.warning(f"Error processing chat notification jobs: {e}")
await asyncio.sleep(30)