워커 수정
This commit is contained in:
@@ -26,6 +26,14 @@ class NaverWorker extends BaseCommand
|
||||
helper(['log', 'redis']); // redis helper 추가
|
||||
|
||||
$this->db = \Config\Database::connect();
|
||||
|
||||
// 워커 시작 시점에 선제적으로 연결 상태를 보정
|
||||
try {
|
||||
$this->db->initialize();
|
||||
} catch (\Throwable $e) {
|
||||
CLI::error('Database connection init failed: ' . $e->getMessage());
|
||||
}
|
||||
|
||||
$logModel = model(NaverWorkerLogModel::class);
|
||||
$naverService = new \App\Services\NaverService(); // 서비스 생성
|
||||
|
||||
@@ -107,12 +115,13 @@ class NaverWorker extends BaseCommand
|
||||
'raw_payload' => $rawData,
|
||||
'status' => 'INIT'
|
||||
]);
|
||||
} catch (\CodeIgniter\Database\Exceptions\DatabaseException $e) {
|
||||
// MySQL gone away 에러 시 재연결 후 재시도
|
||||
if (strpos($e->getMessage(), 'MySQL server has gone away') !== false) {
|
||||
} catch (\Throwable $e) {
|
||||
// MySQL 연결 계열 에러 시 재연결 후 재시도
|
||||
if ($this->isMySqlConnectionError($e)) {
|
||||
CLI::write(CLI::color('⚠️ MySQL gone away, reconnecting...', 'yellow'));
|
||||
$this->db->close();
|
||||
$this->db = \Config\Database::connect();
|
||||
$this->db->initialize();
|
||||
$logModel = model(NaverWorkerLogModel::class);
|
||||
|
||||
// 재시도
|
||||
@@ -188,17 +197,18 @@ class NaverWorker extends BaseCommand
|
||||
}
|
||||
|
||||
/**
|
||||
* MySQL gone away 에러 발생 시 재연결 후 재시도하는 안전한 update
|
||||
* MySQL 연결 계열 에러 발생 시 재연결 후 재시도하는 안전한 update
|
||||
*/
|
||||
protected function safeUpdateLog($logModel, $logId, $data)
|
||||
{
|
||||
try {
|
||||
return $logModel->update($logId, $data);
|
||||
} catch (\CodeIgniter\Database\Exceptions\DatabaseException $e) {
|
||||
if (strpos($e->getMessage(), 'MySQL server has gone away') !== false) {
|
||||
} catch (\Throwable $e) {
|
||||
if ($this->isMySqlConnectionError($e)) {
|
||||
CLI::write(CLI::color('⚠️ MySQL gone away on update, reconnecting...', 'yellow'));
|
||||
$this->db->close();
|
||||
$this->db = \Config\Database::connect();
|
||||
$this->db->initialize();
|
||||
$logModel = model(\App\Models\Entities\NaverWorkerLogModel::class);
|
||||
|
||||
// 재시도
|
||||
@@ -209,6 +219,18 @@ class NaverWorker extends BaseCommand
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* MySQL 연결 끊김 계열 에러 여부 판별
|
||||
*/
|
||||
protected function isMySqlConnectionError(\Throwable $e): bool
|
||||
{
|
||||
$message = strtolower($e->getMessage());
|
||||
|
||||
return str_contains($message, 'mysql server has gone away')
|
||||
|| str_contains($message, 'lost connection to mysql server')
|
||||
|| str_contains($message, 'server has gone away');
|
||||
}
|
||||
|
||||
/**
|
||||
* 폴백 파일에서 데이터 읽기 (Redis 장애 시 파일에서 직접 처리)
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user