connect('redis', 6379); $redis->select(9); CLI::write(CLI::color('🟢 Naver Worker running...', 'green')); } catch (\Exception $e) { CLI::error("Redis 연결 불가: " . $e->getMessage()); return; } while (true) { $result = $redis->brPop(['naver:raw_queue'], 30); if (!$result) { // 데이터가 없어서 타임아웃 난 경우. // 굳이 sleep 안 해도 바로 다음 brPop이 다시 30초 대기를 시작함. continue; } if ($result) { $rawData = $result[1]; // [1] 꺼내자마자 DB에 원문 저장 (2차 임시 저장) $logId = $logModel->insert([ 'raw_payload' => $rawData, 'status' => 'INIT' ]); try { $responseJson = json_decode($result[1], true); $payload = $responseJson['request_data'] ?? []; if (empty($payload)) { throw new \Exception("빈 페이로드 데이터"); } // 서비스의 함수 하나로 모든 처리 완료 $insertId = $naverService->processArticle($payload); // [3] 성공 시 로그 업데이트 $logModel->update($logId, [ 'atcl_no' => $payload['articleNumber'] ?? null, 'status' => 'SUCCESS', 'target_db_id' => $insertId ]); CLI::write("✅ Success! DB ID: $insertId", 'cyan'); } catch (\Exception $e) { CLI::error("❌ Task Failed: " . $e->getMessage()); // 실패 로그는 여기서 남김 helper('log'); write_custom_log("FAILED_DATA | Error: " . $e->getMessage(), 'ERROR', 'failed'); } } } } }