fix: properly start/stop tasks (#18)

https://github.com/lnbits/lnbits/issues/2411
This commit is contained in:
dni ⚡ 2024-04-17 22:41:59 +02:00 committed by GitHub
commit dbeb3d9050
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 21 deletions

View file

@ -1,16 +1,14 @@
import asyncio import asyncio
from typing import List from loguru import logger
from fastapi import APIRouter from fastapi import APIRouter
from lnbits.db import Database from lnbits.db import Database
from lnbits.helpers import template_renderer from lnbits.helpers import template_renderer
from lnbits.tasks import catch_everything_and_restart from lnbits.tasks import create_permanent_unique_task
db = Database("ext_splitpayments") db = Database("ext_splitpayments")
scheduled_tasks: List[asyncio.Task] = []
splitpayments_static_files = [ splitpayments_static_files = [
{ {
"path": "/splitpayments/static", "path": "/splitpayments/static",
@ -31,7 +29,15 @@ from .views import * # noqa: F401,F403
from .views_api import * # noqa: F401,F403 from .views_api import * # noqa: F401,F403
scheduled_tasks: list[asyncio.Task] = []
def splitpayments_stop():
for task in scheduled_tasks:
try:
task.cancel()
except Exception as ex:
logger.warning(ex)
def splitpayments_start(): def splitpayments_start():
loop = asyncio.get_event_loop() task = create_permanent_unique_task("ext_splitpayments", wait_for_paid_invoices)
task = loop.create_task(catch_everything_and_restart(wait_for_paid_invoices))
scheduled_tasks.append(task) scheduled_tasks.append(task)

View file

@ -6,9 +6,9 @@ from loguru import logger
from starlette.exceptions import HTTPException from starlette.exceptions import HTTPException
from lnbits.core.crud import get_wallet, get_wallet_for_key from lnbits.core.crud import get_wallet, get_wallet_for_key
from lnbits.decorators import WalletTypeInfo, check_admin, require_admin_key from lnbits.decorators import WalletTypeInfo, require_admin_key
from . import scheduled_tasks, splitpayments_ext from . import splitpayments_ext
from .crud import get_targets, set_targets from .crud import get_targets, set_targets
from .models import Target, TargetPutList from .models import Target, TargetPutList
@ -81,16 +81,3 @@ async def api_targets_delete(
source_wallet: WalletTypeInfo = Depends(require_admin_key), source_wallet: WalletTypeInfo = Depends(require_admin_key),
) -> None: ) -> None:
await set_targets(source_wallet.wallet.id, []) await set_targets(source_wallet.wallet.id, [])
# deinit extension invoice listener
@splitpayments_ext.delete(
"/api/v1", status_code=HTTPStatus.OK, dependencies=[Depends(check_admin)]
)
async def api_stop():
for t in scheduled_tasks:
try:
t.cancel()
except Exception as ex:
logger.warning(ex)
return {"success": True}