로그 생성이 안되어 테스트 및 수정본

This commit is contained in:
2026-03-20 09:33:30 +09:00
parent 80cb9451d2
commit bb07396abf
12 changed files with 205 additions and 23 deletions

View File

@@ -17,6 +17,9 @@ class NaverWorker extends BaseCommand
// DB 객체를 담을 변수 선언
protected $db;
protected $redisHost;
protected $redisPort;
protected $redisDatabase;
public function run(array $params)
{
@@ -29,13 +32,13 @@ class NaverWorker extends BaseCommand
$redis = new \Redis();
try {
// 두 가지 환경 변수 형식 지원 (REDIS_HOST 또는 redis.default.host)
$redisHost = getenv('REDIS_HOST') ?: (getenv('redis.default.host') ?: '127.0.0.1');
$redisPort = getenv('REDIS_PORT') ?: (getenv('redis.default.port') ?: 6379);
$redisDatabase = getenv('REDIS_DATABASE') ?: (getenv('redis.default.database') ?: 9);
$this->redisHost = getenv('REDIS_HOST') ?: (getenv('redis.default.host') ?: '127.0.0.1');
$this->redisPort = getenv('REDIS_PORT') ?: (getenv('redis.default.port') ?: 6379);
$this->redisDatabase = getenv('REDIS_DATABASE') ?: (getenv('redis.default.database') ?: 9);
$redis->connect($redisHost, (int)$redisPort);
$redis->select((int)$redisDatabase);
CLI::write(CLI::color('🟢 Naver Worker running... (Redis: ' . $redisHost . ':' . $redisPort . ' DB:' . $redisDatabase . ')', 'green'));
$redis->connect($this->redisHost, (int)$this->redisPort);
$redis->select((int)$this->redisDatabase);
CLI::write(CLI::color('🟢 Naver Worker running... (Redis: ' . $this->redisHost . ':' . $this->redisPort . ' DB:' . $this->redisDatabase . ')', 'green'));
} catch (\Exception $e) {
CLI::error("Redis 연결 불가: " . $e->getMessage());
return;
@@ -45,7 +48,37 @@ class NaverWorker extends BaseCommand
while (true) {
$result = $redis->brPop(['naver:raw_queue'], 30);
// Redis brPop with retry logic
$maxRetries = 3;
$retryCount = 0;
$result = null;
while ($retryCount < $maxRetries) {
try {
$result = $redis->brPop(['naver:raw_queue'], 30);
break; // 성공하면 루프 탈출
} catch (\RedisException $e) {
$retryCount++;
CLI::write(CLI::color("⚠️ Redis error (attempt {$retryCount}/{$maxRetries}): " . $e->getMessage(), 'yellow'));
if ($retryCount >= $maxRetries) {
CLI::error("❌ Redis reconnection failed after {$maxRetries} attempts");
break;
}
// Redis 재연결 시도
try {
CLI::write(CLI::color('🔄 Reconnecting to Redis...', 'yellow'));
$redis->close();
$redis->connect($this->redisHost, (int)$this->redisPort);
$redis->select((int)$this->redisDatabase);
CLI::write(CLI::color('✅ Redis reconnected', 'green'));
} catch (\Exception $reconnectError) {
CLI::error("Redis reconnection error: " . $reconnectError->getMessage());
sleep(2); // 재연결 실패 시 잠시 대기
}
}
}
if (!$result) {
// 데이터가 없어서 타임아웃 난 경우.

32
app/Commands/TestLog.php Normal file
View File

@@ -0,0 +1,32 @@
<?php
namespace App\Commands;
use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
class TestLog extends BaseCommand
{
protected $group = 'Test';
protected $name = 'test:log';
protected $description = 'Test log_message function';
public function run(array $params)
{
CLI::write('Testing log_message()...', 'yellow');
log_message('error', '===== TEST LOG MESSAGE FROM CLI ===== ' . date('Y-m-d H:i:s'));
log_message('debug', 'Debug level test');
log_message('info', 'Info level test');
$logFile = WRITEPATH . 'logs/log-' . date('Y-m-d') . '.log';
CLI::write('Log file: ' . $logFile);
CLI::write('Exists: ' . (file_exists($logFile) ? 'YES' : 'NO'));
if (file_exists($logFile)) {
CLI::write('Last 10 lines:');
CLI::write(shell_exec('tail -10 ' . escapeshellarg($logFile)));
}
}
}