Skip to content

Commit 810b680

Browse files
authored
fix: properly handle task start/stopping (#17)
lnbits/lnbits#2411
1 parent ddcf6f9 commit 810b680

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

__init__.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import asyncio
2+
from loguru import logger
23

34
from fastapi import APIRouter
45

56
from lnbits.db import Database
67
from lnbits.helpers import template_renderer
7-
from lnbits.tasks import catch_everything_and_restart
8+
from lnbits.tasks import create_permanent_unique_task
89

910
db = Database("ext_copilot")
1011

@@ -26,7 +27,17 @@ def copilot_renderer():
2627
from .views import * # noqa
2728
from .views_api import * # noqa
2829

30+
scheduled_tasks: list[asyncio.Task] = []
31+
32+
33+
def copilot_stop():
34+
for task in scheduled_tasks:
35+
try:
36+
task.cancel()
37+
except Exception as ex:
38+
logger.warning(ex)
39+
2940

3041
def copilot_start():
31-
loop = asyncio.get_event_loop()
32-
loop.create_task(catch_everything_and_restart(wait_for_paid_invoices))
42+
task = create_permanent_unique_task("ext_copilot", wait_for_paid_invoices)
43+
scheduled_tasks.append(task)

0 commit comments

Comments
 (0)