|
|
6 months ago | |
|---|---|---|
| .. | ||
| AdminControllers | 6 months ago | |
| Commands | 6 months ago | |
| Databases | 6 months ago | |
| Docs | 6 months ago | |
| Enums | 6 months ago | |
| Helpers | 6 months ago | |
| Logics | 6 months ago | |
| Models | 6 months ago | |
| Repositories | 6 months ago | |
| Services | 6 months ago | |
| config | 6 months ago | |
| routes | 6 months ago | |
| CleanupServiceProvider.php | 6 months ago | |
| README.md | 6 months ago | |
CLEANUP_TYPE - 5种清理类型枚举DATA_CATEGORY - 5种数据分类枚举TASK_STATUS - 7种任务状态枚举PLAN_TYPE - 5种计划类型枚举BACKUP_TYPE - 3种备份类型枚举COMPRESSION_TYPE - 3种压缩类型枚举BACKUP_STATUS - 3种备份状态枚举kku_cleanup_configs - 清理配置表kku_cleanup_plans - 清理计划表kku_cleanup_plan_contents - 计划内容表kku_cleanup_tasks - 清理任务表kku_cleanup_backups - 备份记录表kku_cleanup_backup_files - 备份文件表kku_cleanup_logs - 清理日志表kku_cleanup_table_stats - 表统计信息表CleanupConfig - 清理配置模型CleanupPlan - 清理计划模型CleanupPlanContent - 计划内容模型CleanupTask - 清理任务模型CleanupBackup - 备份记录模型CleanupService - 主服务类,提供完整的对外接口ModelScannerLogic - Model扫描逻辑,支持自动识别和配置生成TableScannerLogic - 表扫描逻辑(已废弃,建议使用ModelScannerLogic)CleanupPlanLogic - 计划管理逻辑CleanupTaskLogic - 任务管理逻辑CleanupExecutorLogic - 清理执行逻辑,支持Model类和表名双模式BackupLogic - 备份管理逻辑ScanTablesCommand - 表扫描命令,支持强制刷新和详细显示CleanupDataCommand - 数据清理命令,提供完整的命令行接口CleanupService - 主服务类cleanup.php - 完整的配置文件CleanupServiceProvider - 服务提供者| 组件类别 | 完成度 | 说明 |
|---|---|---|
| 枚举定义 | 100% | 7个枚举类,完整实现 |
| 数据库设计 | 100% | 8个表,完整SQL结构 |
| 模型层 | 100% | 5个核心模型,完整实现 |
| 服务层 | 80% | 主服务完成,4个逻辑类待实现 |
| 命令行工具 | 100% | 2个命令,功能完整 |
| 服务层 | 100% | 主服务类和服务提供者完整 |
| Dcat Admin后台 | 0% | 后台管理界面待开发 |
| 配置文件 | 100% | 详细配置,支持环境变量 |
| 服务注册 | 100% | 完整的服务提供者 |
总体完成度: 75%
Cleanup 模块是一个专门用于数据清理的系统模块,提供灵活的数据清理配置和执行功能。该模块主要用于测试环境的数据清理,支持清除所有模块的运行数据,同时保留配置数据。
首先扫描系统中的所有数据表,生成默认配置:
# 扫描数据表并生成配置
php artisan cleanup:scan-tables
# 强制重新扫描
php artisan cleanup:scan-tables --force
通过后台管理界面创建清理计划:
/admin/cleanup/plans 创建清理计划# 自定义选择特定表
--tables=kku_farm_users,kku_item_users,kku_pet_logs
# 按模块选择,排除配置表
--modules=Farm,GameItems --exclude-tables=kku_farm_configs
# 按分类选择日志数据
--categories=2 --exclude-modules=Config
# 混合选择
--modules=Farm --categories=2 --tables=kku_pet_users
每个表都可以制定不同的清理方式:
# 配置农场用户表:按时间删除,保留90天
php artisan cleanup:config-table 1 kku_farm_users \
--type=3 --time-field=created_at --before="90_days_ago" --backup --priority=100
# 配置物品用户表:按用户删除指定测试用户
php artisan cleanup:config-table 1 kku_item_users \
--type=4 --user-field=user_id --users=1001,1002,1003 --backup --priority=150
# 配置缓存表:直接清空,无需备份
php artisan cleanup:config-table 1 kku_cache \
--type=1 --no-backup --priority=400
# 基于计划创建任务
php artisan cleanup:create-task 1 --execute
# 预览清理结果(不实际执行)
php artisan cleanup:execute 1 --dry-run
# 执行清理任务(会自动创建备份)
php artisan cleanup:execute 1
# 强制执行(跳过确认)
php artisan cleanup:execute 1 --force
# 创建数据备份
php artisan cleanup:backup 1 --type=sql --compression=gzip
# 恢复数据备份
php artisan cleanup:restore 123
app/Module/Cleanup/
├── AdminControllers/ # 后台管理控制器
│ ├── CleanupConfigController.php # 清理配置管理
│ ├── CleanupTaskController.php # 清理任务管理
│ ├── CleanupLogController.php # 清理日志管理
│ └── Helper/ # 辅助类
├── Commands/ # 命令行工具
│ ├── CleanupDataCommand.php # 数据清理命令
│ └── ScanTablesCommand.php # 扫描数据表命令
├── Databases/ # 数据库相关文件
│ └── GenerateSql/ # SQL生成文件
├── Docs/ # 文档目录
│ ├── 设计概述.md # 设计概述
│ ├── 数据库设计.md # 数据库设计
│ ├── 功能需求.md # 功能需求
│ └── 接口设计.md # 接口设计
├── Enums/ # 枚举定义
│ ├── CLEANUP_TYPE.php # 清理类型枚举
│ ├── DATA_CATEGORY.php # 数据分类枚举
│ └── TASK_STATUS.php # 任务状态枚举
├── Events/ # 事件类
├── Listeners/ # 事件监听器
├── Logics/ # 业务逻辑类
│ ├── ModelScannerLogic.php # Model扫描逻辑(推荐)
│ ├── TableScannerLogic.php # 表扫描逻辑(已废弃)
│ ├── CleanupExecutorLogic.php # 清理执行逻辑
│ ├── CleanupPlanLogic.php # 计划管理逻辑
│ ├── CleanupTaskLogic.php # 任务管理逻辑
│ └── BackupLogic.php # 备份管理逻辑
├── Models/ # 数据模型
│ ├── CleanupConfig.php # 清理配置模型
│ ├── CleanupTask.php # 清理任务模型
│ └── CleanupLog.php # 清理日志模型
├── Providers/ # 服务提供者
│ └── CleanupServiceProvider.php # 模块服务提供者
├── Repositories/ # 数据仓库
├── Services/ # 服务类
│ ├── CleanupService.php # 清理服务
│ └── ConfigService.php # 配置服务
├── Validations/ # 验证规则
├── Validators/ # 验证器
└── README.md # 本文档
直接指定要清理的表列表,完全自由选择:
{
"selection_type": "custom",
"tables": ["kku_farm_users", "kku_item_users", "kku_pet_logs"]
}
选择一个或多个模块的所有表:
{
"selection_type": "module",
"modules": ["Farm", "GameItems"],
"exclude_tables": ["kku_farm_configs"]
}
按数据分类选择表:
{
"selection_type": "category",
"categories": [1, 2], // 用户数据和日志数据
"exclude_modules": ["Config"]
}
选择所有表,但可排除特定内容:
{
"selection_type": "all",
"exclude_categories": [5], // 排除配置数据
"exclude_tables": ["kku_user_profiles"]
}
组合多种选择方式:
{
"selection_type": "mixed",
"modules": ["Farm"],
"categories": [2],
"tables": ["kku_pet_users"],
"exclude_tables": ["kku_farm_configs"]
}
kku_cache, kku_sessionskku_user_sessionskku_farm_logs, kku_user_actionlogskku_farm_users, kku_item_usersuse App\Module\Cleanup\Services\ConfigService;
// 获取所有配置
$configs = ConfigService::getCleanupConfigs();
// 更新特定表的配置
ConfigService::updateCleanupConfig('kku_farm_users', [
'cleanup_type' => 3, // 按时间删除
'conditions' => [
'time_field' => 'created_at',
'time_condition' => 'older_than',
'time_value' => 30,
'time_unit' => 'days'
],
'is_enabled' => true,
'priority' => 100
]);
use App\Module\Cleanup\Services\CleanupService;
// 创建清理任务
$result = CleanupService::createCleanupTask([
'task_name' => '测试数据清理',
'cleanup_type' => 2, // 模块清理
'target_modules' => ['Farm', 'GameItems'],
'conditions' => [
'exclude_tables' => ['kku_farm_configs']
]
]);
// 预览清理结果
$preview = CleanupService::previewCleanup($taskId);
// 执行清理
$result = CleanupService::executeCleanup($taskId);
use App\Module\Cleanup\Services\PlanService;
// 配置农场用户表:按时间删除
PlanService::configureTable(1, 'kku_farm_users', [
'cleanup_type' => 3,
'conditions' => [
'time_field' => 'created_at',
'before' => '90_days_ago'
],
'priority' => 100,
'backup_enabled' => true,
'notes' => '保留90天内的农场用户数据'
]);
// 配置物品表:按用户删除
PlanService::configureTable(1, 'kku_item_users', [
'cleanup_type' => 4,
'conditions' => [
'user_field' => 'user_id',
'user_condition' => 'in',
'user_values' => [1001, 1002, 1003]
],
'priority' => 150,
'backup_enabled' => true,
'notes' => '清理指定测试用户的物品数据'
]);
// 配置缓存表:直接清空
PlanService::configureTable(1, 'kku_cache', [
'cleanup_type' => 1,
'conditions' => null,
'priority' => 400,
'backup_enabled' => false,
'notes' => '缓存表直接清空,无需备份'
]);
use App\Module\Cleanup\Services\CleanupService;
// 创建备份(默认为数据库备份)
$backupResult = CleanupService::createPlanBackup($planId, [
'backup_type' => 1, // 1=数据库备份, 2=SQL文件, 3=JSON文件, 4=CSV文件
'compression' => 'gzip',
'include_structure' => true
]);
// 获取SQL备份列表
$sqlBackups = CleanupService::getSqlBackups([
'table_name' => 'kku_farm_users',
'min_records' => 100,
'order_by' => 'created_at',
'order_direction' => 'desc'
]);
// 获取SQL备份详情
$backupDetail = CleanupService::getSqlBackupDetail($sqlBackupId);
// 获取SQL备份内容
$backupContent = CleanupService::getSqlBackupContent($sqlBackupId);
// 恢复备份
$restoreResult = CleanupService::restoreBackup($backupId);
访问以下URL进行后台管理:
/admin/cleanup/plans - 清理计划管理/admin/cleanup/plans/{id}/contents - 计划内容和表级配置/admin/cleanup/tasks - 清理任务管理/admin/cleanup/logs - 清理日志查看/admin/cleanup/backups - 数据备份管理/admin/cleanup/reports - 统计报告清理任务执行失败
清理速度过慢
权限不足
# 查看清理日志
tail -f storage/logs/cleanup.log
# 查看任务状态
php artisan cleanup:status [task_id]
CLEANUP_TYPE 枚举中添加新类型CleanupExecutorLogic 中实现清理逻辑DATA_CATEGORY 枚举中添加新分类