Merge pull request #10 from lnbits/bug/loop-tasks-stop
add the main tasks to scheduled_tasks and make dem deinitilize on api_stop
This commit is contained in:
commit
8f8786ccc5
2 changed files with 15 additions and 3 deletions
10
__init__.py
10
__init__.py
|
|
@ -1,3 +1,5 @@
|
||||||
|
import asyncio
|
||||||
|
from typing import List
|
||||||
from fastapi import APIRouter
|
from fastapi import APIRouter
|
||||||
from starlette.staticfiles import StaticFiles
|
from starlette.staticfiles import StaticFiles
|
||||||
|
|
||||||
|
|
@ -17,6 +19,8 @@ nostrclient_static_files = [
|
||||||
|
|
||||||
nostrclient_ext: APIRouter = APIRouter(prefix="/nostrclient", tags=["nostrclient"])
|
nostrclient_ext: APIRouter = APIRouter(prefix="/nostrclient", tags=["nostrclient"])
|
||||||
|
|
||||||
|
scheduled_tasks: List[asyncio.Task] = []
|
||||||
|
|
||||||
|
|
||||||
def nostr_renderer():
|
def nostr_renderer():
|
||||||
return template_renderer(["lnbits/extensions/nostrclient/templates"])
|
return template_renderer(["lnbits/extensions/nostrclient/templates"])
|
||||||
|
|
@ -29,5 +33,7 @@ from .views_api import * # noqa
|
||||||
|
|
||||||
def nostrclient_start():
|
def nostrclient_start():
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
loop.create_task(catch_everything_and_restart(init_relays))
|
task1 = loop.create_task(catch_everything_and_restart(init_relays))
|
||||||
loop.create_task(catch_everything_and_restart(subscribe_events))
|
scheduled_tasks.append(task1)
|
||||||
|
task2 = loop.create_task(catch_everything_and_restart(subscribe_events))
|
||||||
|
scheduled_tasks.append(task2)
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ from starlette.exceptions import HTTPException
|
||||||
from lnbits.decorators import check_admin
|
from lnbits.decorators import check_admin
|
||||||
from lnbits.helpers import urlsafe_short_hash
|
from lnbits.helpers import urlsafe_short_hash
|
||||||
|
|
||||||
from . import nostrclient_ext
|
from . import nostrclient_ext, scheduled_tasks
|
||||||
from .crud import add_relay, delete_relay, get_relays
|
from .crud import add_relay, delete_relay, get_relays
|
||||||
from .models import Relay, RelayList
|
from .models import Relay, RelayList
|
||||||
from .services import NostrRouter, nostr
|
from .services import NostrRouter, nostr
|
||||||
|
|
@ -92,6 +92,12 @@ async def api_stop():
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(e)
|
logger.error(e)
|
||||||
|
|
||||||
|
for scheduled_task in scheduled_tasks:
|
||||||
|
try:
|
||||||
|
scheduled_task.cancel()
|
||||||
|
except Exception as ex:
|
||||||
|
logger.warning(ex)
|
||||||
|
|
||||||
return {"success": True}
|
return {"success": True}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue