diff --git a/backend/app/Controllers/CurriculumController.php b/backend/app/Controllers/CurriculumController.php index 8219a62..52b76b6 100644 --- a/backend/app/Controllers/CurriculumController.php +++ b/backend/app/Controllers/CurriculumController.php @@ -46,17 +46,58 @@ class CurriculumController ]; file_put_contents($processingDir . '/' . $taskId . '.json', json_encode($taskState, JSON_UNESCAPED_UNICODE)); - // Launch Background Worker + // Prepare task paths $scriptPath = realpath(__DIR__ . '/../../scripts/curriculum_worker.php'); $logPath = $processingDir . '/' . $taskId . '.log'; - $cmd = "php " . escapeshellarg($scriptPath) . " " . escapeshellarg($taskId) . " > " . escapeshellarg($logPath) . " 2>&1 &"; - exec($cmd); + // Send processing response immediately $response->json([ 'status' => 'processing', 'task_id' => $taskId, 'message' => 'تم استلام الكتاب، وجاري المعالجة المعمقة بواسطة الذكاء الاصطناعي. يرجى الانتظار...' ]); + + // Terminate request and flush buffers so the client receives the JSON immediately + if (function_exists('fastcgi_finish_request')) { + fastcgi_finish_request(); + } else { + // Fallback for non-FPM + ob_end_flush(); + flush(); + } + + // Run worker natively in the same PHP process (in the background from client's perspective) + try { + // Capture all output to log file + ini_set('display_errors', 1); + error_reporting(E_ALL); + ob_start(); + + // Execute the script logic natively by setting argv + $argv = ['curriculum_worker.php', $taskId]; + $_SERVER['argv'] = $argv; + + require $scriptPath; + + $output = ob_get_clean(); + file_put_contents($logPath, $output, FILE_APPEND); + } catch (\Exception $e) { + $err = "[Exception] " . $e->getMessage() . " in " . $e->getFile() . ":" . $e->getLine(); + file_put_contents($logPath, $err . " +", FILE_APPEND); + + // Update state file to error + $stateFile = $processingDir . '/' . $taskId . '.json'; + if (file_exists($stateFile)) { + $st = json_decode(file_get_contents($stateFile), true); + $st['status'] = 'error'; + $st['message'] = 'تعطل النظام أثناء التحليل (Crash). يرجى مراجعة سجل الأخطاء.'; + file_put_contents($stateFile, json_encode($st, JSON_UNESCAPED_UNICODE)); + } + } + + // Ensure script stops here so it doesn't return anything else + exit; } /** @@ -82,9 +123,34 @@ class CurriculumController $response->json($state); } + + /** + * Get Upload Log + */ + public function getUploadLog(Request $request, Response $response): void + { + $taskId = $request->getQueryParams()['task_id'] ?? ''; + if (empty($taskId)) { + $response->status(400)->json(['status' => 'error', 'message' => 'رقم المهمة مفقود']); + return; + } + + $processingDir = __DIR__ . '/../../storage/curriculum/processing'; + $logFile = $processingDir . '/' . $taskId . '.log'; + + if (!file_exists($logFile)) { + $response->status(404)->json(['status' => 'error', 'message' => 'سجل الأخطاء غير موجود']); + return; + } + + $logContent = file_get_contents($logFile); + $response->json(['status' => 'success', 'log' => $logContent]); + } + /** * Get Complete Live Tree */ + public function getTree(Request $request, Response $response): void { $response->json([ diff --git a/backend/app/Views/CurriculumStudio.php b/backend/app/Views/CurriculumStudio.php index 3da2e31..5bfe951 100644 --- a/backend/app/Views/CurriculumStudio.php +++ b/backend/app/Views/CurriculumStudio.php @@ -450,8 +450,43 @@ class CurriculumStudio
جاري التهيئة...
0%
+ + + + + +