Skip to content

Commit 5da2c87

Browse files
authored
fix: properly start/stop tasks (#8)
lnbits/lnbits#2411
1 parent 9f909d4 commit 5da2c87

File tree

2 files changed

+13
-18
lines changed

2 files changed

+13
-18
lines changed

__init__.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import asyncio
2-
from typing import List
2+
from loguru import logger
33

44
from fastapi import APIRouter
55

66
from lnbits.db import Database
77
from lnbits.helpers import template_renderer
8-
from lnbits.tasks import catch_everything_and_restart
8+
from lnbits.tasks import create_permanent_unique_task
99

1010
db = Database("ext_scrub")
1111

12-
scheduled_tasks: List[asyncio.Task] = []
13-
1412
scrub_static_files = [
1513
{
1614
"path": "/scrub/static",
@@ -30,7 +28,15 @@ def scrub_renderer():
3028
from .views_api import * # noqa: F401,F403
3129

3230

31+
scheduled_tasks: list[asyncio.Task] = []
32+
33+
def scrub_stop():
34+
for task in scheduled_tasks:
35+
try:
36+
task.cancel()
37+
except Exception as ex:
38+
logger.warning(ex)
39+
3340
def scrub_start():
34-
loop = asyncio.get_event_loop()
35-
task = loop.create_task(catch_everything_and_restart(wait_for_paid_invoices))
41+
task = create_permanent_unique_task("ext_scrub", wait_for_paid_invoices)
3642
scheduled_tasks.append(task)

views_api.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
require_admin_key,
1313
)
1414

15-
from . import scheduled_tasks, scrub_ext
15+
from . import scrub_ext
1616
from .crud import (
1717
create_scrub_link,
1818
delete_scrub_link,
@@ -111,14 +111,3 @@ async def api_link_delete(link_id, wallet: WalletTypeInfo = Depends(require_admi
111111

112112
await delete_scrub_link(link_id)
113113
return "", HTTPStatus.NO_CONTENT
114-
115-
116-
@scrub_ext.delete("/api/v1", status_code=HTTPStatus.OK, dependencies=[Depends(check_admin)])
117-
async def api_stop():
118-
for t in scheduled_tasks:
119-
try:
120-
t.cancel()
121-
except Exception as ex:
122-
logger.warning(ex)
123-
124-
return {"success": True}

0 commit comments

Comments
 (0)