fix: Run curriculum worker in true CLI background to prevent web server timeout at 20%

This commit is contained in:
Hamza-Ayed
2026-08-29 02:29:55 +03:00
parent 598467b58f
commit 3d09b8235d
@@ -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';