Update: 2026-05-08 15:02:13
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
* POST /v1/batches/finalize
|
||||
*
|
||||
* Marks a batch as ready for processing.
|
||||
* Triggers background processing (or processes synchronously depending on setup).
|
||||
* Sends instant response to mobile app, then processes in background via fastcgi_finish_request.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
@@ -40,8 +40,6 @@ if (!$batch || ($decoded['role'] !== 'super_admin' && $batch['tenant_id'] !== $t
|
||||
json_error('الدفعة غير موجودة', 404);
|
||||
}
|
||||
|
||||
|
||||
|
||||
if ($batch['status'] !== 'uploading') {
|
||||
json_error('تم إنهاء هذه الدفعة مسبقاً', 400);
|
||||
}
|
||||
@@ -58,11 +56,9 @@ $stmt = $db->prepare("
|
||||
");
|
||||
$stmt->execute([$batchId]);
|
||||
|
||||
// 3. Post-Response Processing (Background)
|
||||
// Manually send response instead of json_success() because it calls exit;
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
http_response_code(200);
|
||||
echo json_encode([
|
||||
// 3. Send response IMMEDIATELY to mobile app
|
||||
// We manually build the response instead of using json_success() because it calls exit()
|
||||
$responsePayload = json_encode([
|
||||
'success' => true,
|
||||
'data' => [
|
||||
'batch_id' => $batchId,
|
||||
@@ -73,22 +69,74 @@ echo json_encode([
|
||||
'timestamp' => date('c')
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
|
||||
// This allows the mobile app to get an instant success message,
|
||||
// while the server continues processing the AI extraction in the background.
|
||||
// Set headers
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
header('Content-Length: ' . strlen($responsePayload));
|
||||
http_response_code(200);
|
||||
|
||||
// Flush ALL output buffers to send response to client NOW
|
||||
echo $responsePayload;
|
||||
|
||||
// Flush PHP output buffers
|
||||
if (ob_get_level() > 0) {
|
||||
ob_end_flush();
|
||||
}
|
||||
flush();
|
||||
|
||||
// Log the API call for app.log (mimicking json_response behavior)
|
||||
$logEntry = sprintf(
|
||||
"API %s %s | 200 | OK | %s",
|
||||
$_SERVER['REQUEST_METHOD'] ?? 'CLI',
|
||||
$_SERVER['REQUEST_URI'] ?? '',
|
||||
'تم إنهاء الدفعة بنجاح وبدء المعالجة الفورية'
|
||||
);
|
||||
error_log($logEntry);
|
||||
@file_put_contents(
|
||||
STORAGE_PATH . '/logs/app.log',
|
||||
"[" . date('Y-m-d H:i:s') . "] " . $logEntry . "\n",
|
||||
FILE_APPEND
|
||||
);
|
||||
|
||||
// 4. Tell PHP-FPM: "The client response is done. But keep this PHP process alive."
|
||||
if (function_exists('fastcgi_finish_request')) {
|
||||
fastcgi_finish_request();
|
||||
}
|
||||
|
||||
// Ensure the script keeps running
|
||||
// 5. Now process in the background (client has already received the response)
|
||||
ignore_user_abort(true);
|
||||
set_time_limit(300); // Give it 5 minutes
|
||||
set_time_limit(300); // 5 minutes max
|
||||
|
||||
$queueStmt = $db->prepare("SELECT id FROM invoice_processing_queue WHERE batch_id = ? AND status = 'pending'");
|
||||
$queueStmt->execute([$batchId]);
|
||||
$items = $queueStmt->fetchAll();
|
||||
$bgLog = function(string $msg) {
|
||||
@file_put_contents(
|
||||
STORAGE_PATH . '/logs/worker.log',
|
||||
"[" . date('Y-m-d H:i:s') . "] [finalize-bg] " . $msg . "\n",
|
||||
FILE_APPEND
|
||||
);
|
||||
};
|
||||
|
||||
foreach ($items as $item) {
|
||||
InvoiceProcessor::processQueueItem((int)$item['id']);
|
||||
$bgLog("Background processing started for batch: $batchId");
|
||||
|
||||
try {
|
||||
$queueStmt = $db->prepare("SELECT id FROM invoice_processing_queue WHERE batch_id = ? AND status = 'pending' ORDER BY created_at ASC");
|
||||
$queueStmt->execute([$batchId]);
|
||||
$items = $queueStmt->fetchAll(\PDO::FETCH_COLUMN);
|
||||
|
||||
$bgLog("Found " . count($items) . " pending item(s) for batch $batchId");
|
||||
|
||||
foreach ($items as $queueId) {
|
||||
$bgLog("Processing queue item: $queueId");
|
||||
try {
|
||||
$success = InvoiceProcessor::processQueueItem((int)$queueId);
|
||||
$bgLog("Queue item $queueId: " . ($success ? "SUCCESS" : "FAILED"));
|
||||
} catch (\Throwable $e) {
|
||||
$bgLog("Queue item $queueId EXCEPTION: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
$bgLog("Background processing finished for batch: $batchId");
|
||||
|
||||
} catch (\Throwable $e) {
|
||||
$bgLog("FATAL ERROR in background processing: " . $e->getMessage());
|
||||
}
|
||||
|
||||
exit; // End here
|
||||
exit;
|
||||
|
||||
Reference in New Issue
Block a user