reuse db connection for pending and expired tasks (#951)

* reuse db connection for pending and expired tasks

* make format
This commit is contained in:
calle
2022-09-09 15:02:07 +03:00
committed by GitHub
parent 7d0b5ade3e
commit 54305c8f91
3 changed files with 39 additions and 23 deletions

View File

@@ -1,4 +1,5 @@
import secrets
from datetime import date, datetime
from typing import List, Optional, Union
from lnbits.helpers import urlsafe_short_hash
@@ -6,7 +7,6 @@ from lnbits.helpers import urlsafe_short_hash
from . import db
from .models import Card, CreateCardData, Hit, Refund
from datetime import date, datetime
async def create_card(data: CreateCardData, wallet_id: str) -> Card:
card_id = urlsafe_short_hash().upper()
@@ -181,7 +181,8 @@ async def get_hits(cards_ids: Union[str, List[str]]) -> List[Hit]:
async def get_hits_today(card_id: str) -> Optional[Hit]:
rows = await db.fetchall(
f"SELECT * FROM boltcards.hits WHERE card_id = ?", (card_id,),
f"SELECT * FROM boltcards.hits WHERE card_id = ?",
(card_id,),
)
updatedrow = []
for row in rows:

View File

@@ -21,7 +21,6 @@ from starlette.requests import Request
from starlette.responses import HTMLResponse
from lnbits import bolt11
from lnbits.core.services import create_invoice
from lnbits.core.views.api import pay_invoice
@@ -98,6 +97,7 @@ async def api_scan(p, c, request: Request, external_id: str = None):
"defaultDescription": f"Boltcard (refund address lnurl://{lnurlpay})",
}
@boltcards_ext.get(
"/api/v1/lnurl/cb/{hitid}",
status_code=HTTPStatus.OK,
@@ -129,6 +129,7 @@ async def lnurl_callback(
except:
return {"status": "ERROR", "reason": f"Payment failed"}
# /boltcards/api/v1/auth?a=00000000000000000000000000000000
@boltcards_ext.get("/api/v1/auth")
async def api_auth(a, request: Request):
@@ -159,11 +160,12 @@ async def api_auth(a, request: Request):
"k4": card.k2,
"lnurlw_base": "lnurlw://" + lnurlw_base,
"protocol_name": "new_bolt_card_response",
"protocol_version": 1
"protocol_version": 1,
}
return response
###############LNURLPAY REFUNDS#################

View File

@@ -16,6 +16,8 @@ from lnbits.core.crud import (
from lnbits.core.services import redeem_lnurl_withdraw
from lnbits.settings import WALLET
from .core import db
deferred_async: List[Callable] = []
@@ -86,24 +88,35 @@ async def check_pending_payments():
incoming = True
while True:
logger.debug(
f"Task: checking all pending payments (incoming={incoming}, outgoing={outgoing}) of last 15 days"
)
for payment in await get_payments(
since=(int(time.time()) - 60 * 60 * 24 * 15), # 15 days ago
complete=False,
pending=True,
outgoing=outgoing,
incoming=incoming,
exclude_uncheckable=True,
):
await payment.check_status()
logger.debug("Task: pending payments check finished")
# we delete expired invoices once upon the first pending check
if incoming:
logger.debug("Task: deleting all expired invoices")
await delete_expired_invoices()
logger.debug("Task: expired invoice deletion finished")
async with db.connect() as conn:
logger.debug(
f"Task: checking all pending payments (incoming={incoming}, outgoing={outgoing}) of last 15 days"
)
start_time: float = time.time()
pending_payments = await get_payments(
since=(int(time.time()) - 60 * 60 * 24 * 15), # 15 days ago
complete=False,
pending=True,
outgoing=outgoing,
incoming=incoming,
exclude_uncheckable=True,
conn=conn,
)
logger.debug(f"Task: checking {len(pending_payments)} pending payments")
for payment in pending_payments:
await payment.check_status()
logger.debug(
f"Task: pending payments check finished (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: float = time.time()
await delete_expired_invoices(conn=conn)
logger.debug(
f"Task: expired invoice deletion finished (took {time.time() - start_time:0.3f} s)"
)
# after the first check we will only check outgoing, not incoming
# that will be handled by the global invoice listeners, hopefully