feat: stop extension background work on uninstall or upgrade

This commit is contained in:
Vlad Stan 2023-02-15 15:34:09 +02:00
parent ef3beaaccf
commit 6a5afe3025
2 changed files with 16 additions and 1 deletions

View File

@ -2,6 +2,7 @@ import importlib
import re
from typing import Any
import httpx
from loguru import logger
from lnbits.db import Connection
@ -42,3 +43,12 @@ async def run_migration(db: Connection, migrations_module: Any, current_version:
else:
async with core_db.connect() as conn:
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 with httpx.AsyncClient() as client:
try:
await client.delete(url=f"/{ext_id}/api/v1?usr={user}")
except Exception as ex:
logger.warning(ex)

View File

@ -29,7 +29,7 @@ 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
from lnbits.core.helpers import migrate_extension_database, stop_extension_work
from lnbits.core.models import Payment, User, Wallet
from lnbits.decorators import (
WalletTypeInfo,
@ -755,6 +755,8 @@ async def api_install_extension(
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)
@ -798,6 +800,9 @@ 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)
if ext_id not in settings.lnbits_deactivated_extensions:
settings.lnbits_deactivated_extensions += [ext_id]