From 864197cc111e696717f7868cc2b7ecc01471a28a Mon Sep 17 00:00:00 2001 From: nmemmert <67961292+nmemmert@users.noreply.github.com> Date: Fri, 7 Nov 2025 10:07:50 -0500 Subject: [PATCH] Add error handling to delete_job endpoint --- pdf_renamer.py | 71 +++++++++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 32 deletions(-) diff --git a/pdf_renamer.py b/pdf_renamer.py index 50cea52..129370b 100644 --- a/pdf_renamer.py +++ b/pdf_renamer.py @@ -1379,39 +1379,46 @@ def download(filename): @app.route('/delete/', methods=['POST']) @login_required def delete_job(job_id): - with job_lock: - if job_id in job_queue: - job = job_queue[job_id] - # Only allow deletion of completed jobs - if job.get('status') == 'completed': - # Clean up the ZIP file from archives folder - if job.get('zip_name'): - zip_path = os.path.join(app.config['ARCHIVE_FOLDER'], job['zip_name']) - if os.path.exists(zip_path): - try: - os.remove(zip_path) - print(f"DEBUG: Deleted archive file: {zip_path}") - except Exception as e: - print(f"ERROR: Failed to delete archive: {e}") - flash('Job deleted but archive file could not be removed.', 'warning') - - # Clean up any remaining uploaded files - for file_info in job.get('files', []): - if 'path' in file_info and os.path.exists(file_info['path']): - try: - os.remove(file_info['path']) - print(f"DEBUG: Deleted uploaded file: {file_info['path']}") - except Exception as e: - print(f"ERROR: Failed to delete uploaded file: {e}") - - # Remove the job from the queue - del job_queue[job_id] - save_jobs() # Persist job deletion - flash('Job deleted successfully.', 'success') + try: + with job_lock: + if job_id in job_queue: + job = job_queue[job_id] + # Only allow deletion of completed jobs + if job.get('status') == 'completed': + # Clean up the ZIP file from archives folder + if job.get('zip_name'): + zip_path = os.path.join(app.config['ARCHIVE_FOLDER'], job['zip_name']) + if os.path.exists(zip_path): + try: + os.remove(zip_path) + print(f"DEBUG: Deleted archive file: {zip_path}") + except Exception as e: + print(f"ERROR: Failed to delete archive: {e}") + flash('Job deleted but archive file could not be removed.', 'warning') + + # Clean up any remaining uploaded files + for file_info in job.get('files', []): + if 'path' in file_info and os.path.exists(file_info['path']): + try: + os.remove(file_info['path']) + print(f"DEBUG: Deleted uploaded file: {file_info['path']}") + except Exception as e: + print(f"ERROR: Failed to delete uploaded file: {e}") + + # Remove the job from the queue + del job_queue[job_id] + save_jobs() # Persist job deletion + flash('Job deleted successfully.', 'success') + else: + flash('Only completed jobs can be deleted.', 'error') else: - flash('Only completed jobs can be deleted.', 'error') - else: - flash('Job not found.', 'error') + flash('Job not found.', 'error') + except Exception as e: + print(f"ERROR: Exception in delete_job: {e}") + import traceback + traceback.print_exc() + flash(f'Error deleting job: {str(e)}', 'error') + return redirect('/jobs') @app.route('/completed-jobs')