changeset 141:0b3e9ee53c46

Added 'MaximumConnectionRetries' & 'ConnectionRetryInterval' to configure the retries when connecting to the DB at startup
author Alain Mazy <alain@mazy.be>
date Mon, 03 Feb 2020 22:29:51 +0100
parents 4cd7e45b671e
children bbd57f5672fa
files Framework/Common/DatabaseManager.cpp Framework/Common/IDatabaseFactory.h Framework/MySQL/MySQLParameters.cpp Framework/MySQL/MySQLParameters.h Framework/PostgreSQL/PostgreSQLParameters.cpp Framework/PostgreSQL/PostgreSQLParameters.h MySQL/NEWS MySQL/Plugins/MySQLIndex.h MySQL/Plugins/MySQLStorageArea.h PostgreSQL/NEWS PostgreSQL/Plugins/PostgreSQLIndex.h PostgreSQL/Plugins/PostgreSQLStorageArea.h
diffstat 12 files changed, 68 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/Common/DatabaseManager.cpp	Fri Jan 31 17:24:29 2020 +0100
+++ b/Framework/Common/DatabaseManager.cpp	Mon Feb 03 22:29:51 2020 +0100
@@ -32,9 +32,11 @@
 {
   IDatabase& DatabaseManager::GetDatabase()
   {
-    static const unsigned int MAX_CONNECTION_ATTEMPTS = 10;   // TODO: Parameter
+    unsigned int maxConnectionRetries = 10;
+    unsigned int connectionRetryInterval = 5;
+    unsigned int count = 0;
 
-    unsigned int count = 0;
+    factory_->GetConnectionRetriesParameters(maxConnectionRetries, connectionRetryInterval);
       
     while (database_.get() == NULL)
     {
@@ -50,10 +52,10 @@
         {
           count ++;
 
-          if (count <= MAX_CONNECTION_ATTEMPTS)
+          if (count <= maxConnectionRetries)
           {
             LOG(WARNING) << "Database is currently unavailable, retrying...";
-            boost::this_thread::sleep(boost::posix_time::seconds(1));
+            boost::this_thread::sleep(boost::posix_time::seconds(connectionRetryInterval));
             continue;
           }
           else
--- a/Framework/Common/IDatabaseFactory.h	Fri Jan 31 17:24:29 2020 +0100
+++ b/Framework/Common/IDatabaseFactory.h	Mon Feb 03 22:29:51 2020 +0100
@@ -35,5 +35,7 @@
     virtual Dialect GetDialect() const = 0;
 
     virtual IDatabase* Open() = 0;
+
+    virtual void GetConnectionRetriesParameters(unsigned int& maxConnectionRetries, unsigned int& connectionRetryInterval) = 0;
   };
 }
--- a/Framework/MySQL/MySQLParameters.cpp	Fri Jan 31 17:24:29 2020 +0100
+++ b/Framework/MySQL/MySQLParameters.cpp	Mon Feb 03 22:29:51 2020 +0100
@@ -89,6 +89,9 @@
     }
 
     lock_ = configuration.GetBooleanValue("Lock", true);  // Use locking by default
+
+    maxConnectionRetries_ = configuration.GetUnsignedIntegerValue("MaximumConnectionRetries", 10);
+    connectionRetryInterval_ = configuration.GetUnsignedIntegerValue("ConnectionRetryInterval", 5);
   }
 
 
--- a/Framework/MySQL/MySQLParameters.h	Fri Jan 31 17:24:29 2020 +0100
+++ b/Framework/MySQL/MySQLParameters.h	Mon Feb 03 22:29:51 2020 +0100
@@ -39,6 +39,8 @@
     uint16_t     port_;
     std::string  unixSocket_;
     bool         lock_;
+    unsigned int maxConnectionRetries_;
+    unsigned int connectionRetryInterval_;
 
     void Reset();
 
@@ -99,6 +101,16 @@
       return lock_;
     }
 
+    unsigned int GetMaxConnectionRetries() const
+    {
+      return maxConnectionRetries_;
+    }
+
+    unsigned int GetConnectionRetryInterval() const
+    {
+      return connectionRetryInterval_;
+    }
+
     void Format(Json::Value& target) const;
   };
 }
--- a/Framework/PostgreSQL/PostgreSQLParameters.cpp	Fri Jan 31 17:24:29 2020 +0100
+++ b/Framework/PostgreSQL/PostgreSQLParameters.cpp	Mon Feb 03 22:29:51 2020 +0100
@@ -39,6 +39,8 @@
     uri_.clear();
     ssl_ = false;
     lock_ = true;
+    maxConnectionRetries_ = 10;
+    connectionRetryInterval_ = 5;
   }
 
 
@@ -90,6 +92,9 @@
     }
 
     lock_ = configuration.GetBooleanValue("Lock", true);  // Use locking by default
