deepSeekService = $deepSeekService; } /** * Execute the console command. * * @return int */ public function handle() { set_time_limit(0); $taskId = (int)$this->argument('task_id'); $lockName = self::LOCK_PREFIX . $taskId; // 非阻塞抢占该任务的处理锁;抢不到说明已有其他进程正在处理 $acquired = DB::selectOne('SELECT GET_LOCK(?, 0) AS acquired', [$lockName]); if (!$acquired || (int)$acquired->acquired !== 1) { dLog('command')->info("task_id: {$taskId} 正在被其他进程处理,本次直接退出"); return 0; } dLog('command')->info("task_id: {$taskId} 抢占处理锁成功,开始处理..."); try { $result = $this->deepSeekService->runScriptGenerateTaskAsync($taskId); dLog('command')->info('task_id: ' . $taskId . ' 处理结果: ' . json_encode($result, JSON_UNESCAPED_UNICODE)); if (empty($result['ok'])) { logDB('command', 'error', '剧本资产生成worker异常', [ 'task_id' => $taskId, 'status' => $result['status'] ?? null, 'error' => $result['error'] ?? '未知错误', ]); } } catch (\Throwable $e) { dLog('command')->error("task_id: {$taskId} 处理异常: " . $e->getMessage()); logDB('command', 'error', '剧本资产生成worker异常', [ 'task_id' => $taskId, 'error' => $e->getMessage(), ]); } finally { // 显式释放处理锁;即使进程异常退出,连接断开后 MySQL 也会自动释放 DB::statement('SELECT RELEASE_LOCK(?)', [$lockName]); dLog('command')->info("task_id: {$taskId} 处理结束,已释放处理锁"); } return 0; } }