Skip to content

Commit

Permalink
make stop tasks endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
dni committed Feb 17, 2023
1 parent 3df1e4a commit 4027bf3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
6 changes: 5 additions & 1 deletion __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

from fastapi import APIRouter
from starlette.staticfiles import StaticFiles
from typing import List

from lnbits.db import Database
from lnbits.helpers import template_renderer
from lnbits.tasks import catch_everything_and_restart

db = Database("ext_invoices")


invoices_static_files = [
{
"path": "/invoices/static",
Expand All @@ -26,10 +28,12 @@ def invoices_renderer():

from .tasks import wait_for_paid_invoices

scheduled_tasks: List[asyncio.Task] = []

def invoices_start():
loop = asyncio.get_event_loop()
loop.create_task(catch_everything_and_restart(wait_for_paid_invoices))
task = loop.create_task(catch_everything_and_restart(wait_for_paid_invoices))
scheduled_tasks.append(task)


from .views import * # noqa: F401,F403
Expand Down
15 changes: 13 additions & 2 deletions views_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
from lnbits.core.crud import get_user
from lnbits.core.services import create_invoice
from lnbits.core.views.api import api_payment
from lnbits.decorators import WalletTypeInfo, get_key_type
from lnbits.decorators import WalletTypeInfo, get_key_type, check_admin
from lnbits.utils.exchange_rates import fiat_amount_as_satoshis

from . import invoices_ext
from . import invoices_ext, scheduled_tasks
from .crud import (
create_invoice_internal,
create_invoice_items,
Expand Down Expand Up @@ -131,3 +131,14 @@ async def api_invoices_check_payment(invoice_id: str, payment_hash: str):
logger.error(exc)
return {"paid": False}
return status


@invoices_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 4027bf3

Please sign in to comment.