+
+    maxConnectionRetries_ = configuration.GetUnsignedIntegerValue("MaximumConnectionRetries", 10);
+    connectionRetryInterval_ = configuration.GetUnsignedIntegerValue("ConnectionRetryInterval", 5);
   }
 
 
--- a/Framework/PostgreSQL/PostgreSQLParameters.h	Fri Jan 31 17:24:29 2020 +0100
+++ b/Framework/PostgreSQL/PostgreSQLParameters.h	Mon Feb 03 22:29:51 2020 +0100
@@ -40,6 +40,8 @@
     std::string  uri_;
     bool         ssl_;
     bool         lock_;
+    unsigned int maxConnectionRetries_;
+    unsigned int connectionRetryInterval_;
 
     void Reset();
 
@@ -112,6 +114,16 @@
       return lock_;
     }
 
+    unsigned int GetMaxConnectionRetries() const
+    {
+      return maxConnectionRetries_;
+    }
+
+    unsigned int GetConnectionRetryInterval() const
+    {
+      return connectionRetryInterval_;
+    }
+
     void Format(std::string& target) const;
   };
 }
--- a/MySQL/NEWS	Fri Jan 31 17:24:29 2020 +0100
+++ b/MySQL/NEWS	Mon Feb 03 22:29:51 2020 +0100
@@ -3,6 +3,8 @@
 
 * Implementation of new extensions: LookupResourceAndParent and GetAllMetadata
 * Added an advisory lock to avoid race conditions during database setup
+* Added "MaximumConnectionRetries" & "ConnectionRetryInterval" to configure 
+  the retries when connecting to the DB at startup
 
 
 Release 2.0 (2019-01-23)
--- a/MySQL/Plugins/MySQLIndex.h	Fri Jan 31 17:24:29 2020 +0100
+++ b/MySQL/Plugins/MySQLIndex.h	Mon Feb 03 22:29:51 2020 +0100
@@ -49,6 +49,12 @@
       {
         return that_.OpenInternal();
       }
+
+      virtual void GetConnectionRetriesParameters(unsigned int& maxConnectionRetries, unsigned int& connectionRetryInterval)
+      {
+        maxConnectionRetries = that_.parameters_.GetMaxConnectionRetries();
+        connectionRetryInterval = that_.parameters_.GetConnectionRetryInterval();
+      }
     };
 
     OrthancPluginContext*  context_;
--- a/MySQL/Plugins/MySQLStorageArea.h	Fri Jan 31 17:24:29 2020 +0100
+++ b/MySQL/Plugins/MySQLStorageArea.h	Mon Feb 03 22:29:51 2020 +0100
@@ -50,6 +50,12 @@
       {
         return that_.OpenInternal();
       }
+
+      virtual void GetConnectionRetriesParameters(unsigned int& maxConnectionRetries, unsigned int& connectionRetryInterval)
+      {
+        maxConnectionRetries = that_.parameters_.GetMaxConnectionRetries();
+        connectionRetryInterval = that_.parameters_.GetConnectionRetryInterval();
+      }
     };
 
     OrthancPluginContext*  context_;
--- a/PostgreSQL/NEWS	Fri Jan 31 17:24:29 2020 +0100
+++ b/PostgreSQL/NEWS	Mon Feb 03 22:29:51 2020 +0100
@@ -2,6 +2,8 @@
 ===============================
 
 * Added an advisory lock to avoid race conditions during database setup
+* Added "MaximumConnectionRetries" & "ConnectionRetryInterval" to configure 
+  the retries when connecting to the DB at startup
 
 
 Release 3.2 (2019-03-01)
--- a/PostgreSQL/Plugins/PostgreSQLIndex.h	Fri Jan 31 17:24:29 2020 +0100
+++ b/PostgreSQL/Plugins/PostgreSQLIndex.h	Mon Feb 03 22:29:51 2020 +0100
@@ -49,6 +49,12 @@
       {
         return that_.OpenInternal();
       }
+
+      virtual void GetConnectionRetriesParameters(unsigned int& maxConnectionRetries, unsigned int& connectionRetryInterval)
+      {
+        maxConnectionRetries = that_.parameters_.GetMaxConnectionRetries();
+        connectionRetryInterval = that_.parameters_.GetConnectionRetryInterval();
+      }
     };
 
     OrthancPluginContext*  context_;
--- a/PostgreSQL/Plugins/PostgreSQLStorageArea.h	Fri Jan 31 17:24:29 2020 +0100
+++ b/PostgreSQL/Plugins/PostgreSQLStorageArea.h	Mon Feb 03 22:29:51 2020 +0100
@@ -49,6 +49,12 @@
       {
         return that_.OpenInternal();
       }
+
+      virtual void GetConnectionRetriesParameters(unsigned int& maxConnectionRetries, unsigned int& connectionRetryInterval)
+      {
+        maxConnectionRetries = that_.parameters_.GetMaxConnectionRetries();
+        connectionRetryInterval = that_.parameters_.GetConnectionRetryInterval();
+      }
     };
 
     OrthancPluginContext*  context_;