Add error handling to delete_job endpoint

This commit is contained in:
nmemmert committed 2025-11-07 10:07:50 -05:00
1 parent 90ed775883
commit 864197cc11
1 file changed
+39 -32
+39 -32
View File
@@ -1379,39 +1379,46 @@ def download(filename):
@app.route('/delete/<job_id>', methods=['POST']) @app.route('/delete/<job_id>', methods=['POST'])
@login_required @login_required
def delete_job(job_id): def delete_job(job_id):
with job_lock: try:
if job_id in job_queue: with job_lock:
job = job_queue[job_id] if job_id in job_queue:
# Only allow deletion of completed jobs job = job_queue[job_id]
if job.get('status') == 'completed': # Only allow deletion of completed jobs
# Clean up the ZIP file from archives folder if job.get('status') == 'completed':
if job.get('zip_name'): # Clean up the ZIP file from archives folder
zip_path = os.path.join(app.config['ARCHIVE_FOLDER'], job['zip_name']) if job.get('zip_name'):
if os.path.exists(zip_path): zip_path = os.path.join(app.config['ARCHIVE_FOLDER'], job['zip_name'])
try: if os.path.exists(zip_path):
os.remove(zip_path) try:
print(f"DEBUG: Deleted archive file: {zip_path}") os.remove(zip_path)
except Exception as e: print(f"DEBUG: Deleted archive file: {zip_path}")
print(f"ERROR: Failed to delete archive: {e}") except Exception as e:
flash('Job deleted but archive file could not be removed.', 'warning') 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', []): # Clean up any remaining uploaded files
if 'path' in file_info and os.path.exists(file_info['path']): for file_info in job.get('files', []):
try: if 'path' in file_info and os.path.exists(file_info['path']):
os.remove(file_info['path']) try:
print(f"DEBUG: Deleted uploaded file: {file_info['path']}") os.remove(file_info['path'])
except Exception as e: print(f"DEBUG: Deleted uploaded file: {file_info['path']}")
print(f"ERROR: Failed to delete uploaded file: {e}") except Exception as e:
print(f"ERROR: Failed to delete uploaded file: {e}")
# Remove the job from the queue
del job_queue[job_id] # Remove the job from the queue
save_jobs() # Persist job deletion del job_queue[job_id]
flash('Job deleted successfully.', 'success') save_jobs() # Persist job deletion
flash('Job deleted successfully.', 'success')
else:
flash('Only completed jobs can be deleted.', 'error')
else: else:
flash('Only completed jobs can be deleted.', 'error') flash('Job not found.', 'error')
else: except Exception as e:
flash('Job not found.', 'error') print(f"ERROR: Exception in delete_job: {e}")
import traceback
traceback.print_exc()
flash(f'Error deleting job: {str(e)}', 'error')
return redirect('/jobs') return redirect('/jobs')
@app.route('/completed-jobs') @app.route('/completed-jobs')