From 3d09b8235d277e8e25094b5850d40b00af1d34ef Mon Sep 17 00:00:00 2001 From: Hamza-Ayed Date: Sat, 29 Aug 2026 02:29:55 +0300 Subject: [PATCH] fix: Run curriculum worker in true CLI background to prevent web server timeout at 20% --- .../app/Controllers/CurriculumController.php | 24 ++++++------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/backend/app/Controllers/CurriculumController.php b/backend/app/Controllers/CurriculumController.php index 398ed4b..d5dad77 100644 --- a/backend/app/Controllers/CurriculumController.php +++ b/backend/app/Controllers/CurriculumController.php @@ -66,26 +66,16 @@ class CurriculumController flush(); } - // Run worker natively in the same PHP process (in the background from client's perspective) + // Run worker completely in the background via CLI try { - // Capture all output to log file - ini_set('display_errors', 1); - error_reporting(E_ALL); - ob_start(); + $cmd = "php " . escapeshellarg($scriptPath) . " " . escapeshellarg($taskId) . " > /dev/null 2>&1 &"; + exec($cmd); - // CRITICAL: Set $GLOBALS['argv'] so the required worker script can access $argv[1] - // (In a web request, $argv is empty — we must set it globally before requiring the worker) - $GLOBALS['argv'] = ['curriculum_worker.php', $taskId]; - $_SERVER['argv'] = $GLOBALS['argv']; - - require $scriptPath; - - $output = ob_get_clean(); - file_put_contents($logPath, $output, FILE_APPEND); + // Log that we spawned it + file_put_contents($logPath, "[System] Spawning background worker: $cmd\n", FILE_APPEND); } catch (\Exception $e) { - $err = "[Exception] " . $e->getMessage() . " in " . $e->getFile() . ":" . $e->getLine(); - file_put_contents($logPath, $err . " -", FILE_APPEND); + $err = "[Exception] " . $e->getMessage(); + file_put_contents($logPath, $err . "\n", FILE_APPEND); // Update state file to error $stateFile = $processingDir . '/' . $taskId . '.json';