58 lines
1.9 KiB
PHP
58 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Commands;
|
|
|
|
use CodeIgniter\CLI\BaseCommand;
|
|
use CodeIgniter\CLI\CLI;
|
|
|
|
// 헬퍼 로드 (app/Helpers/log_helper.php 가 있어야 함 autoload 설정 넣어놓았음)
|
|
|
|
class NaverWorker extends BaseCommand
|
|
{
|
|
protected $group = 'Workers';
|
|
protected $name = 'naver:worker';
|
|
protected $description = 'Redis에서 데이터를 꺼내 DB에 저장하고 네이버 API를 호출합니다.';
|
|
|
|
public function run(array $params)
|
|
{
|
|
helper('log'); // 여기서 로드 완료!
|
|
|
|
$redis = new \Redis();
|
|
try {
|
|
$redis->connect('redis', 6379);
|
|
$redis->select(9);
|
|
CLI::write(CLI::color('🟢 Naver Worker running...', 'green'));
|
|
} catch (\Exception $e) {
|
|
CLI::error("Redis 연결 불가: " . $e->getMessage());
|
|
return;
|
|
}
|
|
|
|
$naverService = new \App\Services\NaverService(); // 서비스 생성
|
|
|
|
while (true) {
|
|
$result = $redis->brPop(['naver:raw_queue'], 30);
|
|
|
|
if ($result) {
|
|
try {
|
|
$responseJson = json_decode($result[1], true);
|
|
$payload = $responseJson['request_data'] ?? [];
|
|
|
|
if (empty($payload)) {
|
|
throw new \Exception("빈 페이로드 데이터");
|
|
}
|
|
|
|
// 서비스의 함수 하나로 모든 처리 완료
|
|
$insertId = $naverService->processArticle($payload);
|
|
|
|
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');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |