Better database connection cleanup.

This commit is contained in:
SergeantPanda 2025-10-16 14:22:19 -05:00
parent 4df2f79bcf
commit 9b2ebf169b
4 changed files with 37 additions and 10 deletions

View file

@ -50,13 +50,21 @@ app.conf.update(
)
# Add memory cleanup after task completion
#@task_postrun.connect # Use the imported signal
@task_postrun.connect # Use the imported signal
def cleanup_task_memory(**kwargs):
"""Clean up memory after each task completes"""
"""Clean up memory and database connections after each task completes"""
from django.db import connection
# Get task name from kwargs
task_name = kwargs.get('task').name if kwargs.get('task') else ''
# Only run cleanup for memory-intensive tasks
# Close database connection for this Celery worker process
try:
connection.close()
except Exception:
pass
# Only run memory cleanup for memory-intensive tasks
memory_intensive_tasks = [
'apps.m3u.tasks.refresh_single_m3u_account',
'apps.m3u.tasks.refresh_m3u_accounts',