From a7618c2db7e64ab92de1dff50bf6f0ce3b432343 Mon Sep 17 00:00:00 2001
From: Robert Lange <robert.lange@uni-leipzig.de>
Date: Tue, 22 Nov 2022 10:25:02 +0100
Subject: [PATCH] refs #22413 [finc] backport vf8 health check

* only do health check (get status) when record id is set - testConnections is tried later anyway
* early return when get status (basic availability) has no results - don't try DAIA in testConnections in FincILS again
* separate between DAIA and PAIA in FincILS testConnections
---
 module/finc/src/finc/ILS/Connection.php     | 39 ++++++++++++++++++-
 module/finc/src/finc/ILS/Driver/FincILS.php | 42 ++++++++++++---------
 2 files changed, 62 insertions(+), 19 deletions(-)

diff --git a/module/finc/src/finc/ILS/Connection.php b/module/finc/src/finc/ILS/Connection.php
index 2fe8401af44..5ea444a8116 100644
--- a/module/finc/src/finc/ILS/Connection.php
+++ b/module/finc/src/finc/ILS/Connection.php
@@ -115,7 +115,6 @@ class Connection extends \VuFind\ILS\Connection implements TranslatorAwareInterf
         return false;
     }
 
-
     /**
      * Get access to the driver object.
      * Extends parent function with a bypass of failover on InitException
@@ -172,4 +171,42 @@ class Connection extends \VuFind\ILS\Connection implements TranslatorAwareInterf
             throw $e;
         }
     }
+
+    /**
+     * Get Offline Mode
+     *
+     * This is responsible for returning the offline mode
+     *
+     * @param bool $healthCheck Perform a health check in addition to consulting
+     * the ILS status?
+     *
+     * @return string|bool "ils-offline" for systems where the main ILS is offline,
+     * "ils-none" for systems which do not use an ILS, false for online systems.
+     *
+     * @deprecated Backport of VuFind 8.1 (healthCheckId) - remove after upgrade
+     */
+    public function getOfflineMode($healthCheck = false)
+    {
+        // If we have NoILS failover configured, force driver initialization so
+        // we can know we are checking the offline mode against the correct driver.
+        if ($this->hasNoILSFailover()) {
+            $this->getDriver();
+        }
+        $hasOfflineMode = $this->checkCapability('getOfflineMode');
+        // If we need to perform a health check, try to do a random item lookup
+        // before proceeding.
+        if ($healthCheck && $this->config->healthCheckId) {
+            if (empty($this->getStatus($this->config->healthCheckId)) && $hasOfflineMode) {
+                // show different return string to separate between basic availability and PAIA later?
+                return 'ils-offline';
+            }
+        }
+
+        // If we're encountering failures, let's go into ils-offline mode if
+        // the ILS driver does not natively support getOfflineMode().
+        $default = $this->failing ? 'ils-offline' : false;
+
+        // Graceful degradation -- return false if no method supported.
+        return $hasOfflineMode ? $this->getDriver()->getOfflineMode() : $default;
+    }
 }
diff --git a/module/finc/src/finc/ILS/Driver/FincILS.php b/module/finc/src/finc/ILS/Driver/FincILS.php
index 786db5ce3cc..169b8f021ea 100644
--- a/module/finc/src/finc/ILS/Driver/FincILS.php
+++ b/module/finc/src/finc/ILS/Driver/FincILS.php
@@ -261,7 +261,7 @@ class FincILS extends PAIA implements LoggerAwareInterface
      * systems which do not use an ILS, false for systems that are fully online. If
      * not implemented, the value defaults to false
      *
-     * @return bool
+     * @return bool|string
      */
     public function getOfflineMode()
     {
@@ -271,7 +271,7 @@ class FincILS extends PAIA implements LoggerAwareInterface
                 return false;
             }
             // test again
-            $this->testILSConnections();
+            $this->_testILSConnections();
             return false;
         } catch (\Exception $e) {
             $this->debug($e->getMessage());
@@ -1697,28 +1697,34 @@ class FincILS extends PAIA implements LoggerAwareInterface
     }
 
     /**
-     * Private service test method
+     * Private service test method for basic accessibility of DAIA / PAIA
+     *
+     * @param boolean $testDAIA test DAIA service
+     * @param boolean $testPAIA test PAIA service
      *
      * @return void
      * @throws ILSException
      */
-    private function testILSConnections()
+    private function _testILSConnections($testDAIA = true, $testPAIA = true)
     {
         try {
-            // test DAIA service
-            preg_match(
-                "/^(http[s:\/0-9a-zA-Z\.]*(:[0-9]*)?\/[0-9a-zA-Z\-\/]*)/",
-                $this->baseUrl,
-                $daiaMatches
-            );
-            $this->httpService->get($daiaMatches[1], [], $this->ilsTestTimeout);
-            // test PAIA service
-            preg_match(
-                "/^(http[s:\/0-9a-zA-Z\.]*(:[0-9]*)?\/[0-9a-zA-Z\-\/]*)/",
-                $this->paiaURL,
-                $paiaMatches
-            );
-            $this->httpService->get($paiaMatches[1], [], $this->ilsTestTimeout);
+            if ($testDAIA) {
+                preg_match(
+                    "/^(http[s:\/0-9a-zA-Z\.]*(:[0-9]*)?\/[0-9a-zA-Z\-\/]*)/",
+                    $this->baseUrl,
+                    $daiaMatches
+                );
+                $this->httpService->get($daiaMatches[1], [], $this->ilsTestTimeout);
+            }
+
+            if ($testPAIA) {
+                preg_match(
+                    "/^(http[s:\/0-9a-zA-Z\.]*(:[0-9]*)?\/[0-9a-zA-Z\-\/]*)/",
+                    $this->paiaURL,
+                    $paiaMatches
+                );
+                $this->httpService->get($paiaMatches[1], [], $this->ilsTestTimeout);
+            }
 
             // test succeeded, save state
             $this->isOnline = true;
-- 
GitLab