From: Maxim Mamontov <faust@gts.dp.ua>
Date: Wed, 25 May 2011 11:35:43 +0000 (+0300)
Subject: 'retries' parameter added to PostgreSQL storage plugin
X-Git-Tag: 2.408-alpha~147
X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/838ec5745c5198692997e0a1cb8a147682cba210

'retries' parameter added to PostgreSQL storage plugin
---

diff --git a/projects/stargazer/inst/freebsd/etc/stargazer/conf-available.d/store_postgresql.conf b/projects/stargazer/inst/freebsd/etc/stargazer/conf-available.d/store_postgresql.conf
index 86c76f44..5132c8fc 100644
--- a/projects/stargazer/inst/freebsd/etc/stargazer/conf-available.d/store_postgresql.conf
+++ b/projects/stargazer/inst/freebsd/etc/stargazer/conf-available.d/store_postgresql.conf
@@ -22,4 +22,10 @@
     # Value: any, supported by database
     # Default: 123456
     password = 123456
-</StoreModule>
\ No newline at end of file
+
+    # Number of tries to reconnect
+    # Parameter: optional
+    # Value: positive integer
+    # Default: 3
+    # retries = 3
+</StoreModule>
diff --git a/projects/stargazer/inst/linux/etc/stargazer/conf-available.d/store_postgresql.conf b/projects/stargazer/inst/linux/etc/stargazer/conf-available.d/store_postgresql.conf
index 86c76f44..5132c8fc 100644
--- a/projects/stargazer/inst/linux/etc/stargazer/conf-available.d/store_postgresql.conf
+++ b/projects/stargazer/inst/linux/etc/stargazer/conf-available.d/store_postgresql.conf
@@ -22,4 +22,10 @@
     # Value: any, supported by database
     # Default: 123456
     password = 123456
-</StoreModule>
\ No newline at end of file
+
+    # Number of tries to reconnect
+    # Parameter: optional
+    # Value: positive integer
+    # Default: 3
+    # retries = 3
+</StoreModule>
diff --git a/projects/stargazer/plugins/store/postgresql/postgresql_store.cpp b/projects/stargazer/plugins/store/postgresql/postgresql_store.cpp
index fd0ebf5e..4bf72c0e 100644
--- a/projects/stargazer/plugins/store/postgresql/postgresql_store.cpp
+++ b/projects/stargazer/plugins/store/postgresql/postgresql_store.cpp
@@ -78,7 +78,8 @@ POSTGRESQL_STORE::POSTGRESQL_STORE()
       user("stg"),
       password("123456"),
       version(0),
-      connection(NULL)
+      connection(NULL),
+      retries(3)
 {
 pthread_mutex_init(&mutex, NULL);
 }
@@ -117,6 +118,15 @@ for(i = settings.moduleParams.begin(); i != settings.moduleParams.end(); ++i)
         {
         password = *(i->value.begin());
         }
+    if (s == "retries")
+        {
+        if (str2x(*(i->value.begin()), retries))
+            {
+            strError = "Invalid 'retries' value";
+            printfd(__FILE__, "POSTGRESQL_STORE::ParseSettings(): '%s'\n", strError.c_str());
+            return -1;
+            }
+        }
     }
 
 clientEncoding = "KOI8";
@@ -138,7 +148,8 @@ if (PQstatus(connection) != CONNECTION_OK)
     {
     strError = PQerrorMessage(connection);
     printfd(__FILE__, "POSTGRESQL_STORE::Connect(): '%s'\n", strError.c_str());
-    return 1;
+    // Will try to connect later
+    return 0;
     }
 
 if (PQsetClientEncoding(connection, clientEncoding.c_str()))
@@ -153,7 +164,12 @@ return CheckVersion();
 //-----------------------------------------------------------------------------
 int POSTGRESQL_STORE::Reset() const
 {
-PQreset(connection);
+for (int i = 0; i < retries && PQstatus(connection) != CONNECTION_OK; ++i)
+    {
+    struct timespec ts = {1, 0};
+    nanosleep(&ts, NULL);
+    PQreset(connection);
+    }
 
 if (PQstatus(connection) != CONNECTION_OK)
     {
@@ -166,7 +182,7 @@ if (PQsetClientEncoding(connection, clientEncoding.c_str()))
     {
     strError = PQerrorMessage(connection);
     printfd(__FILE__, "POSTGRESQL_STORE::Reset(): '%s'\n", strError.c_str());
-    return 1;
+    return -1;
     }
 
 return CheckVersion();
diff --git a/projects/stargazer/plugins/store/postgresql/postgresql_store.h b/projects/stargazer/plugins/store/postgresql/postgresql_store.h
index a32b155e..8169137e 100644
--- a/projects/stargazer/plugins/store/postgresql/postgresql_store.h
+++ b/projects/stargazer/plugins/store/postgresql/postgresql_store.h
@@ -152,6 +152,7 @@ private:
     MODULE_SETTINGS settings;
     mutable pthread_mutex_t mutex;
     mutable int version;
+    int retries;
 
     PGconn * connection;
 };