Remove artifact directories older than the configured retention period. Args: checkpoint_path: The path of the current checkpoint being saved, which contains the artifacts folder to be cleaned.
(self, checkpoint_path: str)
| 2446 | return artifact_dir |
| 2447 | |
| 2448 | def _cleanup_old_artifacts(self, checkpoint_path: str) -> None: |
| 2449 | """ |
| 2450 | Remove artifact directories older than the configured retention period. |
| 2451 | |
| 2452 | Args: |
| 2453 | checkpoint_path: The path of the current checkpoint being saved, which |
| 2454 | contains the artifacts folder to be cleaned. |
| 2455 | """ |
| 2456 | if not self.config.cleanup_old_artifacts: |
| 2457 | return |
| 2458 | |
| 2459 | artifacts_base_path = os.path.join(checkpoint_path, "artifacts") |
| 2460 | |
| 2461 | if not os.path.isdir(artifacts_base_path): |
| 2462 | return |
| 2463 | |
| 2464 | now = time.time() |
| 2465 | # Convert retention days to seconds |
| 2466 | retention_seconds = self.config.artifact_retention_days * 24 * 60 * 60 |
| 2467 | deleted_count = 0 |
| 2468 | |
| 2469 | logger.debug(f"Starting artifact cleanup in {artifacts_base_path}...") |
| 2470 | |
| 2471 | for dirname in os.listdir(artifacts_base_path): |
| 2472 | dirpath = os.path.join(artifacts_base_path, dirname) |
| 2473 | if os.path.isdir(dirpath): |
| 2474 | try: |
| 2475 | dir_mod_time = os.path.getmtime(dirpath) |
| 2476 | if (now - dir_mod_time) > retention_seconds: |
| 2477 | shutil.rmtree(dirpath) |
| 2478 | deleted_count += 1 |
| 2479 | logger.debug(f"Removed old artifact directory: {dirpath}") |
| 2480 | except FileNotFoundError: |
| 2481 | # Can happen in race conditions; ignore. |
| 2482 | continue |
| 2483 | except Exception as e: |
| 2484 | logger.error(f"Error removing artifact directory {dirpath}: {e}") |
| 2485 | |
| 2486 | if deleted_count > 0: |
| 2487 | logger.info(f"Cleaned up {deleted_count} old artifact directories.") |
| 2488 | |
| 2489 | def _write_artifact_file(self, artifact_dir: str, key: str, value: Union[str, bytes]) -> None: |
| 2490 | """Write an artifact to a file""" |