mirror of
https://github.com/pacnpal/Pac-cogs.git
synced 2025-12-20 02:41:06 -05:00
Enhanced FFmpeg Integration:
Added robust error handling and logging Improved binary verification and initialization Added proper GPU detection and hardware acceleration Optimized encoding parameters for different content types Improved File Operations: Added retry mechanisms for file operations Enhanced temporary directory management Improved cleanup of failed downloads Added proper permission handling Enhanced Queue Management: Fixed queue manager initialization Added better error recovery Improved status tracking and logging Enhanced cleanup of failed items Better Error Handling: Added comprehensive exception hierarchy Improved error logging and reporting Added fallback mechanisms for failures Enhanced error recovery strategies
This commit is contained in:
@@ -6,31 +6,73 @@ import shutil
|
||||
import stat
|
||||
import logging
|
||||
import contextlib
|
||||
import time
|
||||
|
||||
logger = logging.getLogger("VideoArchiver")
|
||||
|
||||
@contextlib.contextmanager
|
||||
def temp_path_context():
|
||||
"""Context manager for temporary path creation and cleanup"""
|
||||
temp_dir = tempfile.mkdtemp(prefix="videoarchiver_")
|
||||
temp_dir = None
|
||||
try:
|
||||
# Ensure proper permissions
|
||||
# Create temp directory with proper permissions
|
||||
temp_dir = tempfile.mkdtemp(prefix="videoarchiver_")
|
||||
logger.debug(f"Created temporary directory: {temp_dir}")
|
||||
|
||||
# Ensure directory has rwx permissions for user only
|
||||
os.chmod(temp_dir, stat.S_IRWXU)
|
||||
|
||||
# Verify directory exists and is writable
|
||||
if not os.path.exists(temp_dir):
|
||||
raise OSError(f"Failed to create temporary directory: {temp_dir}")
|
||||
if not os.access(temp_dir, os.W_OK):
|
||||
raise OSError(f"Temporary directory is not writable: {temp_dir}")
|
||||
|
||||
yield temp_dir
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error in temp_path_context: {str(e)}")
|
||||
raise
|
||||
|
||||
finally:
|
||||
try:
|
||||
# Ensure all files are deletable
|
||||
for root, dirs, files in os.walk(temp_dir):
|
||||
for d in dirs:
|
||||
if temp_dir and os.path.exists(temp_dir):
|
||||
try:
|
||||
# Ensure all files are deletable with retries
|
||||
max_retries = 3
|
||||
for attempt in range(max_retries):
|
||||
try:
|
||||
os.chmod(os.path.join(root, d), stat.S_IRWXU)
|
||||
except OSError:
|
||||
pass
|
||||
for f in files:
|
||||
try:
|
||||
os.chmod(os.path.join(root, f), stat.S_IRWXU)
|
||||
except OSError:
|
||||
pass
|
||||
shutil.rmtree(temp_dir, ignore_errors=True)
|
||||
except Exception as e:
|
||||
logger.error(f"Error cleaning up temp directory {temp_dir}: {e}")
|
||||
# Set permissions recursively
|
||||
for root, dirs, files in os.walk(temp_dir):
|
||||
for d in dirs:
|
||||
try:
|
||||
dir_path = os.path.join(root, d)
|
||||
os.chmod(dir_path, stat.S_IRWXU)
|
||||
except OSError as e:
|
||||
logger.warning(f"Failed to set permissions on directory {dir_path}: {e}")
|
||||
for f in files:
|
||||
try:
|
||||
file_path = os.path.join(root, f)
|
||||
os.chmod(file_path, stat.S_IRWXU)
|
||||
except OSError as e:
|
||||
logger.warning(f"Failed to set permissions on file {file_path}: {e}")
|
||||
|
||||
# Try to remove the directory
|
||||
shutil.rmtree(temp_dir, ignore_errors=True)
|
||||
|
||||
# Verify directory is gone
|
||||
if not os.path.exists(temp_dir):
|
||||
logger.debug(f"Successfully cleaned up temporary directory: {temp_dir}")
|
||||
break
|
||||
|
||||
if attempt < max_retries - 1:
|
||||
time.sleep(1) # Wait before retry
|
||||
|
||||
except Exception as e:
|
||||
if attempt == max_retries - 1:
|
||||
logger.error(f"Failed to clean up temporary directory {temp_dir} after {max_retries} attempts: {e}")
|
||||
elif attempt < max_retries - 1:
|
||||
time.sleep(1) # Wait before retry
|
||||
continue
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error during temp directory cleanup: {str(e)}")
|
||||
|
||||
Reference in New Issue
Block a user