nostrmarket/tasks.py
2025-09-03 10:54:07 +02:00

51 lines
1.6 KiB
Python

import asyncio
from asyncio import Queue
from lnbits.core.models import Payment
from lnbits.tasks import register_invoice_listener
from loguru import logger
from .nostr.nostr_client import NostrClient
from .services import (
handle_order_paid,
process_nostr_message,
subscribe_to_all_merchants,
)
async def wait_for_paid_invoices():
invoice_queue = Queue()
register_invoice_listener(invoice_queue)
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") != "nostrmarket":
return
order_id = payment.extra.get("order_id")
merchant_pubkey = payment.extra.get("merchant_pubkey")
if not order_id or not merchant_pubkey:
return None
await handle_order_paid(order_id, merchant_pubkey)
async def wait_for_nostr_events(nostr_client: NostrClient):
logger.info("[NOSTRMARKET DEBUG] Starting wait_for_nostr_events task")
while True:
try:
logger.info("[NOSTRMARKET DEBUG] Subscribing to all merchants...")
await subscribe_to_all_merchants()
while True:
logger.debug("[NOSTRMARKET DEBUG] Waiting for nostr event...")
message = await nostr_client.get_event()
logger.info(f"[NOSTRMARKET DEBUG] Received event from nostr_client: {message[:100]}...")
await process_nostr_message(message)
except Exception as e:
logger.warning(f"[NOSTRMARKET DEBUG] Subscription failed. Will retry in 10 seconds: {e}")
await asyncio.sleep(10)