From 825e603a110c75a192732e02c3c3d336bc025aa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Mon, 15 Apr 2024 10:32:47 +0200 Subject: [PATCH] fix: properly start and stop tasks (#31) --- __init__.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/__init__.py b/__init__.py index 17c4033..c73a19a 100644 --- a/__init__.py +++ b/__init__.py @@ -1,10 +1,10 @@ import asyncio +from loguru import logger from fastapi import APIRouter - 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 db = Database("ext_boltcards") @@ -25,10 +25,20 @@ def boltcards_renderer(): from .lnurl import * # noqa: F401,F403 from .tasks import * # noqa: F401,F403 +scheduled_tasks: list[asyncio.Task] = [] + + +def boltcards_stop(): + for task in scheduled_tasks: + try: + task.cancel() + except Exception as ex: + logger.warning(ex) + def boltcards_start(): - loop = asyncio.get_event_loop() - loop.create_task(catch_everything_and_restart(wait_for_paid_invoices)) # noqa: F405 + task = create_permanent_unique_task(wait_for_paid_invoices) + scheduled_tasks.append(task) from .views import * # noqa: F401,F403