Make cdr_pgsql gracefully handle restarts (bug #3628)
authorMark Spencer <markster@digium.com>
Sat, 19 Feb 2005 20:10:52 +0000 (20:10 +0000)
committerMark Spencer <markster@digium.com>
Sat, 19 Feb 2005 20:10:52 +0000 (20:10 +0000)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@5056 65c4cc65-6c06-0410-ace0-fbb531ad65f3

cdr/cdr_pgsql.c

index efad07c..44e6d25 100755 (executable)
@@ -132,6 +132,19 @@ static int pgsql_log(struct ast_cdr *cdr)
                         pgerror = PQresultErrorMessage(result);
                        ast_log(LOG_ERROR,"cdr_pgsql: Failed to insert call detail record into database!\n");
                         ast_log(LOG_ERROR,"cdr_pgsql: Reason: %s\n", pgerror);
+                       ast_log(LOG_ERROR,"cdr_pgsql: Connection may have been lost... attempting to reconnect.\n");
+                       PQreset(conn);
+                       if (PQstatus(conn) == CONNECTION_OK) {
+                               ast_log(LOG_ERROR, "cdr_pgsql: Connection reestablished.\n");
+                               connected = 1;
+                               result = PQexec(conn, sqlcmd);
+                               if ( PQresultStatus(result) != PGRES_COMMAND_OK)
+                               {
+                                       pgerror = PQresultErrorMessage(result);
+                                       ast_log(LOG_ERROR,"cdr_pgsql: HARD ERROR!  Attempted reconnection failed.  DROPPING CALL RECORD!\n");
+                                       ast_log(LOG_ERROR,"cdr_pgsql: Reason: %s\n", pgerror);
+                               }
+                       }
                        ast_mutex_unlock(&pgsql_lock);
                        return -1;
                }
@@ -314,7 +327,7 @@ static int process_my_load_module(struct ast_config *cfg)
                connected = 1;
        } else {
                 pgerror = PQerrorMessage(conn);
-               ast_log(LOG_ERROR, "cdr_pgsql: Unable to connect to database server %s.  Calls will not be logged!\n", pghostname);
+               ast_log(LOG_ERROR, "cdr_pgsql: Unable to connect to database server %s.  CALLS WILL NOT BE LOGGED!!\n", pghostname);
                 ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
                connected = 0;
        }