fix: extension DELETE call

This commit is contained in:
Vlad Stan 2023-02-15 17:25:58 +02:00
parent 6540cd9d76
commit e3ac700005
2 changed files with 22 additions and 8 deletions

View File

@ -7,6 +7,7 @@ from loguru import logger
from lnbits.db import Connection
from lnbits.extension_manager import Extension
from lnbits.settings import settings
from . import db as core_db
from .crud import update_migration_version
@ -45,10 +46,19 @@ async def run_migration(db: Connection, migrations_module: Any, current_version:
await update_migration_version(conn, db_name, version)
async def stop_extension_work(ext_id: str, user: str):
"""Stop background workk for extension (like asyncio.Tasks, WebSockets, etc)"""
async def stop_extension_background_work(ext_id: str, user: str):
"""
Stop background workk for extension (like asyncio.Tasks, WebSockets, etc)
It tries first to call the endpoint using `http` and if ti fails it tries using `https`
"""
async with httpx.AsyncClient() as client:
try:
await client.delete(url=f"/{ext_id}/api/v1?usr={user}")
url = f"http://{settings.host}:{settings.port}/{ext_id}/api/v1?usr={user}"
await client.delete(url)
except Exception as ex:
logger.warning(ex)
try:
# try https
url = f"https://{settings.host}:{settings.port}/{ext_id}/api/v1?usr={user}"
except Exception as ex:
logger.warning(ex)

View File

@ -29,7 +29,10 @@ from sse_starlette.sse import EventSourceResponse
from starlette.responses import RedirectResponse, StreamingResponse
from lnbits import bolt11, lnurl
from lnbits.core.helpers import migrate_extension_database, stop_extension_work
from lnbits.core.helpers import (
migrate_extension_database,
stop_extension_background_work,
)
from lnbits.core.models import Payment, User, Wallet
from lnbits.decorators import (
WalletTypeInfo,
@ -729,7 +732,6 @@ async def websocket_update_get(item_id: str, data: str):
async def api_install_extension(
data: CreateExtension, user: User = Depends(check_admin)
):
release = await InstallableExtension.get_extension_release(
data.ext_id, data.source_repo, data.archive
)
@ -752,11 +754,13 @@ async def api_install_extension(
await migrate_extension_database(extension, db_version)
await add_installed_extension(ext_info)
# call stop while the old routes are still active
await stop_extension_background_work(data.ext_id, user.id)
if data.ext_id not in settings.lnbits_deactivated_extensions:
settings.lnbits_deactivated_extensions += [data.ext_id]
# call stop while the old routes are still active
await stop_extension_work(data.ext_id, settings.super_user)
# mount routes for the new version
core_app_extra.register_new_ext_routes(extension)
@ -801,7 +805,7 @@ async def api_uninstall_extension(ext_id: str, user: User = Depends(check_admin)
try:
# call stop while the old routes are still active
await stop_extension_work(ext_id, settings.super_user)
await stop_extension_background_work(ext_id, user.id)
if ext_id not in settings.lnbits_deactivated_extensions:
settings.lnbits_deactivated_extensions += [ext_id]