Skip to content

Commit

Permalink
fix: properly start/stop tasks (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
dni authored Apr 17, 2024
1 parent 9f909d4 commit 5da2c87
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
18 changes: 12 additions & 6 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import asyncio
from typing import List
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_scrub")

scheduled_tasks: List[asyncio.Task] = []

scrub_static_files = [
{
"path": "/scrub/static",
Expand All @@ -30,7 +28,15 @@ def scrub_renderer():
from .views_api import * # noqa: F401,F403


scheduled_tasks: list[asyncio.Task] = []

def scrub_stop():
for task in scheduled_tasks:
try:
task.cancel()
except Exception as ex:
logger.warning(ex)

def scrub_start():
loop = asyncio.get_event_loop()
task = loop.create_task(catch_everything_and_restart(wait_for_paid_invoices))
task = create_permanent_unique_task("ext_scrub", wait_for_paid_invoices)
scheduled_tasks.append(task)
13 changes: 1 addition & 12 deletions views_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
require_admin_key,
)

from . import scheduled_tasks, scrub_ext
from . import scrub_ext
from .crud import (
create_scrub_link,
delete_scrub_link,
Expand Down Expand Up @@ -111,14 +111,3 @@ async def api_link_delete(link_id, wallet: WalletTypeInfo = Depends(require_admi

await delete_scrub_link(link_id)
return "", HTTPStatus.NO_CONTENT


@scrub_ext.delete("/api/v1", status_code=HTTPStatus.OK, dependencies=[Depends(check_admin)])
async def api_stop():
for t in scheduled_tasks:
try:
t.cancel()
except Exception as ex:
logger.warning(ex)

return {"success": True}

0 comments on commit 5da2c87

Please sign in to comment.