Skip to content
Snippets Groups Projects
Commit 44a0f6ac authored by Enzo Simonnet's avatar Enzo Simonnet
Browse files

pb avec la gestion d'erreur

parent 51bfc942
No related branches found
No related tags found
No related merge requests found
...@@ -14,6 +14,7 @@ use Symfony\Component\Serializer\SerializerAwareInterface; ...@@ -14,6 +14,7 @@ use Symfony\Component\Serializer\SerializerAwareInterface;
use Symfony\Component\Serializer\SerializerInterface; use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\HttpClient\HttpClient; use Symfony\Component\HttpClient\HttpClient;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Component\HttpClient\Exception\ClientException;
class WiktionaryManager class WiktionaryManager
{ {
...@@ -71,30 +72,63 @@ class WiktionaryManager ...@@ -71,30 +72,63 @@ class WiktionaryManager
$url = 'http://localhost:5000/search/' . $language . '/' . $language . '/' . $word . '/a_wikstraktor'; $url = 'http://localhost:5000/search/' . $language . '/' . $language . '/' . $word . '/a_wikstraktor';
$client = HttpClient::create(); $client = HttpClient::create();
// Make the initial request with a timeout // Function to restart the server
//try { function restartServer() {
$response = $client->request('GET', $url, ['timeout' => 2.5]); $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 // Check the HTTP status code
if ($response->getStatusCode() === 200) { if ($response->getStatusCode() === 200) {
// Server is up and returned a successful response // Server is up and returned a successful response
$result = $response->getContent(); $result = $response->getContent();
} else {
// Handle other cases, if needed
$result = null;
} }
/*} catch (TransportExceptionInterface $exception) {
// Handle timeout for the initial request } catch (TransportExceptionInterface $exception) {
$startServerCommand = '/var/www/live-query-wiktextract/venv/bin/python3 /var/www/live-query-wiktextract/src/app.py'; // Catch the exception and check if it's a 404 response
exec($startServerCommand); 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 // 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()) { if ($chunk->isTimeout()) {
// Handle timeout for streaming responses // Handle timeout for streaming responses
$startServerCommand = '/var/www/live-query-wiktextract/venv/bin/python3 /var/www/live-query-wiktextract/src/app.py'; restartServer();
exec($startServerCommand);
} }
}*/ }
//dump(gettype($result));dump($url);dump(microtime(true)-$time_start);dump($result);/*Debug*/ //dump(gettype($result));dump($url);dump(microtime(true)-$time_start);dump($result);/*Debug*/
// Output and error handling // Output and error handling
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment