| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533 |
- <?php
- namespace App\Module\Cleanup\Logics;
- use App\Module\Cleanup\Models\CleanupTask;
- use App\Module\Cleanup\Models\CleanupPlan;
- use App\Module\Cleanup\Models\CleanupBackup;
- use App\Module\Cleanup\Enums\TASK_STATUS;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- /**
- * 清理任务管理逻辑类
- *
- * 负责清理任务的创建、管理和状态控制
- */
- class CleanupTaskLogic
- {
- /**
- * 基于计划创建清理任务
- *
- * @param int $planId 计划ID
- * @param array $taskOptions 任务选项
- * @return array 创建结果
- */
- public static function createTask(int $planId, array $taskOptions = []): array
- {
- try {
- DB::beginTransaction();
- $plan = CleanupPlan::with('contents')->findOrFail($planId);
-
- if (!$plan->is_enabled) {
- throw new \Exception('计划已禁用,无法创建任务');
- }
- if ($plan->contents->isEmpty()) {
- throw new \Exception('计划没有配置内容,无法创建任务');
- }
- // 验证任务选项
- $validatedOptions = static::validateTaskOptions($taskOptions);
-
- // 统计任务信息
- $enabledContents = $plan->contents->where('is_enabled', true);
- $totalTables = $enabledContents->count();
-
- if ($totalTables === 0) {
- throw new \Exception('计划中没有启用的表配置,无法创建任务');
- }
- // 创建任务
- $task = CleanupTask::create([
- 'task_name' => $validatedOptions['task_name'] ?? "清理任务 - {$plan->plan_name}",
- 'plan_id' => $planId,
- 'status' => TASK_STATUS::PENDING->value,
- 'progress' => 0,
- 'current_step' => '准备中',
- 'total_tables' => $totalTables,
- 'processed_tables' => 0,
- 'total_records' => 0,
- 'deleted_records' => 0,
- 'backup_size' => 0,
- 'execution_time' => 0,
- 'backup_time' => 0,
- 'created_by' => $validatedOptions['created_by'] ?? 0,
- ]);
- DB::commit();
- return [
- 'success' => true,
- 'message' => '清理任务创建成功',
- 'data' => [
- 'task_id' => $task->id,
- 'task_name' => $task->task_name,
- 'plan_name' => $plan->plan_name,
- 'total_tables' => $totalTables,
- 'status' => TASK_STATUS::from($task->status)->getDescription(),
- ]
- ];
- } catch (\Exception $e) {
- DB::rollBack();
- Log::error('创建清理任务失败', [
- 'plan_id' => $planId,
- 'task_options' => $taskOptions,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return [
- 'success' => false,
- 'message' => '创建清理任务失败: ' . $e->getMessage(),
- 'data' => null
- ];
- }
- }
- /**
- * 更新任务状态
- *
- * @param int $taskId 任务ID
- * @param TASK_STATUS $status 新状态
- * @param array $updateData 更新数据
- * @return array 更新结果
- */
- public static function updateTaskStatus(int $taskId, TASK_STATUS $status, array $updateData = []): array
- {
- try {
- $task = CleanupTask::findOrFail($taskId);
-
- $data = array_merge($updateData, [
- 'status' => $status->value,
- ]);
- // 根据状态设置时间戳
- switch ($status) {
- case TASK_STATUS::RUNNING:
- $data['started_at'] = now();
- break;
- case TASK_STATUS::BACKING_UP:
- $data['started_at'] = $data['started_at'] ?? now();
- break;
- case TASK_STATUS::COMPLETED:
- $data['completed_at'] = now();
- $data['progress'] = 100;
- break;
- case TASK_STATUS::FAILED:
- case TASK_STATUS::CANCELLED:
- $data['completed_at'] = now();
- break;
- }
- $task->update($data);
- return [
- 'success' => true,
- 'message' => '任务状态更新成功',
- 'data' => [
- 'task_id' => $task->id,
- 'status' => $status->getDescription(),
- 'progress' => $task->progress,
- ]
- ];
- } catch (\Exception $e) {
- Log::error('更新任务状态失败', [
- 'task_id' => $taskId,
- 'status' => $status->value,
- 'update_data' => $updateData,
- 'error' => $e->getMessage()
- ]);
- return [
- 'success' => false,
- 'message' => '更新任务状态失败: ' . $e->getMessage(),
- 'data' => null
- ];
- }
- }
- /**
- * 更新任务进度
- *
- * @param int $taskId 任务ID
- * @param int $processedTables 已处理表数
- * @param int $deletedRecords 已删除记录数
- * @param string $currentStep 当前步骤
- * @return array 更新结果
- */
- public static function updateTaskProgress(int $taskId, int $processedTables, int $deletedRecords, string $currentStep): array
- {
- try {
- $task = CleanupTask::findOrFail($taskId);
-
- $progress = $task->total_tables > 0 ? round(($processedTables / $task->total_tables) * 100, 2) : 0;
-
- $task->update([
- 'progress' => $progress,
- 'processed_tables' => $processedTables,
- 'deleted_records' => $deletedRecords,
- 'current_step' => $currentStep,
- ]);
- return [
- 'success' => true,
- 'data' => [
- 'task_id' => $task->id,
- 'progress' => $progress,
- 'processed_tables' => $processedTables,
- 'total_tables' => $task->total_tables,
- 'deleted_records' => $deletedRecords,
- 'current_step' => $currentStep,
- ]
- ];
- } catch (\Exception $e) {
- Log::error('更新任务进度失败', [
- 'task_id' => $taskId,
- 'processed_tables' => $processedTables,
- 'deleted_records' => $deletedRecords,
- 'current_step' => $currentStep,
- 'error' => $e->getMessage()
- ]);
- return [
- 'success' => false,
- 'message' => '更新任务进度失败: ' . $e->getMessage(),
- 'data' => null
- ];
- }
- }
- /**
- * 取消任务
- *
- * @param int $taskId 任务ID
- * @param string $reason 取消原因
- * @return array 取消结果
- */
- public static function cancelTask(int $taskId, string $reason = ''): array
- {
- try {
- $task = CleanupTask::findOrFail($taskId);
-
- // 检查任务状态
- $currentStatus = TASK_STATUS::from($task->status);
- if (in_array($currentStatus, [TASK_STATUS::COMPLETED, TASK_STATUS::FAILED, TASK_STATUS::CANCELLED])) {
- throw new \Exception('任务已完成或已取消,无法再次取消');
- }
- $task->update([
- 'status' => TASK_STATUS::CANCELLED->value,
- 'completed_at' => now(),
- 'error_message' => $reason ?: '用户取消',
- ]);
- return [
- 'success' => true,
- 'message' => '任务已取消',
- 'data' => [
- 'task_id' => $task->id,
- 'status' => TASK_STATUS::CANCELLED->getDescription(),
- ]
- ];
- } catch (\Exception $e) {
- Log::error('取消任务失败', [
- 'task_id' => $taskId,
- 'reason' => $reason,
- 'error' => $e->getMessage()
- ]);
- return [
- 'success' => false,
- 'message' => '取消任务失败: ' . $e->getMessage(),
- 'data' => null
- ];
- }
- }
- /**
- * 获取任务详情
- *
- * @param int $taskId 任务ID
- * @return array 任务详情
- */
- public static function getTaskDetails(int $taskId): array
- {
- try {
- $task = CleanupTask::with(['plan', 'backup'])->findOrFail($taskId);
-
- return [
- 'success' => true,
- 'data' => [
- 'task' => [
- 'id' => $task->id,
- 'task_name' => $task->task_name,
- 'status' => $task->status,
- 'status_name' => TASK_STATUS::from($task->status)->getDescription(),
- 'progress' => $task->progress,
- 'current_step' => $task->current_step,
- 'total_tables' => $task->total_tables,
- 'processed_tables' => $task->processed_tables,
- 'total_records' => $task->total_records,
- 'deleted_records' => $task->deleted_records,
- 'backup_size' => $task->backup_size,
- 'execution_time' => $task->execution_time,
- 'backup_time' => $task->backup_time,
- 'started_at' => $task->started_at,
- 'backup_completed_at' => $task->backup_completed_at,
- 'completed_at' => $task->completed_at,
- 'error_message' => $task->error_message,
- 'created_at' => $task->created_at,
- ],
- 'plan' => $task->plan ? [
- 'id' => $task->plan->id,
- 'plan_name' => $task->plan->plan_name,
- 'plan_type' => $task->plan->plan_type,
- 'description' => $task->plan->description,
- ] : null,
- 'backup' => $task->backup ? [
- 'id' => $task->backup->id,
- 'backup_name' => $task->backup->backup_name,
- 'backup_size' => $task->backup->backup_size,
- 'file_count' => $task->backup->file_count,
- 'status' => $task->backup->status,
- ] : null,
- ]
- ];
- } catch (\Exception $e) {
- Log::error('获取任务详情失败', [
- 'task_id' => $taskId,
- 'error' => $e->getMessage()
- ]);
- return [
- 'success' => false,
- 'message' => '获取任务详情失败: ' . $e->getMessage(),
- 'data' => null
- ];
- }
- }
- /**
- * 验证任务选项
- *
- * @param array $taskOptions 任务选项
- * @return array 验证后的选项
- */
- private static function validateTaskOptions(array $taskOptions): array
- {
- return [
- 'task_name' => $taskOptions['task_name'] ?? null,
- 'created_by' => $taskOptions['created_by'] ?? 0,
- ];
- }
- /**
- * 启动任务执行
- *
- * @param int $taskId 任务ID
- * @return array 启动结果
- */
- public static function startTask(int $taskId): array
- {
- try {
- $task = CleanupTask::findOrFail($taskId);
- // 检查任务状态
- if ($task->status !== TASK_STATUS::PENDING->value) {
- return [
- 'success' => false,
- 'message' => '只有待执行状态的任务可以启动'
- ];
- }
- // 更新任务状态为执行中
- $task->update([
- 'status' => TASK_STATUS::RUNNING->value,
- 'started_at' => now(),
- 'current_step' => '任务启动中...'
- ]);
- return [
- 'success' => true,
- 'message' => '任务启动成功',
- 'data' => $task->fresh()
- ];
- } catch (\Exception $e) {
- return [
- 'success' => false,
- 'message' => '启动任务失败:' . $e->getMessage()
- ];
- }
- }
- /**
- * 暂停任务执行
- *
- * @param int $taskId 任务ID
- * @return array 暂停结果
- */
- public static function pauseTask(int $taskId): array
- {
- try {
- $task = CleanupTask::findOrFail($taskId);
- // 检查任务状态
- if (!in_array($task->status, [TASK_STATUS::BACKING_UP->value, TASK_STATUS::RUNNING->value])) {
- return [
- 'success' => false,
- 'message' => '只有备份中或执行中的任务可以暂停'
- ];
- }
- // 更新任务状态为已暂停
- $task->update([
- 'status' => TASK_STATUS::PAUSED->value,
- 'current_step' => '任务已暂停'
- ]);
- return [
- 'success' => true,
- 'message' => '任务暂停成功',
- 'data' => $task->fresh()
- ];
- } catch (\Exception $e) {
- return [
- 'success' => false,
- 'message' => '暂停任务失败:' . $e->getMessage()
- ];
- }
- }
- /**
- * 恢复任务执行
- *
- * @param int $taskId 任务ID
- * @return array 恢复结果
- */
- public static function resumeTask(int $taskId): array
- {
- try {
- $task = CleanupTask::findOrFail($taskId);
- // 检查任务状态
- if ($task->status !== TASK_STATUS::PAUSED->value) {
- return [
- 'success' => false,
- 'message' => '只有已暂停的任务可以恢复'
- ];
- }
- // 更新任务状态为执行中
- $task->update([
- 'status' => TASK_STATUS::RUNNING->value,
- 'current_step' => '任务恢复执行中...'
- ]);
- return [
- 'success' => true,
- 'message' => '任务恢复成功',
- 'data' => $task->fresh()
- ];
- } catch (\Exception $e) {
- return [
- 'success' => false,
- 'message' => '恢复任务失败:' . $e->getMessage()
- ];
- }
- }
- /**
- * 停止任务执行
- *
- * @param int $taskId 任务ID
- * @return array 停止结果
- */
- public static function stopTask(int $taskId): array
- {
- try {
- $task = CleanupTask::findOrFail($taskId);
- // 检查任务状态
- if (!in_array($task->status, [TASK_STATUS::BACKING_UP->value, TASK_STATUS::RUNNING->value, TASK_STATUS::PAUSED->value])) {
- return [
- 'success' => false,
- 'message' => '只有备份中、执行中或已暂停的任务可以停止'
- ];
- }
- // 更新任务状态为已取消
- $task->update([
- 'status' => TASK_STATUS::CANCELLED->value,
- 'finished_at' => now(),
- 'current_step' => '任务已停止',
- 'error_message' => '任务被手动停止'
- ]);
- return [
- 'success' => true,
- 'message' => '任务停止成功',
- 'data' => $task->fresh()
- ];
- } catch (\Exception $e) {
- return [
- 'success' => false,
- 'message' => '停止任务失败:' . $e->getMessage()
- ];
- }
- }
- /**
- * 获取任务进度
- *
- * @param int $taskId 任务ID
- * @return array 进度信息
- */
- public static function getTaskProgress(int $taskId): array
- {
- try {
- $task = CleanupTask::findOrFail($taskId);
- return [
- 'success' => true,
- 'data' => [
- 'task_id' => $task->id,
- 'status' => $task->status,
- 'progress' => $task->progress,
- 'current_step' => $task->current_step,
- 'total_tables' => $task->total_tables,
- 'processed_tables' => $task->processed_tables,
- 'deleted_records' => $task->deleted_records,
- 'started_at' => $task->started_at,
- 'finished_at' => $task->finished_at,
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'success' => false,
- 'message' => '获取任务进度失败:' . $e->getMessage()
- ];
- }
- }
- }
|