From 0d8ff9a7b138d989e2448ba7275ad4b38d95127a Mon Sep 17 00:00:00 2001 From: Hamza-Ayed Date: Fri, 8 May 2026 14:47:40 +0300 Subject: [PATCH] Update: 2026-05-08 14:47:40 --- app/modules_app/batches/finalize.php | 30 ++++++++--------- app/modules_app/batches/process_worker.php | 38 ++++++++++++++++++++++ 2 files changed, 51 insertions(+), 17 deletions(-) create mode 100644 app/modules_app/batches/process_worker.php diff --git a/app/modules_app/batches/finalize.php b/app/modules_app/batches/finalize.php index 178d992..188712f 100644 --- a/app/modules_app/batches/finalize.php +++ b/app/modules_app/batches/finalize.php @@ -58,26 +58,22 @@ $stmt = $db->prepare(" "); $stmt->execute([$batchId]); -// 3. If it's a single invoice, try processing it SYNCHRONOUSLY right now! -if ($batch['total_images'] == 1) { - // We need the queue ID for this batch - $queueStmt = $db->prepare("SELECT id FROM invoice_processing_queue WHERE batch_id = ? AND status = 'pending' LIMIT 1"); - $queueStmt->execute([$batchId]); - $queueId = $queueStmt->fetchColumn(); +// 3. Trigger the worker via a "Fire and Forget" HTTP request +// This ensures processing starts immediately but the mobile app doesn't timeout. +$workerUrl = (isset($_SERVER['HTTPS']) ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . '/api/v1/batches/process-worker'; +$postData = json_encode(['batch_id' => $batchId]); - if ($queueId) { - InvoiceProcessor::processQueueItem((int)$queueId); - } -} else { - // For multiple invoices, try triggering the worker in the background - $workerPath = ROOT_PATH . '/app/cron/process_batches.php'; - $logPath = STORAGE_PATH . '/logs/cron.log'; - // Mute exec since it might fail depending on server config - @exec("php " . escapeshellarg($workerPath) . " >> " . escapeshellarg($logPath) . " 2>&1 &"); -} +$ch = curl_init($workerUrl); +curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); +curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); +curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); +curl_setopt($ch, CURLOPT_TIMEOUT, 1); // Only wait 1 second +curl_setopt($ch, CURLOPT_NOSIGNAL, 1); +@curl_exec($ch); +curl_close($ch); json_success([ 'batch_id' => $batchId, 'status' => 'processing', 'total_images' => $batch['total_images'] -], 'تم إنهاء الدفعة بنجاح وإرسالها للمعالجة'); +], 'تم إنهاء الدفعة بنجاح وبدء المعالجة الفورية'); diff --git a/app/modules_app/batches/process_worker.php b/app/modules_app/batches/process_worker.php new file mode 100644 index 0000000..91cb6cb --- /dev/null +++ b/app/modules_app/batches/process_worker.php @@ -0,0 +1,38 @@ +prepare("SELECT id FROM invoice_processing_queue WHERE batch_id = ? AND status = 'pending'"); +$stmt->execute([$batchId]); +$items = $stmt->fetchAll(); + +foreach ($items as $item) { + InvoiceProcessor::processQueueItem((int)$item['id']); +} + +echo "Done";