Make wait_for_sync async

This commit is contained in:
2025-06-17 15:15:27 +02:00
parent a20fbdb295
commit 6455415edd
2 changed files with 18 additions and 5 deletions

View File

@@ -188,7 +188,7 @@ async def periodic_sync_check():
# Attempt resync with progressively longer timeouts based on consecutive failures
timeout = min(5 + (_consecutive_sync_failures * 2), 30) # Increase timeout up to 30 seconds
if _payment_handler.wait_for_sync(timeout_seconds=timeout):
if await _payment_handler.wait_for_sync(timeout_seconds=timeout):
logger.info("SDK resync successful")
_last_sync_time = time.time()
_consecutive_sync_failures = 0

View File

@@ -47,6 +47,7 @@ import time
import logging
from pprint import pprint
import threading
import asyncio
# Set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
@@ -281,7 +282,7 @@ class PaymentHandler:
logger.info("Breez SDK connected successfully.")
# Shorter sync timeout for initial connection
self.wait_for_sync(timeout_seconds=10)
self._wait_for_sync_blocking(timeout_seconds=10)
except Exception as e:
logger.error(f"Failed to connect to Breez SDK: {e}")
@@ -290,9 +291,9 @@ class PaymentHandler:
self._initialized = True
logger.debug("PaymentHandler initialization complete")
def wait_for_sync(self, timeout_seconds: int = 10) -> bool:
"""Wait for the SDK to sync before proceeding."""
logger.debug(f"Waiting for sync (timeout={timeout_seconds}s)")
def _wait_for_sync_blocking(self, timeout_seconds: int = 10) -> bool:
"""Blocking helper to wait for the SDK to sync before proceeding."""
logger.debug(f"Waiting for sync (timeout={timeout_seconds}s) [blocking]")
start_time = time.time()
while time.time() - start_time < timeout_seconds:
if self.listener.is_synced():
@@ -302,6 +303,18 @@ class PaymentHandler:
logger.warning("SDK sync timeout")
return False
async def wait_for_sync(self, timeout_seconds: int = 10) -> bool:
"""Asynchronously wait for the SDK to sync before proceeding."""
logger.debug(f"Waiting for sync (timeout={timeout_seconds}s) [async]")
start_time = time.time()
while time.time() - start_time < timeout_seconds:
if self.listener.is_synced():
logger.debug("SDK synced successfully")
return True
await asyncio.sleep(0.1)
logger.warning("SDK sync timeout")
return False
def wait_for_payment(self, identifier: str, timeout_seconds: int = 60) -> bool:
"""
Wait for payment to complete or timeout for a specific identifier