From 38e5eeece02f71c5a78c812e0f69e244bc19c841 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Mon, 17 Apr 2023 14:39:27 +0200 Subject: [PATCH 1/2] add the main tasks to scheduled_tasks and make dem deinitilize on api_stop --- __init__.py | 9 +++++++-- views_api.py | 8 +++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/__init__.py b/__init__.py index 60d8e23..5a251b0 100644 --- a/__init__.py +++ b/__init__.py @@ -1,3 +1,4 @@ +from typing import List from fastapi import APIRouter from starlette.staticfiles import StaticFiles @@ -17,6 +18,8 @@ nostrclient_static_files = [ nostrclient_ext: APIRouter = APIRouter(prefix="/nostrclient", tags=["nostrclient"]) +scheduled_tasks: List[asyncio.Task] = [] + def nostr_renderer(): return template_renderer(["lnbits/extensions/nostrclient/templates"]) @@ -29,5 +32,7 @@ from .views_api import * # noqa def nostrclient_start(): loop = asyncio.get_event_loop() - loop.create_task(catch_everything_and_restart(init_relays)) - loop.create_task(catch_everything_and_restart(subscribe_events)) + task1 = loop.create_task(catch_everything_and_restart(init_relays)) + scheduled_tasks.append(task1) + task2 = loop.create_task(catch_everything_and_restart(subscribe_events)) + scheduled_tasks.append(task2) diff --git a/views_api.py b/views_api.py index 15cc3ab..c0be01e 100644 --- a/views_api.py +++ b/views_api.py @@ -9,7 +9,7 @@ from starlette.exceptions import HTTPException from lnbits.decorators import check_admin from lnbits.helpers import urlsafe_short_hash -from . import nostrclient_ext +from . import nostrclient_ext, scheduled_tasks from .crud import add_relay, delete_relay, get_relays from .models import Relay, RelayList from .services import NostrRouter, nostr @@ -92,6 +92,12 @@ async def api_stop(): except Exception as e: logger.error(e) + for scheduled_task in scheduled_tasks: + try: + scheduled_task.cancel() + except Exception as ex: + logger.warning(ex) + return {"success": True} From 6500e5c9bb2df62468ee7c099294eb1edff8fbb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Mon, 17 Apr 2023 14:42:14 +0200 Subject: [PATCH 2/2] forgot asyncio --- __init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/__init__.py b/__init__.py index 5a251b0..b09d0e9 100644 --- a/__init__.py +++ b/__init__.py @@ -1,3 +1,4 @@ +import asyncio from typing import List from fastapi import APIRouter from starlette.staticfiles import StaticFiles