Update: 2026-05-08 14:47:40
This commit is contained in:
@@ -58,26 +58,22 @@ $stmt = $db->prepare("
|
|||||||
");
|
");
|
||||||
$stmt->execute([$batchId]);
|
$stmt->execute([$batchId]);
|
||||||
|
|
||||||
// 3. If it's a single invoice, try processing it SYNCHRONOUSLY right now!
|
// 3. Trigger the worker via a "Fire and Forget" HTTP request
|
||||||
if ($batch['total_images'] == 1) {
|
// This ensures processing starts immediately but the mobile app doesn't timeout.
|
||||||
// We need the queue ID for this batch
|
$workerUrl = (isset($_SERVER['HTTPS']) ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . '/api/v1/batches/process-worker';
|
||||||
$queueStmt = $db->prepare("SELECT id FROM invoice_processing_queue WHERE batch_id = ? AND status = 'pending' LIMIT 1");
|
$postData = json_encode(['batch_id' => $batchId]);
|
||||||
$queueStmt->execute([$batchId]);
|
|
||||||
$queueId = $queueStmt->fetchColumn();
|
|
||||||
|
|
||||||
if ($queueId) {
|
$ch = curl_init($workerUrl);
|
||||||
InvoiceProcessor::processQueueItem((int)$queueId);
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
|
||||||
}
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
|
||||||
} else {
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
// For multiple invoices, try triggering the worker in the background
|
curl_setopt($ch, CURLOPT_TIMEOUT, 1); // Only wait 1 second
|
||||||
$workerPath = ROOT_PATH . '/app/cron/process_batches.php';
|
curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
|
||||||
$logPath = STORAGE_PATH . '/logs/cron.log';
|
@curl_exec($ch);
|
||||||
// Mute exec since it might fail depending on server config
|
curl_close($ch);
|
||||||
@exec("php " . escapeshellarg($workerPath) . " >> " . escapeshellarg($logPath) . " 2>&1 &");
|
|
||||||
}
|
|
||||||
|
|
||||||
json_success([
|
json_success([
|
||||||
'batch_id' => $batchId,
|
'batch_id' => $batchId,
|
||||||
'status' => 'processing',
|
'status' => 'processing',
|
||||||
'total_images' => $batch['total_images']
|
'total_images' => $batch['total_images']
|
||||||
], 'تم إنهاء الدفعة بنجاح وإرسالها للمعالجة');
|
], 'تم إنهاء الدفعة بنجاح وبدء المعالجة الفورية');
|
||||||
|
|||||||
38
app/modules_app/batches/process_worker.php
Normal file
38
app/modules_app/batches/process_worker.php
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Background Worker Trigger (HTTP)
|
||||||
|
* POST /api/v1/batches/process-worker
|
||||||
|
*
|
||||||
|
* This endpoint is triggered by finalize.php to start processing in the background.
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
require_once __DIR__ . '/../../../bootstrap/init.php';
|
||||||
|
|
||||||
|
use App\Services\InvoiceProcessor;
|
||||||
|
use App\Core\Database;
|
||||||
|
|
||||||
|
// 1. Ignore user abort and set no time limit
|
||||||
|
ignore_user_abort(true);
|
||||||
|
set_time_limit(0);
|
||||||
|
|
||||||
|
// 2. Get batch ID
|
||||||
|
$data = json_decode(file_get_contents('php://input'), true);
|
||||||
|
$batchId = $data['batch_id'] ?? null;
|
||||||
|
|
||||||
|
if (!$batchId) {
|
||||||
|
exit('No batch ID');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Process all pending items for this batch
|
||||||
|
$db = Database::getInstance();
|
||||||
|
$stmt = $db->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";
|
||||||
Reference in New Issue
Block a user