Skip to content

Commit

Permalink
fix: properly start/stop tasks (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
dni authored Jun 26, 2024
1 parent 94d383b commit 2812118
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 29 deletions.
26 changes: 20 additions & 6 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import asyncio
from typing import List

from fastapi import APIRouter
from loguru import logger

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
from .relay.client_manager import NostrClientManager

db = Database("ext_nostrrelay")

nostrrelay_ext: APIRouter = APIRouter(prefix="/nostrrelay", tags=["NostrRelay"])

client_manager: NostrClientManager = NostrClientManager()

nostrrelay_static_files = [
{
"path": "/nostrrelay/static",
Expand All @@ -26,8 +29,6 @@
}
]

scheduled_tasks: List[asyncio.Task] = []


def nostrrelay_renderer():
return template_renderer(["nostrrelay/templates"])
Expand All @@ -38,7 +39,20 @@ def nostrrelay_renderer():
from .views_api import * # noqa


scheduled_tasks: list[asyncio.Task] = []

async def nostrrelay_stop():
for task in scheduled_tasks:
try:
task.cancel()
except Exception as ex:
logger.warning(ex)
try:
await client_manager.stop()
except Exception as ex:
logger.warning(ex)


def nostrrelay_start():
loop = asyncio.get_event_loop()
task = loop.create_task(catch_everything_and_restart(wait_for_paid_invoices))
task = create_permanent_unique_task("ext_nostrrelay", wait_for_paid_invoices)
scheduled_tasks.append(task)
25 changes: 2 additions & 23 deletions views_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@
from fastapi import Depends, Request, WebSocket
from fastapi.exceptions import HTTPException
from loguru import logger
from pydantic.types import UUID4
from starlette.responses import JSONResponse

from lnbits.core.crud import get_user
from lnbits.core.services import create_invoice
from lnbits.decorators import (
WalletTypeInfo,
check_admin,
require_admin_key,
require_invoice_key,
)
from lnbits.helpers import urlsafe_short_hash

from . import nostrrelay_ext, scheduled_tasks
from . import nostrrelay_ext, client_manager
from .crud import (
create_account,
create_relay,
Expand All @@ -34,11 +31,9 @@
)
from .helpers import extract_domain, normalize_public_key, relay_info_response
from .models import BuyOrder, NostrAccount, NostrPartialAccount
from .relay.client_manager import NostrClientConnection, NostrClientManager
from .relay.client_manager import NostrClientConnection
from .relay.relay import NostrRelay

client_manager = NostrClientManager()


@nostrrelay_ext.websocket("/{relay_id}")
async def websocket_endpoint(relay_id: str, websocket: WebSocket):
Expand Down Expand Up @@ -360,19 +355,3 @@ async def api_pay_to_join(data: BuyOrder):
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
detail="Cannot create invoice for client to join",
)


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

try:
await client_manager.stop()
except Exception as ex:
logger.warning(ex)

return {"success": True}

0 comments on commit 2812118

Please sign in to comment.