From 810b68087c090a9216aa0fa48a522a1c059b51d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Mon, 15 Apr 2024 11:31:51 +0200 Subject: [PATCH] fix: properly handle task start/stopping (#17) https://github.com/lnbits/lnbits/issues/2411 --- __init__.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/__init__.py b/__init__.py index 6c6042e..a21005e 100644 --- a/__init__.py +++ b/__init__.py @@ -1,10 +1,11 @@ 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_copilot") @@ -26,7 +27,17 @@ def copilot_renderer(): from .views import * # noqa from .views_api import * # noqa +scheduled_tasks: list[asyncio.Task] = [] + + +def copilot_stop(): + for task in scheduled_tasks: + try: + task.cancel() + except Exception as ex: + logger.warning(ex) + def copilot_start(): - loop = asyncio.get_event_loop() - loop.create_task(catch_everything_and_restart(wait_for_paid_invoices)) + task = create_permanent_unique_task("ext_copilot", wait_for_paid_invoices) + scheduled_tasks.append(task)