mirror of
https://github.com/Dispatcharr/Dispatcharr.git
synced 2026-01-23 02:35:14 +00:00
Better database connection cleanup.
This commit is contained in:
parent
4df2f79bcf
commit
9b2ebf169b
4 changed files with 37 additions and 10 deletions
|
|
@ -33,14 +33,8 @@ class BaseConfig:
|
||||||
settings = CoreSettings.get_proxy_settings()
|
settings = CoreSettings.get_proxy_settings()
|
||||||
cls._proxy_settings_cache = settings
|
cls._proxy_settings_cache = settings
|
||||||
cls._proxy_settings_cache_time = now
|
cls._proxy_settings_cache_time = now
|
||||||
|
|
||||||
# Close the connection after reading settings to avoid keeping it open
|
|
||||||
try:
|
|
||||||
connection.close()
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
|
|
||||||
return settings
|
return settings
|
||||||
|
|
||||||
except Exception:
|
except Exception:
|
||||||
# Return defaults if database query fails
|
# Return defaults if database query fails
|
||||||
return {
|
return {
|
||||||
|
|
@ -50,6 +44,13 @@ class BaseConfig:
|
||||||
"channel_shutdown_delay": 0,
|
"channel_shutdown_delay": 0,
|
||||||
"channel_init_grace_period": 5,
|
"channel_init_grace_period": 5,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
finally:
|
||||||
|
# Always close the connection after reading settings
|
||||||
|
try:
|
||||||
|
connection.close()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_redis_chunk_ttl(cls):
|
def get_redis_chunk_ttl(cls):
|
||||||
|
|
|
||||||
|
|
@ -597,6 +597,8 @@ class ChannelService:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _update_stream_stats_in_db(stream_id, **stats):
|
def _update_stream_stats_in_db(stream_id, **stats):
|
||||||
"""Update stream stats in database"""
|
"""Update stream stats in database"""
|
||||||
|
from django.db import connection
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from apps.channels.models import Stream
|
from apps.channels.models import Stream
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
@ -622,6 +624,13 @@ class ChannelService:
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error updating stream stats in database for stream {stream_id}: {e}")
|
logger.error(f"Error updating stream stats in database for stream {stream_id}: {e}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
finally:
|
||||||
|
# Always close database connection after update
|
||||||
|
try:
|
||||||
|
connection.close()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
# Helper methods for Redis operations
|
# Helper methods for Redis operations
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -930,6 +930,7 @@ class StreamManager:
|
||||||
|
|
||||||
# Import both models for proper resource management
|
# Import both models for proper resource management
|
||||||
from apps.channels.models import Stream, Channel
|
from apps.channels.models import Stream, Channel
|
||||||
|
from django.db import connection
|
||||||
|
|
||||||
# Update stream profile if we're switching streams
|
# Update stream profile if we're switching streams
|
||||||
if self.current_stream_id and stream_id and self.current_stream_id != stream_id:
|
if self.current_stream_id and stream_id and self.current_stream_id != stream_id:
|
||||||
|
|
@ -947,8 +948,16 @@ class StreamManager:
|
||||||
logger.debug(f"Updated m3u profile for channel {self.channel_id} to use profile from stream {stream_id}")
|
logger.debug(f"Updated m3u profile for channel {self.channel_id} to use profile from stream {stream_id}")
|
||||||
else:
|
else:
|
||||||
logger.warning(f"Failed to update stream profile for channel {self.channel_id}")
|
logger.warning(f"Failed to update stream profile for channel {self.channel_id}")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error updating stream profile for channel {self.channel_id}: {e}")
|
logger.error(f"Error updating stream profile for channel {self.channel_id}: {e}")
|
||||||
|
|
||||||
|
finally:
|
||||||
|
# Always close database connection after profile update
|
||||||
|
try:
|
||||||
|
connection.close()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
# CRITICAL: Set a flag to prevent immediate reconnection with old URL
|
# CRITICAL: Set a flag to prevent immediate reconnection with old URL
|
||||||
self.url_switching = True
|
self.url_switching = True
|
||||||
|
|
|
||||||
|
|
@ -50,13 +50,21 @@ app.conf.update(
|
||||||
)
|
)
|
||||||
|
|
||||||
# Add memory cleanup after task completion
|
# 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):
|
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
|
# Get task name from kwargs
|
||||||
task_name = kwargs.get('task').name if kwargs.get('task') else ''
|
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 = [
|
memory_intensive_tasks = [
|
||||||
'apps.m3u.tasks.refresh_single_m3u_account',
|
'apps.m3u.tasks.refresh_single_m3u_account',
|
||||||
'apps.m3u.tasks.refresh_m3u_accounts',
|
'apps.m3u.tasks.refresh_m3u_accounts',
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue