Add detailed debugging to backup_cancel function

🐛 Debug Enhancement:
- Added extensive print statements to track execution flow
- Identify exact location where 'User matching query does not exist' occurs
- Added traceback logging for better error diagnosis
- Isolated potential problem areas: user access, model saves, audit logging

This will help pinpoint where the user query error happens during backup cancellation.
This commit is contained in:
2025-09-29 20:42:54 +02:00
parent 33888a3b18
commit c46c3543b1

View File

@@ -6013,9 +6013,13 @@ def backup_restore(request):
def backup_cancel(request, backup_id):
"""Cancel a running backup job"""
from stiftung.models import BackupJob
import traceback
try:
print(f"DEBUG: Attempting to cancel backup job {backup_id}")
backup_job = BackupJob.objects.get(id=backup_id)
print(f"DEBUG: Found backup job - ID: {backup_job.id}, Status: {backup_job.status}")
print(f"DEBUG: Created by: {backup_job.created_by}, Current user: {request.user}")
# Only allow cancelling running or pending jobs
if backup_job.status not in ['running', 'pending']:
@@ -6024,35 +6028,53 @@ def backup_cancel(request, backup_id):
# Check if user has permission to cancel (either own job or admin)
# Handle case where created_by might be None (user deleted)
print(f"DEBUG: Checking permissions - created_by: {backup_job.created_by}, is_staff: {request.user.is_staff}")
if backup_job.created_by is not None and backup_job.created_by != request.user and not request.user.is_staff:
messages.error(request, "Sie können nur Ihre eigenen Backup-Jobs abbrechen.")
return redirect("stiftung:backup_management")
# Mark as cancelled
print("DEBUG: About to mark job as cancelled")
from django.utils import timezone
backup_job.status = "cancelled"
backup_job.completed_at = timezone.now()
print(f"DEBUG: About to set error message with username: {request.user.username}")
backup_job.error_message = f"Abgebrochen von {request.user.username}"
print("DEBUG: About to save backup job")
backup_job.save()
print("DEBUG: Backup job saved successfully")
# Log the cancellation (with error handling)
try:
print("DEBUG: About to log system action")
from stiftung.audit import log_system_action
print(f"DEBUG: About to call get_backup_type_display")
backup_type_display = backup_job.get_backup_type_display()
print(f"DEBUG: Backup type display: {backup_type_display}")
log_system_action(
request=request,
action="backup_cancel",
description=f"Backup-Job abgebrochen: {backup_job.get_backup_type_display()}",
description=f"Backup-Job abgebrochen: {backup_type_display}",
details={"backup_job_id": str(backup_job.id)},
)
print("DEBUG: System action logged successfully")
except Exception as audit_error:
print(f"ERROR in audit logging: {audit_error}")
print(f"ERROR traceback: {traceback.format_exc()}")
# Don't fail the cancellation if logging fails
print(f"Warning: Could not log backup cancellation: {audit_error}")
messages.success(request, f"Backup-Job wurde abgebrochen.")
except BackupJob.DoesNotExist:
print(f"ERROR: Backup job {backup_id} not found")
messages.error(request, "Backup-Job nicht gefunden.")
except Exception as e:
print(f"ERROR: Unexpected error in backup_cancel: {e}")
print(f"ERROR traceback: {traceback.format_exc()}")
messages.error(request, f"Fehler beim Abbrechen des Backup-Jobs: {e}")
return redirect("stiftung:backup_management")