From d837d5ccdc49dcaa98c82610d3b09d09baa385be Mon Sep 17 00:00:00 2001
From: Enzo Simonnet <enzosim@laposte.net>
Date: Wed, 7 Feb 2024 16:18:08 -0500
Subject: [PATCH] =?UTF-8?q?requ=C3=AAte=20serveur=20wiktextract=20ok?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 src/Manager/WiktionaryManager.php | 71 ++++++-------------------------
 1 file changed, 13 insertions(+), 58 deletions(-)

diff --git a/src/Manager/WiktionaryManager.php b/src/Manager/WiktionaryManager.php
index 5984e0f..c59fc5d 100644
--- a/src/Manager/WiktionaryManager.php
+++ b/src/Manager/WiktionaryManager.php
@@ -36,6 +36,7 @@ class WiktionaryManager
      * @param $word
      * @param $language
      * @return mixed
+     * @throws \Exception|TransportExceptionInterface
      */
     public function search($word, $language)
     {
@@ -45,34 +46,13 @@ class WiktionaryManager
             return $this->lastSearch['data'];
         }
 
-//  usage: wikstraktor.py [-h] [-l LANGUAGE] [-w WIKI_LANGUAGE] [-m MOT]
-//                      [-f DESTINATION_FILE] [-A] [-C]
-//
-//  Interroger un wiktionnaire
-//	ex :
-//	‣./wikstraktor.py -m blue
-//	‣./wikstraktor.py -m blue -f blue.json -A -C
-//	‣./wikstraktor.py -l en -w fr -m blue -f blue.json -A -C
-
-//        options:
-//        -h, --help            show this help message and exit
-//    -l LANGUAGE, --language LANGUAGE
-//                        la langue du mot
-//    -w WIKI_LANGUAGE, --wiki_language WIKI_LANGUAGE
-//                        la langue du wiki
-//    -m MOT, --mot MOT     le mot à chercher
-//    -f DESTINATION_FILE, --destination_file DESTINATION_FILE
-//                        le fichier dans lequel stocker le résultat
-//    -A, --force_ascii     json avec que des caractères ascii
-//    -C, --compact         json sans indentation
-
-
 	    //$time_start = microtime(true); /*Debug*/
 
         $url = 'http://localhost:5000/search/' . $language . '/' . $language . '/' . $word . '/a_wikstraktor';
         $client = HttpClient::create();
+        $result = null;
 
-// Function to restart the server
+        // 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);
@@ -80,55 +60,30 @@ class WiktionaryManager
 
 // Make the initial request with a timeout
         try {
-            $response = $client->request('GET', $url, ['timeout' => 5]);
+            $response = $client->request('GET', $url);
+            // dump($response->getStatusCode());die();
 
             // 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) {
-            // 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"'))) {
+            if (empty(exec('ps aux | grep "[a]pp.py"'))) {
                 restartServer();
-                $response = $client->request('GET', $url, ['timeout' => 5]);
+                if (!empty(exec('ps aux | grep "[a]pp.py"'))) {
+                    $response = $client->request('GET', $url);
 
-                // Check the HTTP status code
-                if ($response->getStatusCode() === 200) {
-                    // Server is up and returned a successful response
-                    $result = $response->getContent();
+                    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
+                    throw new \Exception('Server is down');
                 }
-            } 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)], 5) as $response => $chunk) {
-            if ($chunk->isTimeout()) {
-                // Handle timeout for streaming responses
-                restartServer();
-            }
-        }
 	    //dump(gettype($result));dump($url);dump(microtime(true)-$time_start);dump($result);/*Debug*/
 	// Output and error handling
 
-- 
GitLab