From 3ab2258b1bea8414e0de32042a46cce255281ac8 Mon Sep 17 00:00:00 2001 From: mahdahar <89adham@gmail.com> Date: Wed, 11 Mar 2026 09:43:40 +0700 Subject: [PATCH] fix: reduce spooler timeout to prevent batch processing hang - Reduce cURL timeout from 10s to 3s total (2s connection) - Add better error messages for spooler failures - Continue processing remaining items if one fails - Prevents getting stuck when spooler is slow/unreachable --- app/Controllers/Pages/SuperuserController.php | 4 ++-- app/Libraries/PdfBatchService.php | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/app/Controllers/Pages/SuperuserController.php b/app/Controllers/Pages/SuperuserController.php index 00d60d3..8495c75 100644 --- a/app/Controllers/Pages/SuperuserController.php +++ b/app/Controllers/Pages/SuperuserController.php @@ -51,7 +51,7 @@ class SuperuserController extends BaseController $results = []; $db = \Config\Database::connect(); - foreach ($accessNumbers as $accessnumber) { + foreach ($accessNumbers as $index => $accessnumber) { $accessnumber = trim($accessnumber); if (empty($accessnumber)) { continue; @@ -73,7 +73,7 @@ class SuperuserController extends BaseController continue; } - // Call report/{accessnumber}/pdf endpoint internally + // Call PDF generation service $response = $this->callReportPdfEndpoint($accessnumber); if ($response['success']) { diff --git a/app/Libraries/PdfBatchService.php b/app/Libraries/PdfBatchService.php index fce949b..0863103 100644 --- a/app/Libraries/PdfBatchService.php +++ b/app/Libraries/PdfBatchService.php @@ -105,17 +105,27 @@ class PdfBatchService curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json' ]); - curl_setopt($ch, CURLOPT_TIMEOUT, 10); + curl_setopt($ch, CURLOPT_TIMEOUT, 3); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + $curlError = curl_error($ch); curl_close($ch); + if ($curlError) { + throw new \Exception('Spooler connection failed: ' . $curlError); + } + if ($httpCode !== 200) { - throw new \Exception('Failed to queue PDF generation'); + throw new \Exception('Spooler returned HTTP ' . $httpCode); } $data = json_decode($response, true); + if (!isset($data['jobId'])) { + throw new \Exception('Invalid spooler response'); + } + return $data['jobId']; } }