diff --git a/src/Manager/WiktionaryManager.php b/src/Manager/WiktionaryManager.php index 172546745890f00a25074eaf1bd8127270776e54..5984e0f0b2c5cb233ecfe4ebdc364c3df7ba10ef 100644 --- a/src/Manager/WiktionaryManager.php +++ b/src/Manager/WiktionaryManager.php @@ -14,6 +14,7 @@ use Symfony\Component\Serializer\SerializerAwareInterface; use Symfony\Component\Serializer\SerializerInterface; use Symfony\Component\HttpClient\HttpClient; use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; +use Symfony\Component\HttpClient\Exception\ClientException; class WiktionaryManager { @@ -71,30 +72,63 @@ class WiktionaryManager $url = 'http://localhost:5000/search/' . $language . '/' . $language . '/' . $word . '/a_wikstraktor'; $client = HttpClient::create(); - // Make the initial request with a timeout - //try { - $response = $client->request('GET', $url, ['timeout' => 2.5]); +// Function to restart the server + function restartServer() { + $startServerCommand = 'nohup /var/www/live-query-wiktextract/venv/bin/python3 /var/www/live-query-wiktextract/src/app.py'; + exec($startServerCommand); + } + +// Make the initial request with a timeout + try { + $response = $client->request('GET', $url, ['timeout' => 5]); // Check the HTTP status code if ($response->getStatusCode() === 200) { // Server is up and returned a successful response $result = $response->getContent(); + } else { + // Handle other cases, if needed + $result = null; } - /*} catch (TransportExceptionInterface $exception) { - // Handle timeout for the initial request - $startServerCommand = '/var/www/live-query-wiktextract/venv/bin/python3 /var/www/live-query-wiktextract/src/app.py'; - exec($startServerCommand); + + } catch (TransportExceptionInterface $exception) { + // Catch the exception and check if it's a 404 response + if ($exception->getResponse() && $exception->getResponse()->getStatusCode() === 404) { + // Handle cases where the word is not found + $result = null; + } elseif (empty(exec('ps aux | grep "[a]pp.py"'))) { + restartServer(); + $response = $client->request('GET', $url, ['timeout' => 5]); + + // Check the HTTP status code + if ($response->getStatusCode() === 200) { + // Server is up and returned a successful response + $result = $response->getContent(); + } else { + // Handle cases where the word is not found + $result = null; + // Log or handle the error as needed + } + } else { + // Handle cases where the server is still not responding + $result = null; + // Log or handle the error as needed + } + } catch (Exception $e) { + // Handle other exceptions + dump($e->getResponse()->getContent()); die(); } + + + // Stream responses with a timeout - foreach ($client->stream([$client->request('GET', $url)], 1.5) as $response => $chunk) { + foreach ($client->stream([$client->request('GET', $url)], 5) as $response => $chunk) { if ($chunk->isTimeout()) { // Handle timeout for streaming responses - $startServerCommand = '/var/www/live-query-wiktextract/venv/bin/python3 /var/www/live-query-wiktextract/src/app.py'; - exec($startServerCommand); + restartServer(); } - }*/ - + } //dump(gettype($result));dump($url);dump(microtime(true)-$time_start);dump($result);/*Debug*/ // Output and error handling