Skip to content

Commit

Permalink
fix: properly start/stop tasks (#100)
Browse files Browse the repository at this point in the history
* fix: properly start/stop tasks

lnbits/lnbits#2411
  • Loading branch information
dni authored May 27, 2024
1 parent 49b4ae7 commit 3325626
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 31 deletions.
35 changes: 22 additions & 13 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import asyncio
from asyncio import Task
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 .nostr.nostr_client import NostrClient

db = Database("ext_nostrmarket")

Expand All @@ -23,20 +24,29 @@
def nostrmarket_renderer():
return template_renderer(["nostrmarket/templates"])


from .nostr.nostr_client import NostrClient

nostr_client = NostrClient()

scheduled_tasks: List[Task] = []
nostr_client: NostrClient = NostrClient()


from .tasks import wait_for_nostr_events, wait_for_paid_invoices
from .views 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():

async def _subscribe_to_nostr_client():
# wait for 'nostrclient' extension to initialize
await asyncio.sleep(10)
Expand All @@ -47,8 +57,7 @@ async def _wait_for_nostr_events():
await asyncio.sleep(15)
await wait_for_nostr_events(nostr_client)

loop = asyncio.get_event_loop()
task1 = loop.create_task(catch_everything_and_restart(wait_for_paid_invoices))
task2 = loop.create_task(catch_everything_and_restart(_subscribe_to_nostr_client))
task3 = loop.create_task(catch_everything_and_restart(_wait_for_nostr_events))
task1 = create_permanent_unique_task("ext_nostrmarket_paid_invoices", wait_for_paid_invoices)
task2 = create_permanent_unique_task("ext_nostrmarket_subscribe_to_nostr_client", _subscribe_to_nostr_client)
task3 = create_permanent_unique_task("ext_nostrmarket_wait_for_events", _wait_for_nostr_events)
scheduled_tasks.extend([task1, task2, task3])
19 changes: 1 addition & 18 deletions views_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@
from lnbits.core.services import websocket_updater
from lnbits.decorators import (
WalletTypeInfo,
check_admin,
get_key_type,
require_admin_key,
require_invoice_key,
)
from lnbits.utils.exchange_rates import currencies

from . import nostr_client, nostrmarket_ext, scheduled_tasks
from . import nostr_client, nostrmarket_ext
from .crud import (
create_customer,
create_direct_message,
Expand Down Expand Up @@ -1116,19 +1115,3 @@ async def restart_nostr_client(wallet: WalletTypeInfo = Depends(require_admin_ke
await nostr_client.restart()
except Exception as 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}

0 comments on commit 3325626

Please sign in to comment.