fix: Run curriculum worker in true CLI background to prevent web server timeout at 20%
This commit is contained in:
@@ -66,26 +66,16 @@ class CurriculumController
|
|||||||
flush();
|
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 {
|
try {
|
||||||
// Capture all output to log file
|
$cmd = "php " . escapeshellarg($scriptPath) . " " . escapeshellarg($taskId) . " > /dev/null 2>&1 &";
|
||||||
ini_set('display_errors', 1);
|
exec($cmd);
|
||||||
error_reporting(E_ALL);
|
|
||||||
ob_start();
|
|
||||||
|
|
||||||
// CRITICAL: Set $GLOBALS['argv'] so the required worker script can access $argv[1]
|
// Log that we spawned it
|
||||||
// (In a web request, $argv is empty — we must set it globally before requiring the worker)
|
file_put_contents($logPath, "[System] Spawning background worker: $cmd\n", FILE_APPEND);
|
||||||
$GLOBALS['argv'] = ['curriculum_worker.php', $taskId];
|
|
||||||
$_SERVER['argv'] = $GLOBALS['argv'];
|
|
||||||
|
|
||||||
require $scriptPath;
|
|
||||||
|
|
||||||
$output = ob_get_clean();
|
|
||||||
file_put_contents($logPath, $output, FILE_APPEND);
|
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
$err = "[Exception] " . $e->getMessage() . " in " . $e->getFile() . ":" . $e->getLine();
|
$err = "[Exception] " . $e->getMessage();
|
||||||
file_put_contents($logPath, $err . "
|
file_put_contents($logPath, $err . "\n", FILE_APPEND);
|
||||||
", FILE_APPEND);
|
|
||||||
|
|
||||||
// Update state file to error
|
// Update state file to error
|
||||||
$stateFile = $processingDir . '/' . $taskId . '.json';
|
$stateFile = $processingDir . '/' . $taskId . '.json';
|
||||||
|
|||||||
Reference in New Issue
Block a user