mirror of
https://github.com/lnbits/lnbits-legend.git
synced 2025-02-22 14:22:55 +01:00
fix async so now it is a mess that works.
This commit is contained in:
parent
28e6f40bea
commit
90c640b659
2 changed files with 27 additions and 18 deletions
|
@ -1,5 +1,5 @@
|
||||||
import importlib
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import importlib
|
||||||
|
|
||||||
from quart import Quart, g
|
from quart import Quart, g
|
||||||
from quart_cors import cors # type: ignore
|
from quart_cors import cors # type: ignore
|
||||||
|
@ -8,7 +8,7 @@ from secure import SecureHeaders # type: ignore
|
||||||
|
|
||||||
from .commands import db_migrate, handle_assets
|
from .commands import db_migrate, handle_assets
|
||||||
from .core import core_app
|
from .core import core_app
|
||||||
from .db import open_db
|
from .db import open_db, open_ext_db
|
||||||
from .helpers import get_valid_extensions, get_js_vendored, get_css_vendored, url_for_vendored
|
from .helpers import get_valid_extensions, get_js_vendored, get_css_vendored, url_for_vendored
|
||||||
from .proxy_fix import ASGIProxyFix
|
from .proxy_fix import ASGIProxyFix
|
||||||
|
|
||||||
|
@ -43,7 +43,17 @@ def register_blueprints(app: Quart) -> None:
|
||||||
for ext in get_valid_extensions():
|
for ext in get_valid_extensions():
|
||||||
try:
|
try:
|
||||||
ext_module = importlib.import_module(f"lnbits.extensions.{ext.code}")
|
ext_module = importlib.import_module(f"lnbits.extensions.{ext.code}")
|
||||||
app.register_blueprint(getattr(ext_module, f"{ext.code}_ext"), url_prefix=f"/{ext.code}")
|
bp = getattr(ext_module, f"{ext.code}_ext")
|
||||||
|
|
||||||
|
@bp.before_request
|
||||||
|
async def before_request():
|
||||||
|
g.ext_db = open_ext_db(ext.code)
|
||||||
|
|
||||||
|
@bp.teardown_request
|
||||||
|
async def after_request(exc):
|
||||||
|
g.ext_db.__exit__(type(exc), exc, None)
|
||||||
|
|
||||||
|
app.register_blueprint(bp, url_prefix=f"/{ext.code}")
|
||||||
except Exception:
|
except Exception:
|
||||||
raise ImportError(f"Please make sure that the extension `{ext.code}` follows conventions.")
|
raise ImportError(f"Please make sure that the extension `{ext.code}` follows conventions.")
|
||||||
|
|
||||||
|
@ -99,8 +109,8 @@ def register_async_tasks(app):
|
||||||
|
|
||||||
@app.before_serving
|
@app.before_serving
|
||||||
async def listeners():
|
async def listeners():
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_running_loop()
|
||||||
loop.create_task(invoice_listener(app))
|
loop.create_task(invoice_listener())
|
||||||
|
|
||||||
@app.after_serving
|
@app.after_serving
|
||||||
async def stop_listeners():
|
async def stop_listeners():
|
||||||
|
|
|
@ -31,8 +31,8 @@ def run_on_pseudo_request(awaitable: Awaitable):
|
||||||
send_push_promise=lambda x, h: None,
|
send_push_promise=lambda x, h: None,
|
||||||
)
|
)
|
||||||
async with main_app.request_context(fk):
|
async with main_app.request_context(fk):
|
||||||
g.db = open_db()
|
with open_db() as g.db:
|
||||||
await awaitable
|
await awaitable
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
loop.create_task(run(awaitable))
|
loop.create_task(run(awaitable))
|
||||||
|
@ -57,16 +57,15 @@ async def webhook_handler():
|
||||||
return "", HTTPStatus.NO_CONTENT
|
return "", HTTPStatus.NO_CONTENT
|
||||||
|
|
||||||
|
|
||||||
async def invoice_listener(app):
|
async def invoice_listener():
|
||||||
run_on_pseudo_request(_invoice_listener())
|
|
||||||
|
|
||||||
|
|
||||||
async def _invoice_listener():
|
|
||||||
async for checking_id in WALLET.paid_invoices_stream():
|
async for checking_id in WALLET.paid_invoices_stream():
|
||||||
g.db = open_db()
|
run_on_pseudo_request(invoice_callback_dispatcher(checking_id))
|
||||||
payment = get_standalone_payment(checking_id)
|
|
||||||
if payment.is_in:
|
|
||||||
payment.set_pending(False)
|
async def invoice_callback_dispatcher(checking_id: str):
|
||||||
for ext_name, cb in invoice_listeners:
|
payment = get_standalone_payment(checking_id)
|
||||||
g.ext_db = open_ext_db(ext_name)
|
if payment and payment.is_in:
|
||||||
|
payment.set_pending(False)
|
||||||
|
for ext_name, cb in invoice_listeners:
|
||||||
|
with open_ext_db(ext_name) as g.ext_db: # type: ignore
|
||||||
await cb(payment)
|
await cb(payment)
|
||||||
|
|
Loading…
Add table
Reference in a new issue