2 Commits

Author SHA1 Message Date
jamie 4aaff495e1 Merge pull request 'refactor: improve logging and error handling in file processing' (#4) from v1.0.2 into master
Reviewed-on: #4
2026-01-08 20:43:22 +00:00
jamie be12f5220b refactor: improve logging and error handling in file processing
Release / release (pull_request) Successful in 1m14s
2026-01-08 20:42:42 +00:00
+13 -9
View File
@@ -552,13 +552,11 @@ class AudioEncoder:
def process_file(encoder_type, file_path, encoder):
"""Process a single file"""
global current_jobs
try:
logger.info(f"Starting to process {file_path} for {encoder_type}")
if not ENCODING_ENABLED:
logger.info(f"Encoding is disabled. Skipping {file_path}")
db_manager.remove_from_queue(encoder_type, file_path)
with job_lock:
current_jobs[encoder_type] = None
return
try:
if not os.path.exists(file_path):
logger.warning(f"File {file_path} no longer exists, skipping")
return
@@ -724,16 +722,17 @@ def process_file(encoder_type, file_path, encoder):
if os.path.exists(temp_input):
os.remove(temp_input)
except Exception as e:
logger.error(f"Error processing {file_path}: {e}")
logger.error(f"Error processing {file_path}: {e}", exc_info=True)
try:
file_size = os.path.getsize(file_path) if os.path.exists(file_path) else 0
db_manager.add_job_report(
encoder_type, file_path, os.path.getsize(file_path), 0,
encoder_type, file_path, file_size, 0,
os.path.splitext(file_path)[1], '', 'failed',
error_message=str(e),
target_resolution=target_resolution
target_resolution=target_resolution if 'target_resolution' in locals() else None
)
except:
pass
except Exception as report_error:
logger.error(f"Failed to add job report: {report_error}", exc_info=True)
finally:
# Remove from queue after processing is complete
db_manager.remove_from_queue(encoder_type, file_path)
@@ -743,23 +742,28 @@ def process_file(encoder_type, file_path, encoder):
def worker_thread(encoder_type):
"""Worker thread for processing jobs"""
encoder = VideoEncoder() if encoder_type == 'video' else AudioEncoder()
logger.info(f"Started {encoder_type} worker thread")
while True:
with job_lock:
queue = db_manager.get_queue(encoder_type, limit=1)
if queue and not current_jobs[encoder_type]:
file_path = queue[0]
logger.debug(f"{encoder_type} queue item: {file_path}")
# Check file exists before processing
if os.path.exists(file_path):
current_jobs[encoder_type] = {
'file_path': file_path,
'start_time': time.time()
}
logger.info(f"Starting job for {file_path}")
# Do NOT remove from queue here anymore
else:
# Remove from queue if file does not exist
logger.warning(f"File {file_path} does not exist, removing from queue")
db_manager.remove_from_queue(encoder_type, file_path)
if current_jobs[encoder_type]:
logger.debug(f"Processing {current_jobs[encoder_type]['file_path']}")
process_file(encoder_type, current_jobs[encoder_type]['file_path'], encoder)
time.sleep(1)