mirror of
https://github.com/aljazceru/payments-rest-api.git
synced 2025-12-19 22:44:23 +01:00
Merge pull request #1 from aljazceru/codex/refactor-wait_for_sync-to-use-asyncio.sleep
Make wait_for_sync async
This commit is contained in:
2
main.py
2
main.py
@@ -188,7 +188,7 @@ async def periodic_sync_check():
|
|||||||
|
|
||||||
# Attempt resync with progressively longer timeouts based on consecutive failures
|
# Attempt resync with progressively longer timeouts based on consecutive failures
|
||||||
timeout = min(5 + (_consecutive_sync_failures * 2), 30) # Increase timeout up to 30 seconds
|
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")
|
logger.info("SDK resync successful")
|
||||||
_last_sync_time = time.time()
|
_last_sync_time = time.time()
|
||||||
_consecutive_sync_failures = 0
|
_consecutive_sync_failures = 0
|
||||||
|
|||||||
21
nodeless.py
21
nodeless.py
@@ -47,6 +47,7 @@ import time
|
|||||||
import logging
|
import logging
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
import threading
|
import threading
|
||||||
|
import asyncio
|
||||||
|
|
||||||
# Set up logging
|
# Set up logging
|
||||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
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.")
|
logger.info("Breez SDK connected successfully.")
|
||||||
|
|
||||||
# Shorter sync timeout for initial connection
|
# 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:
|
except Exception as e:
|
||||||
logger.error(f"Failed to connect to Breez SDK: {e}")
|
logger.error(f"Failed to connect to Breez SDK: {e}")
|
||||||
@@ -290,9 +291,9 @@ class PaymentHandler:
|
|||||||
self._initialized = True
|
self._initialized = True
|
||||||
logger.debug("PaymentHandler initialization complete")
|
logger.debug("PaymentHandler initialization complete")
|
||||||
|
|
||||||
def wait_for_sync(self, timeout_seconds: int = 10) -> bool:
|
def _wait_for_sync_blocking(self, timeout_seconds: int = 10) -> bool:
|
||||||
"""Wait for the SDK to sync before proceeding."""
|
"""Blocking helper to wait for the SDK to sync before proceeding."""
|
||||||
logger.debug(f"Waiting for sync (timeout={timeout_seconds}s)")
|
logger.debug(f"Waiting for sync (timeout={timeout_seconds}s) [blocking]")
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
while time.time() - start_time < timeout_seconds:
|
while time.time() - start_time < timeout_seconds:
|
||||||
if self.listener.is_synced():
|
if self.listener.is_synced():
|
||||||
@@ -302,6 +303,18 @@ class PaymentHandler:
|
|||||||
logger.warning("SDK sync timeout")
|
logger.warning("SDK sync timeout")
|
||||||
return False
|
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:
|
def wait_for_payment(self, identifier: str, timeout_seconds: int = 60) -> bool:
|
||||||
"""
|
"""
|
||||||
Wait for payment to complete or timeout for a specific identifier
|
Wait for payment to complete or timeout for a specific identifier
|
||||||
|
|||||||
Reference in New Issue
Block a user