Adds task cleanup on extension shutdown

Implements a mechanism to cancel pending background tasks
when the extension is stopped. This ensures proper cleanup and
prevents potential issues with lingering tasks.
This commit is contained in:
padreug 2025-11-02 01:41:34 +01:00
parent cfa25cc61b
commit 8f35788e1a

View file

@ -19,11 +19,24 @@ castle_static_files = [
}
]
scheduled_tasks: list[asyncio.Task] = []
def castle_stop():
"""Clean up background tasks on extension shutdown"""
for task in scheduled_tasks:
try:
task.cancel()
except Exception as ex:
logger.warning(ex)
def castle_start():
"""Initialize Castle extension background tasks"""
logger.info("Starting Castle accounting extension background tasks")
asyncio.create_task(wait_for_paid_invoices())
from lnbits.tasks import create_permanent_unique_task
task = create_permanent_unique_task("ext_castle", wait_for_paid_invoices)
scheduled_tasks.append(task)
__all__ = ["castle_ext", "castle_static_files", "db", "castle_start"]
__all__ = ["castle_ext", "castle_static_files", "db", "castle_start", "castle_stop"]