Startup optimization: nonblocking expiry check (#1943)

* nonblocking expiry check
* autoruff
* smaller interval

---------

Co-authored-by: dni  <office@dnilabs.com>
This commit is contained in:
callebtc
2023-09-25 12:32:01 +02:00
committed by GitHub
parent 7a182244b9
commit 50561a8696

View File

@@ -18,7 +18,6 @@ from lnbits.core.crud import (
get_payments, get_payments,
get_standalone_payment, get_standalone_payment,
) )
from lnbits.core.db import db
from lnbits.core.services import redeem_lnurl_withdraw from lnbits.core.services import redeem_lnurl_withdraw
from lnbits.settings import settings from lnbits.settings import settings
from lnbits.wallets import get_wallet_class from lnbits.wallets import get_wallet_class
@@ -149,37 +148,36 @@ async def check_pending_payments():
incoming = True incoming = True
while True: while True:
async with db.connect() as conn: logger.info(
logger.info( f"Task: checking all pending payments (incoming={incoming},"
f"Task: checking all pending payments (incoming={incoming}," f" outgoing={outgoing}) of last 15 days"
f" outgoing={outgoing}) of last 15 days" )
) start_time = time.time()
start_time = time.time() pending_payments = await get_payments(
pending_payments = await get_payments( since=(int(time.time()) - 60 * 60 * 24 * 15), # 15 days ago
since=(int(time.time()) - 60 * 60 * 24 * 15), # 15 days ago complete=False,
complete=False, pending=True,
pending=True, outgoing=outgoing,
outgoing=outgoing, incoming=incoming,
incoming=incoming, exclude_uncheckable=True,
exclude_uncheckable=True, )
conn=conn, for payment in pending_payments:
) await payment.check_status()
for payment in pending_payments: await asyncio.sleep(0.01) # to avoid complete blocking
await payment.check_status(conn=conn)
logger.info(
f"Task: pending check finished for {len(pending_payments)} payments"
f" (took {time.time() - start_time:0.3f} s)"
)
# we delete expired invoices once upon the first pending check
if incoming:
logger.debug("Task: deleting all expired invoices")
start_time = time.time()
await delete_expired_invoices()
logger.info( logger.info(
f"Task: pending check finished for {len(pending_payments)} payments" "Task: expired invoice deletion finished (took"
f" (took {time.time() - start_time:0.3f} s)" f" {time.time() - start_time:0.3f} s)"
) )
# we delete expired invoices once upon the first pending check
if incoming:
logger.debug("Task: deleting all expired invoices")
start_time = time.time()
await delete_expired_invoices(conn=conn)
logger.info(
"Task: expired invoice deletion finished (took"
f" {time.time() - start_time:0.3f} s)"
)
# after the first check we will only check outgoing, not incoming # after the first check we will only check outgoing, not incoming
# that will be handled by the global invoice listeners, hopefully # that will be handled by the global invoice listeners, hopefully