fix: properly start/stop tasks (#100)
* fix: properly start/stop tasks https://github.com/lnbits/lnbits/issues/2411
This commit is contained in:
parent
2ce0c98a76
commit
6289d2d36a
2 changed files with 23 additions and 31 deletions
35
__init__.py
35
__init__.py
|
|
@ -1,12 +1,13 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
from asyncio import Task
|
|
||||||
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 .nostr.nostr_client import NostrClient
|
||||||
|
|
||||||
db = Database("ext_nostrmarket")
|
db = Database("ext_nostrmarket")
|
||||||
|
|
||||||
|
|
@ -23,12 +24,7 @@ nostrmarket_static_files = [
|
||||||
def nostrmarket_renderer():
|
def nostrmarket_renderer():
|
||||||
return template_renderer(["nostrmarket/templates"])
|
return template_renderer(["nostrmarket/templates"])
|
||||||
|
|
||||||
|
nostr_client: NostrClient = NostrClient()
|
||||||
from .nostr.nostr_client import NostrClient
|
|
||||||
|
|
||||||
nostr_client = NostrClient()
|
|
||||||
|
|
||||||
scheduled_tasks: List[Task] = []
|
|
||||||
|
|
||||||
|
|
||||||
from .tasks import wait_for_nostr_events, wait_for_paid_invoices
|
from .tasks import wait_for_nostr_events, wait_for_paid_invoices
|
||||||
|
|
@ -36,7 +32,21 @@ from .views import * # noqa
|
||||||
from .views_api import * # noqa
|
from .views_api import * # noqa
|
||||||
|
|
||||||
|
|
||||||
|
scheduled_tasks: list[asyncio.Task] = []
|
||||||
|
|
||||||
|
|
||||||
|
async def nostrmarket_stop():
|
||||||
|
for task in scheduled_tasks:
|
||||||
|
try:
|
||||||
|
task.cancel()
|
||||||
|
except Exception as ex:
|
||||||
|
logger.warning(ex)
|
||||||
|
|
||||||
|
await nostr_client.stop()
|
||||||
|
|
||||||
|
|
||||||
def nostrmarket_start():
|
def nostrmarket_start():
|
||||||
|
|
||||||
async def _subscribe_to_nostr_client():
|
async def _subscribe_to_nostr_client():
|
||||||
# wait for 'nostrclient' extension to initialize
|
# wait for 'nostrclient' extension to initialize
|
||||||
await asyncio.sleep(10)
|
await asyncio.sleep(10)
|
||||||
|
|
@ -47,8 +57,7 @@ def nostrmarket_start():
|
||||||
await asyncio.sleep(15)
|
await asyncio.sleep(15)
|
||||||
await wait_for_nostr_events(nostr_client)
|
await wait_for_nostr_events(nostr_client)
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
task1 = create_permanent_unique_task("ext_nostrmarket_paid_invoices", wait_for_paid_invoices)
|
||||||
task1 = loop.create_task(catch_everything_and_restart(wait_for_paid_invoices))
|
task2 = create_permanent_unique_task("ext_nostrmarket_subscribe_to_nostr_client", _subscribe_to_nostr_client)
|
||||||
task2 = loop.create_task(catch_everything_and_restart(_subscribe_to_nostr_client))
|
task3 = create_permanent_unique_task("ext_nostrmarket_wait_for_events", _wait_for_nostr_events)
|
||||||
task3 = loop.create_task(catch_everything_and_restart(_wait_for_nostr_events))
|
|
||||||
scheduled_tasks.extend([task1, task2, task3])
|
scheduled_tasks.extend([task1, task2, task3])
|
||||||
|
|
|
||||||
19
views_api.py
19
views_api.py
|
|
@ -9,14 +9,13 @@ from loguru import logger
|
||||||
from lnbits.core.services import websocket_updater
|
from lnbits.core.services import websocket_updater
|
||||||
from lnbits.decorators import (
|
from lnbits.decorators import (
|
||||||
WalletTypeInfo,
|
WalletTypeInfo,
|
||||||
check_admin,
|
|
||||||
get_key_type,
|
get_key_type,
|
||||||
require_admin_key,
|
require_admin_key,
|
||||||
require_invoice_key,
|
require_invoice_key,
|
||||||
)
|
)
|
||||||
from lnbits.utils.exchange_rates import currencies
|
from lnbits.utils.exchange_rates import currencies
|
||||||
|
|
||||||
from . import nostr_client, nostrmarket_ext, scheduled_tasks
|
from . import nostr_client, nostrmarket_ext
|
||||||
from .crud import (
|
from .crud import (
|
||||||
create_customer,
|
create_customer,
|
||||||
create_direct_message,
|
create_direct_message,
|
||||||
|
|
@ -1116,19 +1115,3 @@ async def restart_nostr_client(wallet: WalletTypeInfo = Depends(require_admin_ke
|
||||||
await nostr_client.restart()
|
await nostr_client.restart()
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
logger.warning(ex)
|
logger.warning(ex)
|
||||||
|
|
||||||
|
|
||||||
@nostrmarket_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 nostr_client.stop()
|
|
||||||
except Exception as ex:
|
|
||||||
logger.warning(ex)
|
|
||||||
|
|
||||||
return {"success": True}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue