From e54909701d015721be28b7acd97546608ffdf640 Mon Sep 17 00:00:00 2001 From: Sat <792024+santyr@users.noreply.github.com> Date: Tue, 16 Jul 2024 07:50:06 -0600 Subject: [PATCH] Handle hodl invoices asynchronously tasks.py - Modified the on_invoice_paid function to call pay_invoice asynchronously using asyncio.create_task. - Added a new function pay_invoice_in_background to handle the asynchronous payment processing and exception handling. - Ensured that the main event loop remains responsive even when encountering hodl invoices by running pay_invoice in the background. --- tasks.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/tasks.py b/tasks.py index 13b1659..2d924b0 100644 --- a/tasks.py +++ b/tasks.py @@ -45,15 +45,13 @@ async def on_invoice_paid(payment: Payment) -> None: logger.trace(f"splitpayments: performing split payments to {len(targets)} targets") for target in targets: - if target.percent > 0: - amount_msat = int(payment.amount * target.percent / 100) memo = ( f"Split payment: {target.percent}% for {target.alias or target.wallet}" ) - if target.wallet.find("@") >= 0 or target.wallet.find("LNURL") >= 0: + if "@" in target.wallet or "LNURL" in target.wallet: safe_amount_msat = amount_msat - fee_reserve(amount_msat) payment_request = await get_lnurl_invoice( target.wallet, payment.wallet_id, safe_amount_msat, memo @@ -69,12 +67,23 @@ async def on_invoice_paid(payment: Payment) -> None: extra = {**payment.extra, "tag": "splitpayments", "splitted": True} if payment_request: - await pay_invoice( + asyncio.create_task(pay_invoice_in_background( payment_request=payment_request, wallet_id=payment.wallet_id, description=memo, - extra=extra, - ) + extra=extra + )) + +async def pay_invoice_in_background(payment_request, wallet_id, description, extra): + try: + await pay_invoice( + payment_request=payment_request, + wallet_id=wallet_id, + description=description, + extra=extra, + ) + except Exception as e: + logger.error(f"Failed to pay invoice: {e}") async def get_lnurl_invoice(