3.9 KiB
3.9 KiB
Implementation Plan
任务概述
将分散在多个文件中的容器停止逻辑统一到 docker_manager crate 的新模块中,提供两种清理策略:启动时清理和运行时清理。
任务列表
-
1. 创建 container_stop 模块基础结构
- 在
crates/docker_manager/src/创建container_stop.rs文件 - 定义模块常量(超时时间等)
- 在
lib.rs中导出新模块 - Requirements: 3.6, 5.1
- 在
-
2. 实现启动时清理功能
-
2.1 实现 startup_cleanup_containers 函数
- 实现容器模式匹配查找
- 实现 5 秒超时停止逻辑
- 实现 409 错误过滤
- 返回 CleanupResult 统计信息
- Requirements: 1.1, 1.2, 1.3, 1.4
-
2.2 实现 stop_container_startup_mode 辅助函数
- 调用 DockerManager::stop_container_by_id_with_timeout
- 使用 STARTUP_CLEANUP_TIMEOUT_SECONDS 常量
- 实现错误处理和日志记录
- Requirements: 1.2, 4.1, 4.2
-
2.3 实现 is_409_conflict_error 辅助函数
- 检查 DockerError 是否包含 "409" 和 "already in progress"
- 返回布尔值
- Requirements: 1.3
-
3. 实现运行时清理功能
-
3.1 实现 runtime_cleanup_container 函数
- 实现单个容器停止逻辑
- 使用 3 秒优雅停止超时
- 超时后立即强制停止
- Requirements: 2.1, 2.2, 2.3
-
3.2 实现 runtime_cleanup_containers 批量清理函数
- 批量处理多个容器 ID
- 返回 CleanupResult 统计信息
- Requirements: 2.4, 2.5
-
3.3 实现 stop_container_runtime_mode 辅助函数
- 调用 DockerManager::stop_container_by_id_with_timeout
- 使用 RUNTIME_CLEANUP_TIMEOUT_SECONDS 常量
- 实现错误处理和日志记录
- Requirements: 2.2, 4.1, 4.4
-
4. 更新 main.rs 使用新的启动清理接口
-
4.1 导入 container_stop 模块
- 添加
use docker_manager::container_stop; - Requirements: 3.3, 5.2
- 添加
-
4.2 替换 startup_cleanup_orphaned_containers 函数
- 调用
container_stop::startup_cleanup_containers - 更新错误处理逻辑
- 更新日志输出
- Requirements: 3.3, 4.2, 4.3
- 调用
-
4.3 删除旧的 startup_cleanup_orphaned_containers 实现
- 删除
startup_cleanup_orphaned_containers函数 - 删除
find_and_cleanup_orphaned_containers函数(如果不再使用) - Requirements: 5.5
- 删除
-
5. 更新 cleanup_task.rs 使用新的运行时清理接口
-
5.1 导入 container_stop 模块
- 在
destroy_docker_container方法中添加导入 - Requirements: 3.4, 5.3
- 在
-
5.2 替换容器停止逻辑
- 将
stop_container_by_id_with_timeout调用替换为container_stop::runtime_cleanup_container - 简化错误处理逻辑
- 更新日志输出
- Requirements: 3.4, 4.4
- 将
-
5.3 清理 cleanup_single_orphaned_container 方法
- 使用新的 runtime_cleanup_container 接口
- 删除重复的停止逻辑
- Requirements: 5.5
-
6. 更新 container_manager.rs(如需要)
-
6.1 检查是否有容器停止逻辑
- 搜索
stop_container相关调用 - 确定是否需要更新
- Requirements: 3.5, 5.4
- 搜索
-
6.2 如有需要,替换为新接口
- 导入 container_stop 模块
- 替换停止逻辑
- Requirements: 3.5, 5.4
-
7. 添加文档和注释
-
7.1 为 container_stop.rs 添加模块级文档
- 说明两种清理策略的区别
- 提供使用示例
- Requirements: 5.5
-
7.2 为公共函数添加详细文档注释
- 包含参数说明
- 包含返回值说明
- 包含使用示例
- Requirements: 4.5
-
8. 验证和测试
-
8.1 编译检查
- 运行
cargo check确保无编译错误 - 运行
cargo clippy检查代码质量 - Requirements: 5.5
- 运行
-
8.2 功能测试
- 测试启动时清理(模拟 409 错误场景)
- 测试运行时清理(验证 3 秒超时)
- 验证日志输出正确
- Requirements: 1.5, 2.5, 4.1-4.5