Added connection timeout at 5 seconds
authorChuck Scott <cscott@gaslightmedia.com>
Wed, 22 Feb 2017 19:21:52 +0000 (14:21 -0500)
committerChuck Scott <cscott@gaslightmedia.com>
Wed, 22 Feb 2017 19:21:52 +0000 (14:21 -0500)
Improved error handling

classes/serverBandwidthSupport.php
models/admin/dashboardWidget/serverStats.php
views/admin/dashboardWidget/serverStats.html

index 3b7c8fd..1f18a58 100644 (file)
@@ -62,11 +62,17 @@ class GlmServerBandwidthSupport
     public function bandwidthDataConnect($host, $user, $pass, $database, $website)
     {
 
+        // Initialize MySQLi
+        $this->bwdb = mysqli_init();
+
+        // Set a short timeout
+        $this->bwdb->options(MYSQLI_OPT_CONNECT_TIMEOUT, 5);
+
         // Connect to MySQL
-        $this->bwdb = mysqli_connect($host, $user, $pass, $database);
+        $this->bwdb->real_connect($host, $user, $pass, $database);
 
-        if (!$this->bwdb) {
-            $this->bwdb = false;
+        // If there was an error connecting
+        if ($this->bwdb->connect_errno) {
             return mysqli_connect_error();
         }
 
@@ -74,7 +80,6 @@ class GlmServerBandwidthSupport
         $this->bandwidthGetWebsiteID($website);
 
         if (!$this->siteId) {
-            $this->bwdb = false;
             return "Website Name not found: ".$website;
         }
 
index d2580b9..daaf1aa 100644 (file)
@@ -41,6 +41,13 @@ class GlmMembersAdmin_dashboardWidget_serverStats extends GlmServerBandwidthSupp
      * @access public
      */
     public $dbError = '';
+    /**
+     * Stats Results
+     *
+     * @var $haveStats
+     * @access public
+     */
+    public $stats = false;
     /**
      * Server Stats Database Connection Enabled
      *
@@ -100,7 +107,7 @@ class GlmMembersAdmin_dashboardWidget_serverStats extends GlmServerBandwidthSupp
         }
 
         // Connect to server stats database
-        $this->bandwidthDataConnect(
+        $this->dbError = $this->bandwidthDataConnect(
             $dbData['db_host'],
             $dbData['db_user'],
             $dbData['db_pass'],
@@ -121,10 +128,10 @@ class GlmMembersAdmin_dashboardWidget_serverStats extends GlmServerBandwidthSupp
         $stats = false;
 
         // Check for good database connection
-        if ($this->bwdb != false) {
+        if ($this->dbError == '') {
 
             // Get current summary stats
-            $stats = $this->bandwidthGetStats();
+            $this->stats = $this->bandwidthGetStats();
 
             // Build summary info
             $thisDate = date('m/d/Y');
@@ -137,8 +144,9 @@ class GlmMembersAdmin_dashboardWidget_serverStats extends GlmServerBandwidthSupp
 
         // Compile template data
         $templateData = array(
+            'dbError' => $this->dbError,
             'serverStatsEnabled' => $this->enabled,
-            'serverStats' => $stats,
+            'serverStats' => $this->stats,
             'thisDate' => $thisDate,
             'thisDateTime' => $thisDateTime,
             'thisMonth' => $thisMonth,
index 6caaca3..0c28862 100644 (file)
     });
 </script>
 {else}
-<h3><span class="glm-error">NOTE:</span> Unable to connect to server stats database!</h3>
-Check Server Stats Management setting<br>
+<p>
+  <span class="glm-error">NOTE:</span> Unable to connect to server Bandwidth Statistics database!<br>{$dbError}
+</p>
 {/if}
 {/if}
\ No newline at end of file