Update: 2026-05-08 14:52:14
This commit is contained in:
@@ -58,22 +58,37 @@ $stmt = $db->prepare("
|
|||||||
");
|
");
|
||||||
$stmt->execute([$batchId]);
|
$stmt->execute([$batchId]);
|
||||||
|
|
||||||
// 3. Trigger the worker via a "Fire and Forget" HTTP request
|
// 3. Post-Response Processing (Background)
|
||||||
// This ensures processing starts immediately but the mobile app doesn't timeout.
|
// Manually send response instead of json_success() because it calls exit;
|
||||||
$workerUrl = (isset($_SERVER['HTTPS']) ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . '/api/v1/batches/process-worker';
|
header('Content-Type: application/json; charset=utf-8');
|
||||||
$postData = json_encode(['batch_id' => $batchId]);
|
http_response_code(200);
|
||||||
|
echo json_encode([
|
||||||
|
'success' => true,
|
||||||
|
'data' => [
|
||||||
|
'batch_id' => $batchId,
|
||||||
|
'status' => 'processing',
|
||||||
|
'total_images' => $batch['total_images']
|
||||||
|
],
|
||||||
|
'message' => 'تم إنهاء الدفعة بنجاح وبدء المعالجة الفورية',
|
||||||
|
'timestamp' => date('c')
|
||||||
|
], JSON_UNESCAPED_UNICODE);
|
||||||
|
|
||||||
$ch = curl_init($workerUrl);
|
// This allows the mobile app to get an instant success message,
|
||||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
|
// while the server continues processing the AI extraction in the background.
|
||||||
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
|
if (function_exists('fastcgi_finish_request')) {
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
fastcgi_finish_request();
|
||||||
curl_setopt($ch, CURLOPT_TIMEOUT, 1); // Only wait 1 second
|
}
|
||||||
curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
|
|
||||||
@curl_exec($ch);
|
|
||||||
curl_close($ch);
|
|
||||||
|
|
||||||
json_success([
|
// Ensure the script keeps running
|
||||||
'batch_id' => $batchId,
|
ignore_user_abort(true);
|
||||||
'status' => 'processing',
|
set_time_limit(300); // Give it 5 minutes
|
||||||
'total_images' => $batch['total_images']
|
|
||||||
], 'تم إنهاء الدفعة بنجاح وبدء المعالجة الفورية');
|
$queueStmt = $db->prepare("SELECT id FROM invoice_processing_queue WHERE batch_id = ? AND status = 'pending'");
|
||||||
|
$queueStmt->execute([$batchId]);
|
||||||
|
$items = $queueStmt->fetchAll();
|
||||||
|
|
||||||
|
foreach ($items as $item) {
|
||||||
|
InvoiceProcessor::processQueueItem((int)$item['id']);
|
||||||
|
}
|
||||||
|
|
||||||
|
exit; // End here
|
||||||
|
|||||||
Reference in New Issue
Block a user