|
21 | 21 | import urllib.request |
22 | 22 | import uuid |
23 | 23 | import zipfile |
24 | | -from datetime import datetime |
| 24 | +from datetime import datetime, timedelta |
25 | 25 | from typing import Any, Optional |
26 | 26 |
|
27 | 27 | import folder_paths |
@@ -191,6 +191,7 @@ def __init__(self): |
191 | 191 | self.batch_start_time = None |
192 | 192 | self.batch_state_before = None |
193 | 193 | self._worker_task = None |
| 194 | + self._cleanup_performed = False |
194 | 195 |
|
195 | 196 | def is_processing(self) -> bool: |
196 | 197 | """Check if the queue is currently processing tasks""" |
@@ -546,6 +547,11 @@ def finalize(self) -> None: |
546 | 547 | self.batch_start_time = None |
547 | 548 | self.batch_state_before = None |
548 | 549 |
|
| 550 | + # Cleanup old batch files once per session |
| 551 | + if not self._cleanup_performed: |
| 552 | + self._cleanup_old_batches() |
| 553 | + self._cleanup_performed = True |
| 554 | + |
549 | 555 | except Exception as e: |
550 | 556 | logging.error(f"[ComfyUI-Manager] Failed to save batch history: {e}") |
551 | 557 |
|
@@ -587,20 +593,20 @@ def _get_frontend_version(self) -> Optional[str]: |
587 | 593 | # Check if front-end-root is specified (overrides version) |
588 | 594 | if hasattr(args, "front_end_root") and args.front_end_root: |
589 | 595 | return f"custom-root: {args.front_end_root}" |
590 | | - |
| 596 | + |
591 | 597 | # Check if front-end-version is specified |
592 | 598 | if hasattr(args, "front_end_version") and args.front_end_version: |
593 | 599 | if "@" in args.front_end_version: |
594 | 600 | return args.front_end_version.split("@")[1] |
595 | 601 | else: |
596 | 602 | return args.front_end_version |
597 | | - |
| 603 | + |
598 | 604 | # Otherwise, check for installed package |
599 | 605 | pip_packages = self._get_pip_packages() |
600 | 606 | for package_name in ["comfyui-frontend", "comfyui_frontend"]: |
601 | 607 | if package_name in pip_packages: |
602 | 608 | return pip_packages[package_name] |
603 | | - |
| 609 | + |
604 | 610 | return None |
605 | 611 | except Exception: |
606 | 612 | return None |
@@ -741,7 +747,7 @@ def _extract_batch_operations(self) -> list[BatchOperation]: |
741 | 747 | # Only include operations from the current batch |
742 | 748 | if task.batch_id != self.batch_id: |
743 | 749 | continue |
744 | | - |
| 750 | + |
745 | 751 | result_status = OperationResult.success |
746 | 752 | if task.status: |
747 | 753 | status_str = ( |
@@ -773,6 +779,39 @@ def _extract_batch_operations(self) -> list[BatchOperation]: |
773 | 779 |
|
774 | 780 | return operations |
775 | 781 |
|
| 782 | + def _cleanup_old_batches(self) -> None: |
| 783 | + """Clean up batch history files older than 90 days. |
| 784 | +
|
| 785 | + This is a best-effort cleanup that silently ignores any errors |
| 786 | + to avoid disrupting normal operations. |
| 787 | + """ |
| 788 | + try: |
| 789 | + cutoff = datetime.now() - timedelta(days=16) |
| 790 | + cutoff_timestamp = cutoff.timestamp() |
| 791 | + |
| 792 | + pattern = os.path.join(context.manager_batch_history_path, "batch_*.json") |
| 793 | + removed_count = 0 |
| 794 | + |
| 795 | + import glob |
| 796 | + |
| 797 | + for file_path in glob.glob(pattern): |
| 798 | + try: |
| 799 | + if os.path.getmtime(file_path) < cutoff_timestamp: |
| 800 | + os.remove(file_path) |
| 801 | + removed_count += 1 |
| 802 | + except Exception: |
| 803 | + pass |
| 804 | + |
| 805 | + if removed_count > 0: |
| 806 | + logging.debug( |
| 807 | + "[ComfyUI-Manager] Cleaned up %d old batch history files", |
| 808 | + removed_count, |
| 809 | + ) |
| 810 | + |
| 811 | + except Exception: |
| 812 | + # Silently ignore all errors - this is non-critical functionality |
| 813 | + pass |
| 814 | + |
776 | 815 |
|
777 | 816 | task_queue = TaskQueue() |
778 | 817 |
|
|
0 commit comments