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:
parent
cfa25cc61b
commit
8f35788e1a
1 changed files with 16 additions and 3 deletions
19
__init__.py
19
__init__.py
|
|
@ -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"]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue