fix: properly start/stop tasks (#22)
https://github.com/lnbits/nostrclient/pull/28
This commit is contained in:
parent
94d383baff
commit
28121184c3
2 changed files with 22 additions and 29 deletions
26
__init__.py
26
__init__.py
|
|
@ -1,16 +1,19 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
from typing import List
|
|
||||||
|
|
||||||
from fastapi import APIRouter
|
from fastapi import APIRouter
|
||||||
|
from loguru import logger
|
||||||
|
|
||||||
from lnbits.db import Database
|
from lnbits.db import Database
|
||||||
from lnbits.helpers import template_renderer
|
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")
|
db = Database("ext_nostrrelay")
|
||||||
|
|
||||||
nostrrelay_ext: APIRouter = APIRouter(prefix="/nostrrelay", tags=["NostrRelay"])
|
nostrrelay_ext: APIRouter = APIRouter(prefix="/nostrrelay", tags=["NostrRelay"])
|
||||||
|
|
||||||
|
client_manager: NostrClientManager = NostrClientManager()
|
||||||
|
|
||||||
nostrrelay_static_files = [
|
nostrrelay_static_files = [
|
||||||
{
|
{
|
||||||
"path": "/nostrrelay/static",
|
"path": "/nostrrelay/static",
|
||||||
|
|
@ -26,8 +29,6 @@ nostrrelay_redirect_paths = [
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
scheduled_tasks: List[asyncio.Task] = []
|
|
||||||
|
|
||||||
|
|
||||||
def nostrrelay_renderer():
|
def nostrrelay_renderer():
|
||||||
return template_renderer(["nostrrelay/templates"])
|
return template_renderer(["nostrrelay/templates"])
|
||||||
|
|
@ -38,7 +39,20 @@ from .views import * # noqa
|
||||||
from .views_api import * # noqa
|
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():
|
def nostrrelay_start():
|
||||||
loop = asyncio.get_event_loop()
|
task = create_permanent_unique_task("ext_nostrrelay", wait_for_paid_invoices)
|
||||||
task = loop.create_task(catch_everything_and_restart(wait_for_paid_invoices))
|
|
||||||
scheduled_tasks.append(task)
|
scheduled_tasks.append(task)
|
||||||
|
|
|
||||||
25
views_api.py
25
views_api.py
|
|
@ -4,20 +4,17 @@ from typing import List, Optional
|
||||||
from fastapi import Depends, Request, WebSocket
|
from fastapi import Depends, Request, WebSocket
|
||||||
from fastapi.exceptions import HTTPException
|
from fastapi.exceptions import HTTPException
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
from pydantic.types import UUID4
|
|
||||||
from starlette.responses import JSONResponse
|
from starlette.responses import JSONResponse
|
||||||
|
|
||||||
from lnbits.core.crud import get_user
|
from lnbits.core.crud import get_user
|
||||||
from lnbits.core.services import create_invoice
|
from lnbits.core.services import create_invoice
|
||||||
from lnbits.decorators import (
|
from lnbits.decorators import (
|
||||||
WalletTypeInfo,
|
WalletTypeInfo,
|
||||||
check_admin,
|
|
||||||
require_admin_key,
|
require_admin_key,
|
||||||
require_invoice_key,
|
require_invoice_key,
|
||||||
)
|
)
|
||||||
from lnbits.helpers import urlsafe_short_hash
|
from lnbits.helpers import urlsafe_short_hash
|
||||||
|
from . import nostrrelay_ext, client_manager
|
||||||
from . import nostrrelay_ext, scheduled_tasks
|
|
||||||
from .crud import (
|
from .crud import (
|
||||||
create_account,
|
create_account,
|
||||||
create_relay,
|
create_relay,
|
||||||
|
|
@ -34,11 +31,9 @@ from .crud import (
|
||||||
)
|
)
|
||||||
from .helpers import extract_domain, normalize_public_key, relay_info_response
|
from .helpers import extract_domain, normalize_public_key, relay_info_response
|
||||||
from .models import BuyOrder, NostrAccount, NostrPartialAccount
|
from .models import BuyOrder, NostrAccount, NostrPartialAccount
|
||||||
from .relay.client_manager import NostrClientConnection, NostrClientManager
|
from .relay.client_manager import NostrClientConnection
|
||||||
from .relay.relay import NostrRelay
|
from .relay.relay import NostrRelay
|
||||||
|
|
||||||
client_manager = NostrClientManager()
|
|
||||||
|
|
||||||
|
|
||||||
@nostrrelay_ext.websocket("/{relay_id}")
|
@nostrrelay_ext.websocket("/{relay_id}")
|
||||||
async def websocket_endpoint(relay_id: str, websocket: WebSocket):
|
async def websocket_endpoint(relay_id: str, websocket: WebSocket):
|
||||||
|
|
@ -360,19 +355,3 @@ async def api_pay_to_join(data: BuyOrder):
|
||||||
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
|
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
|
||||||
detail="Cannot create invoice for client to join",
|
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}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue