MCPcopy Create free account
hub / github.com/Dispatcharr/Dispatcharr / cleanup_task_memory

Function cleanup_task_memory

dispatcharr/celery.py:85–145  ·  view source on GitHub ↗

Clean up memory and database connections after each task completes

(**kwargs)

Source from the content-addressed store, hash-verified

83# Add memory cleanup after task completion
84@task_postrun.connect # Use the imported signal
85def cleanup_task_memory(**kwargs):
86 """Clean up memory and database connections after each task completes"""
87 from django.db import close_old_connections
88
89 # Get task name from kwargs
90 task_name = kwargs.get('task').name if kwargs.get('task') else ''
91
92 # Return all DB connections to the pool in a clean state
93 try:
94 close_old_connections()
95 except Exception:
96 pass
97
98 # Only run memory cleanup for memory-intensive tasks
99 memory_intensive_tasks = [
100 'apps.m3u.tasks.refresh_single_m3u_account',
101 'apps.m3u.tasks.refresh_m3u_accounts',
102 'apps.m3u.tasks.process_m3u_batch',
103 'apps.m3u.tasks.process_xc_category',
104 'apps.m3u.tasks.sync_auto_channels',
105 'apps.epg.tasks.refresh_epg_data',
106 'apps.epg.tasks.refresh_all_epg_data',
107 'apps.epg.tasks.parse_programs_for_source',
108 'apps.epg.tasks.parse_programs_for_tvg_id',
109 'apps.epg.tasks.build_programme_index_task',
110 'apps.channels.tasks.match_epg_channels',
111 'apps.channels.tasks.match_selected_channels_epg',
112 'apps.channels.tasks.match_single_channel_epg',
113 'core.tasks.rehash_streams',
114 'apps.vod.tasks.refresh_vod_content',
115 'apps.vod.tasks.batch_refresh_series_episodes',
116 ]
117
118 # Check if this is a memory-intensive task
119 if task_name in memory_intensive_tasks:
120 # Import cleanup_memory function
121 from core.utils import cleanup_memory
122
123 # Use the comprehensive cleanup function
124 cleanup_memory(log_usage=True, force_collection=True)
125
126 # Log memory usage if psutil is installed
127 try:
128 import psutil
129 process = psutil.Process()
130 if hasattr(process, 'memory_info'):
131 mem = process.memory_info().rss / (1024 * 1024)
132 print(f"Memory usage after {task_name}: {mem:.2f} MB")
133 except (ImportError, Exception):
134 pass
135 else:
136 # For non-intensive tasks, just log but don't force cleanup
137 try:
138 import psutil
139 process = psutil.Process()
140 if hasattr(process, 'memory_info'):
141 mem = process.memory_info().rss / (1024 * 1024)
142 if mem > 500: # Only log if using more than 500MB

Callers

nothing calls this directly

Calls 2

cleanup_memoryFunction · 0.90
getMethod · 0.45

Tested by

no test coverage detected