fix(#338): Log queue corruption and backup corrupted file
- Log ERROR when queue corruption detected with error details - Create timestamped backup before discarding corrupted data - Add comprehensive tests for corruption handling Refs #338 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,13 +1,18 @@
|
||||
"""Queue manager for issue coordination."""
|
||||
|
||||
import json
|
||||
import logging
|
||||
import shutil
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from src.models import IssueMetadata
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class QueueItemStatus(str, Enum):
|
||||
"""Status of a queue item."""
|
||||
@@ -229,6 +234,40 @@ class QueueManager:
|
||||
|
||||
# Update ready status after loading
|
||||
self._update_ready_status()
|
||||
except (json.JSONDecodeError, KeyError, ValueError):
|
||||
# If file is corrupted, start with empty queue
|
||||
self._items = {}
|
||||
except (json.JSONDecodeError, KeyError, ValueError) as e:
|
||||
# Log corruption details and create backup before discarding
|
||||
self._handle_corrupted_queue(e)
|
||||
|
||||
def _handle_corrupted_queue(self, error: Exception) -> None:
|
||||
"""Handle corrupted queue file by logging, backing up, and resetting.
|
||||
|
||||
Args:
|
||||
error: The exception that was raised during loading
|
||||
"""
|
||||
# Log error with details
|
||||
logger.error(
|
||||
"Queue file corruption detected in '%s': %s - %s",
|
||||
self.queue_file,
|
||||
type(error).__name__,
|
||||
str(error),
|
||||
)
|
||||
|
||||
# Create backup of corrupted file with timestamp
|
||||
if self.queue_file.exists():
|
||||
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||
backup_path = self.queue_file.with_suffix(f".corrupted.{timestamp}.json")
|
||||
try:
|
||||
shutil.copy2(self.queue_file, backup_path)
|
||||
logger.error(
|
||||
"Corrupted queue file backed up to '%s'",
|
||||
backup_path,
|
||||
)
|
||||
except OSError as backup_error:
|
||||
logger.error(
|
||||
"Failed to create backup of corrupted queue file: %s",
|
||||
backup_error,
|
||||
)
|
||||
|
||||
# Reset to empty queue
|
||||
self._items = {}
|
||||
logger.error("Queue reset to empty state after corruption")
|
||||
|
||||
Reference in New Issue
Block a user