Merge pull request #416 from lnbits/fix_db_lock_fastapi

Fix db connect
This commit is contained in:
calle 2021-11-13 12:51:19 +01:00 committed by GitHub
commit e46e881663
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -85,6 +85,7 @@ async def pay_invoice(
description: str = "", description: str = "",
conn: Optional[Connection] = None, conn: Optional[Connection] = None,
) -> str: ) -> str:
async with (db.reuse_conn(conn) if conn else db.connect()) as conn:
temp_id = f"temp_{urlsafe_short_hash()}" temp_id = f"temp_{urlsafe_short_hash()}"
internal_id = f"internal_{urlsafe_short_hash()}" internal_id = f"internal_{urlsafe_short_hash()}"
@ -146,6 +147,7 @@ async def pay_invoice(
# mark the invoice from the other side as not pending anymore # mark the invoice from the other side as not pending anymore
# so the other side only has access to his new money when we are sure # so the other side only has access to his new money when we are sure
# the payer has enough to deduct from # the payer has enough to deduct from
async with db.connect() as conn:
await update_payment_status( await update_payment_status(
checking_id=internal_checking_id, pending=False, conn=conn checking_id=internal_checking_id, pending=False, conn=conn
) )
@ -159,7 +161,7 @@ async def pay_invoice(
# actually pay the external invoice # actually pay the external invoice
payment: PaymentResponse = await WALLET.pay_invoice(payment_request) payment: PaymentResponse = await WALLET.pay_invoice(payment_request)
if payment.checking_id: if payment.checking_id:
async with (db.reuse_conn(conn) if conn else db.connect()) as conn: async with db.connect() as conn:
await create_payment( await create_payment(
checking_id=payment.checking_id, checking_id=payment.checking_id,
fee=payment.fee_msat, fee=payment.fee_msat,
@ -314,7 +316,8 @@ async def check_invoice_status(
if not payment.pending: if not payment.pending:
return status return status
if payment.is_out and status.failed: if payment.is_out and status.failed:
print(f" - deleting outgoing failed payment {payment.checking_id}: {status}") print(
f" - deleting outgoing failed payment {payment.checking_id}: {status}")
await payment.delete() await payment.delete()
elif not status.pending: elif not status.pending:
print( print(