diff --git a/__init__.py b/__init__.py index 5e1e46d..3d90cee 100644 --- a/__init__.py +++ b/__init__.py @@ -1,12 +1,13 @@ import asyncio -from asyncio import Task -from typing import List from fastapi import APIRouter +from loguru import logger from lnbits.db import Database from lnbits.helpers import template_renderer -from lnbits.tasks import catch_everything_and_restart +from lnbits.tasks import create_permanent_unique_task + +from .nostr.nostr_client import NostrClient db = Database("ext_nostrmarket") @@ -23,12 +24,7 @@ nostrmarket_static_files = [ def nostrmarket_renderer(): return template_renderer(["nostrmarket/templates"]) - -from .nostr.nostr_client import NostrClient - -nostr_client = NostrClient() - -scheduled_tasks: List[Task] = [] +nostr_client: NostrClient = NostrClient() from .tasks import wait_for_nostr_events, wait_for_paid_invoices @@ -36,7 +32,21 @@ from .views import * # noqa from .views_api import * # noqa +scheduled_tasks: list[asyncio.Task] = [] + + +async def nostrmarket_stop(): + for task in scheduled_tasks: + try: + task.cancel() + except Exception as ex: + logger.warning(ex) + + await nostr_client.stop() + + def nostrmarket_start(): + async def _subscribe_to_nostr_client(): # wait for 'nostrclient' extension to initialize await asyncio.sleep(10) @@ -47,8 +57,7 @@ def nostrmarket_start(): await asyncio.sleep(15) await wait_for_nostr_events(nostr_client) - loop = asyncio.get_event_loop() - task1 = loop.create_task(catch_everything_and_restart(wait_for_paid_invoices)) - task2 = loop.create_task(catch_everything_and_restart(_subscribe_to_nostr_client)) - task3 = loop.create_task(catch_everything_and_restart(_wait_for_nostr_events)) + task1 = create_permanent_unique_task("ext_nostrmarket_paid_invoices", wait_for_paid_invoices) + task2 = create_permanent_unique_task("ext_nostrmarket_subscribe_to_nostr_client", _subscribe_to_nostr_client) + task3 = create_permanent_unique_task("ext_nostrmarket_wait_for_events", _wait_for_nostr_events) scheduled_tasks.extend([task1, task2, task3]) diff --git a/views_api.py b/views_api.py index ceab28c..9f77217 100644 --- a/views_api.py +++ b/views_api.py @@ -9,14 +9,13 @@ from loguru import logger from lnbits.core.services import websocket_updater from lnbits.decorators import ( WalletTypeInfo, - check_admin, get_key_type, require_admin_key, require_invoice_key, ) from lnbits.utils.exchange_rates import currencies -from . import nostr_client, nostrmarket_ext, scheduled_tasks +from . import nostr_client, nostrmarket_ext from .crud import ( create_customer, create_direct_message, @@ -1116,19 +1115,3 @@ async def restart_nostr_client(wallet: WalletTypeInfo = Depends(require_admin_ke await nostr_client.restart() except Exception as ex: logger.warning(ex) - - -@nostrmarket_ext.delete("/api/v1", status_code=HTTPStatus.OK) -async def api_stop(wallet: WalletTypeInfo = Depends(check_admin)): - for t in scheduled_tasks: - try: - t.cancel() - except Exception as ex: - logger.warning(ex) - - try: - await nostr_client.stop() - except Exception as ex: - logger.warning(ex) - - return {"success": True}