From 965ca5d5dfa81056e87d49ec08190fdac80b9503 Mon Sep 17 00:00:00 2001
From: Dominik Hebeler <dominik@suma-ev.de>
Date: Wed, 6 Mar 2019 14:40:17 +0100
Subject: [PATCH] Waiting for a specified timeout if no main searchengines are
 defined

---
 app/MetaGer.php                  | 18 ++++++++++++++++--
 resources/js/scriptResultPage.js |  2 +-
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/app/MetaGer.php b/app/MetaGer.php
index 26425376e..19d760695 100644
--- a/app/MetaGer.php
+++ b/app/MetaGer.php
@@ -706,8 +706,15 @@ class MetaGer
         $timeStart = microtime(true);
         $answered = [];
         $results = null;
-        while (sizeof($enginesToWaitFor) > 0) {
-            $newEngine = $redis->blpop($this->redisResultWaitingKey, 5);
+
+        # If there is no main searchengine to wait for or if the only main engine is yahoo-ads we will define a timeout of 1s
+        $forceTimeout = null;
+        if (sizeof($enginesToWaitFor) === 0 || (sizeof($enginesToWaitFor) === 1 && $enginesToWaitFor[0]->name === "yahoo-ads")) {
+            $forceTimeout = 1;
+        }
+
+        while (sizeof($enginesToWaitFor) > 0 || ($forceTimeout !== null && (microtime(true) - $timeStart) < $forceTimeout)) {
+            $newEngine = $redis->blpop($this->redisResultWaitingKey, 1);
             if ($newEngine === null || sizeof($newEngine) !== 2) {
                 continue;
             } else {
@@ -731,6 +738,13 @@ class MetaGer
         $pipeline->hset($this->getRedisEngineResult() . "status", "startTime", $timeStart);
         $pipeline->hset($this->getRedisEngineResult() . "status", "engineCount", sizeof($engines));
         $pipeline->hset($this->getRedisEngineResult() . "status", "engineDelivered", sizeof($answered));
+        # Add the cached engines as answered
+        foreach ($engines as $engine) {
+            if ($engine->cached) {
+                $pipeline->hincrby($this->getRedisEngineResult() . "status", "engineDelivered", 1);
+                $pipeline->hincrby($this->getRedisEngineResult() . "status", "engineAnswered", 1);
+            }
+        }
         foreach ($answered as $engine) {
             $pipeline->hset($this->getRedisEngineResult() . $engine, "delivered", "1");
         }
diff --git a/resources/js/scriptResultPage.js b/resources/js/scriptResultPage.js
index aea5a2efc..dd140c821 100644
--- a/resources/js/scriptResultPage.js
+++ b/resources/js/scriptResultPage.js
@@ -103,7 +103,7 @@ function loadMoreResults() {
               }
             }
           }
-          if ($(".no-results-error").length > 0 && $(".image-container > .image").length > 0) {
+          if ($(".no-results-error").length > 0 && ($(".image-container > .image").length > 0) || $(".result:not(.ad)").length > 0) {
             $(".no-results-error").remove();
             if ($(".alert.alert-danger > ul").children().length == 0) {
               $(".alert.alert-danger").remove();
-- 
GitLab