feat: add extension clean-up endpoint
This commit is contained in:
parent
230729483c
commit
7ec3045130
4 changed files with 29 additions and 6 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
|
from typing import List
|
||||||
|
|
||||||
from fastapi import APIRouter
|
from fastapi import APIRouter
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
|
|
@ -19,6 +20,7 @@ nostrrelay_static_files = [
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
|
scheduled_tasks: List[asyncio.Task] = []
|
||||||
|
|
||||||
def nostrrelay_renderer():
|
def nostrrelay_renderer():
|
||||||
return template_renderer(["lnbits/extensions/nostrrelay/templates"])
|
return template_renderer(["lnbits/extensions/nostrrelay/templates"])
|
||||||
|
|
@ -31,4 +33,5 @@ from .views_api import * # noqa
|
||||||
|
|
||||||
def nostrrelay_start():
|
def nostrrelay_start():
|
||||||
loop = asyncio.get_event_loop()
|
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)
|
||||||
|
|
|
||||||
|
|
@ -52,6 +52,10 @@ class NostrClientManager:
|
||||||
self._clients[relay_id] = []
|
self._clients[relay_id] = []
|
||||||
return self._clients[relay_id]
|
return self._clients[relay_id]
|
||||||
|
|
||||||
|
async def stop(self):
|
||||||
|
for relay_id in self._active_relays:
|
||||||
|
await self._stop_clients_for_relay(relay_id)
|
||||||
|
|
||||||
async def _stop_clients_for_relay(self, relay_id: str):
|
async def _stop_clients_for_relay(self, relay_id: str):
|
||||||
for client in self.clients(relay_id):
|
for client in self.clients(relay_id):
|
||||||
if client.relay_id == relay_id:
|
if client.relay_id == relay_id:
|
||||||
|
|
|
||||||
|
|
@ -6,11 +6,11 @@ import pytest
|
||||||
from fastapi import WebSocket
|
from fastapi import WebSocket
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
|
||||||
from lnbits.extensions.nostrrelay.relay.client_connection import (
|
from lnbits.extensions.nostrrelay.relay.client_connection import ( # type: ignore
|
||||||
NostrClientConnection, # type: ignore
|
NostrClientConnection,
|
||||||
)
|
)
|
||||||
from lnbits.extensions.nostrrelay.relay.client_manager import (
|
from lnbits.extensions.nostrrelay.relay.client_manager import ( # type: ignore
|
||||||
NostrClientManager, # type: ignore
|
NostrClientManager,
|
||||||
)
|
)
|
||||||
from lnbits.extensions.nostrrelay.relay.relay import RelaySpec # type: ignore
|
from lnbits.extensions.nostrrelay.relay.relay import RelaySpec # type: ignore
|
||||||
|
|
||||||
|
|
|
||||||
18
views_api.py
18
views_api.py
|
|
@ -15,7 +15,7 @@ from lnbits.decorators import (
|
||||||
)
|
)
|
||||||
from lnbits.helpers import urlsafe_short_hash
|
from lnbits.helpers import urlsafe_short_hash
|
||||||
|
|
||||||
from . import nostrrelay_ext
|
from . import nostrrelay_ext, scheduled_tasks
|
||||||
from .crud import (
|
from .crud import (
|
||||||
create_account,
|
create_account,
|
||||||
create_relay,
|
create_relay,
|
||||||
|
|
@ -283,3 +283,19 @@ 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