From cd6f6ec2c0b19c86cc7beb83ff927b3e226313ea Mon Sep 17 00:00:00 2001
From: Dominik Pfennig <dominik@suma-ev.de>
Date: Mon, 8 May 2017 11:32:42 +0200
Subject: [PATCH] Die erste stabile Version

Die Anzahl der Fetcher reguliert sich nun von alleine. Fehlerfrei ist es aber noch nicht.
---
 app/Jobs/Searcher.php       | 13 +++++++++++--
 app/Models/Searchengine.php | 15 ++++++++++++---
 2 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/app/Jobs/Searcher.php b/app/Jobs/Searcher.php
index 9add9b627..7496b42db 100644
--- a/app/Jobs/Searcher.php
+++ b/app/Jobs/Searcher.php
@@ -33,7 +33,6 @@ class Searcher implements ShouldQueue
         $this->name = $name;
         $this->pid = getmypid();
         // Submit this worker to the Redis System
-        Redis::set($this->name, "running");
         Redis::expire($this->name, 5);
     }
 
@@ -51,6 +50,7 @@ class Searcher implements ShouldQueue
         while(true){
             // Update the expire
             Redis::expire($this->name, 5);
+            Redis::expire($this->name . ".stats", 5);
             // One Searcher can handle a ton of requests to the same server
             // Each search to the server of this Searcher will be submitted to a queue
             // stored in redis which has the same name as this searchengine appended by a ".queue"
@@ -65,7 +65,6 @@ class Searcher implements ShouldQueue
                 $mission = $mission[1];
                 $this->counter++;#
                 $poptime = microtime(true) - $time;
-                $time = microtime(true);
             }
 
             // The mission is a String which can be divided to retrieve two informations:
@@ -81,6 +80,16 @@ class Searcher implements ShouldQueue
 
             $this->storeResult($result, $poptime, $hashValue);
 
+            if($this->counter === 3){
+                Redis::set($this->name, "running");
+            }
+
+            // Reset the time of the last Job so we can calculate
+            // the time we have spend waiting for a new job
+            // We submit that calculation to the Redis systemin the method
+            // storeResult()
+            $time = microtime(true);
+
             // In sync mode every Searcher may only retrieve one result because it would block
             // the execution of the remaining code otherwise:
             if(getenv("QUEUE_DRIVER") === "sync" || $this->counter > $this->MAX_REQUESTS){
diff --git a/app/Models/Searchengine.php b/app/Models/Searchengine.php
index cc98b6b6e..eb26154c1 100644
--- a/app/Models/Searchengine.php
+++ b/app/Models/Searchengine.php
@@ -156,10 +156,19 @@ abstract class Searchengine
                 // Now we have to check if the current count is enough to fetch all the
                 // searches or if it needs help.
                 // Let's hardcode a minimum of 100ms between every search job.
-                die(var_dump($searcherData));
+                // First calculate the median of all Times
+                $median = 0;
+                foreach($searcherData as $pid => $data){
+                    $data = explode(";", $data);
+                    $median += floatval($data[1]);
+                }
+                $median /= sizeof($searcherData);
+                if($median < 100){
+                    $needSearcher = true;
+                }
             }
-            
-            if($needSearcher){
+            if($needSearcher && Redis::get($this->name) !== "locked"){
+                Redis::set($this->name, "locked");
                 $this->dispatch(new Searcher($this->name));
             }
         }
-- 
GitLab