Skip site navigation (1) Skip section navigation (2)

SVN Commit by dpage: r4189 - in trunk/pgadmin3/xtra/pgagent: . include

From: svn(at)pgadmin(dot)org
To: pgadmin-hackers(at)postgresql(dot)org
Subject: SVN Commit by dpage: r4189 - in trunk/pgadmin3/xtra/pgagent: . include
Date: 2005-05-17 13:54:23
Message-ID: 200505171354.j4HDsN1U010965@developer.pgadmin.org (view raw or flat)
Thread:
Lists: pgadmin-hackers
Author: dpage
Date: 2005-05-17 14:54:23 +0100 (Tue, 17 May 2005)
New Revision: 4189

Modified:
   trunk/pgadmin3/xtra/pgagent/connection.cpp
   trunk/pgadmin3/xtra/pgagent/include/pgAgent.h
   trunk/pgadmin3/xtra/pgagent/job.cpp
   trunk/pgadmin3/xtra/pgagent/misc.cpp
   trunk/pgadmin3/xtra/pgagent/pgAgent.cpp
   trunk/pgadmin3/xtra/pgagent/unix.cpp
   trunk/pgadmin3/xtra/pgagent/win32.cpp
Log:
pgAgent now runs on Unix :-)

Modified: trunk/pgadmin3/xtra/pgagent/connection.cpp
===================================================================
--- trunk/pgadmin3/xtra/pgagent/connection.cpp	2005-05-17 13:53:30 UTC (rev 4188)
+++ trunk/pgadmin3/xtra/pgagent/connection.cpp	2005-05-17 13:54:23 UTC (rev 4189)
@@ -44,7 +44,7 @@
     }
     else
     {
-	    lastError=PQerrorMessage(conn);
+        lastError=PQerrorMessage(conn);
         PQfinish(conn);
         conn=0;
     }
@@ -71,12 +71,12 @@
             memset(pool, 0, sizeof(DBconn*) * connPoolCount);
     }
     if (!pool)
-        fatal("Out of memory for connection pool");
+        LogMessage("Out of memory for connection pool", LOG_ERROR);
 
     basicConnectString=connectString;
     string dbname;
 
-    size_t pos=basicConnectString.find("dbname=");
+    int pos=basicConnectString.find("dbname=");
     if (pos == -1)
         dbname = "dba";
     else
@@ -108,7 +108,7 @@
             memset(pool, 0, sizeof(DBconn*) * connPoolCount);
     }
     if (!pool)
-        fatal("Out of memory for connection pool");
+        LogMessage("Out of memory for connection pool", LOG_ERROR);
 
     int i;
     DBconn **emptyConn=0, **oldestConn=0;

Modified: trunk/pgadmin3/xtra/pgagent/include/pgAgent.h
===================================================================
--- trunk/pgadmin3/xtra/pgagent/include/pgAgent.h	2005-05-17 13:53:30 UTC (rev 4188)
+++ trunk/pgadmin3/xtra/pgagent/include/pgAgent.h	2005-05-17 13:54:23 UTC (rev 4189)
@@ -32,7 +32,15 @@
 // to be implemented platform dependent
 void CheckForInterrupt();
 
+// Log levels
+enum
+{
+	LOG_DEBUG = 1,
+	LOG_WARNING,
+	LOG_ERROR
+};
 
+void LogMessage(char *msg, int level);
 void MainLoop();
 
 #endif // PGAGENT_H

Modified: trunk/pgadmin3/xtra/pgagent/job.cpp
===================================================================
--- trunk/pgadmin3/xtra/pgagent/job.cpp	2005-05-17 13:53:30 UTC (rev 4188)
+++ trunk/pgadmin3/xtra/pgagent/job.cpp	2005-05-17 13:54:23 UTC (rev 4189)
@@ -49,7 +49,7 @@
 {
     if (status != "")
     {
-        int rc=serviceConn->ExecuteVoid(
+        serviceConn->ExecuteVoid(
             "UPDATE pgagent.pga_jobprotocol "
             "   SET jprstatus='" + status + "', jprduration=now() - jprstart "
             " WHERE jprid=" + prtid + ";\n"
@@ -64,7 +64,7 @@
 
 int Job::Execute()
 {
-    int rc;
+    int rc=0;
     DBresult *steps=serviceConn->Execute(
         "SELECT jstid, jstkind, jstdbname, jstcode, jstonerror "
         "  FROM pgagent.pga_jobstep "
@@ -84,7 +84,7 @@
         string jpsid, jpecode;
 
 		DBresult *id=serviceConn->Execute(
-			"SELECT nextval('pg_jobprotocolstep_jpeid_seq') AS id");
+			"SELECT nextval('pga_jobprotocolstep_jpeid_seq') AS id");
 		if (id)
 		{
 			jpsid=id->GetString("id");

Modified: trunk/pgadmin3/xtra/pgagent/misc.cpp
===================================================================
--- trunk/pgadmin3/xtra/pgagent/misc.cpp	2005-05-17 13:53:30 UTC (rev 4188)
+++ trunk/pgadmin3/xtra/pgagent/misc.cpp	2005-05-17 13:54:23 UTC (rev 4189)
@@ -17,13 +17,7 @@
 #include <unistd.h>
 #endif
 
-void fatal(const string &text)
-{
-    printf(text.c_str());
-    exit(0);
-}
 
-
 string getArg(int &argc, char** &argv)
 {
     string s;
@@ -40,7 +34,7 @@
         else
         {
             // very bad!
-            fatal("bad argument.");
+            LogMessage("bad argument.", LOG_ERROR);
         }
     }
 

Modified: trunk/pgadmin3/xtra/pgagent/pgAgent.cpp
===================================================================
--- trunk/pgadmin3/xtra/pgagent/pgAgent.cpp	2005-05-17 13:53:30 UTC (rev 4188)
+++ trunk/pgadmin3/xtra/pgagent/pgAgent.cpp	2005-05-17 13:54:23 UTC (rev 4189)
@@ -66,12 +66,11 @@
 
 
 
-	// TODO - station should be the hostname, not '' (?)
-	char hostname[255];
-	gethostname(hostname, 255);
+    char hostname[255];
+    gethostname(hostname, 255);
 
     rc=serviceConn->ExecuteVoid(
-        "INSERT INTO pgagent.pga_jobagent (jagpid, station) SELECT pg_backend_pid(), '" + string(hostname) + "'");
+        "INSERT INTO pgagent.pga_jobagent (jagpid, jagstation) SELECT pg_backend_pid(), '" + string(hostname) + "'");
     if (rc < 0)
         return rc;
 
@@ -82,8 +81,10 @@
         DBresult *res=serviceConn->Execute(
             "SELECT J.jobid "
             "  FROM pgagent.pga_job J "
-            " WHERE jobenabled AND jobagentid IS NULL "
+            " WHERE jobenabled "
+            "   AND jobagentid IS NULL "
             "   AND jobnextrun <= now() "
+            "   AND jobhostagent = '' OR jobhostagent = '" + string(hostname) + "'"
             " ORDER BY jobnextrun");
 
         if (res)
@@ -98,6 +99,7 @@
                 if (job.Runnable())
                 {
                     foundJobToExecute=true;
+                    LogMessage("Executing job", LOG_DEBUG);
                     job.Execute();
                 }
             }
@@ -119,6 +121,18 @@
 
 void MainLoop()
 {
+    // Basic sanity check
+    DBconn *sanityConn=DBconn::Get(serviceDBname, true);
+    DBresult *res=sanityConn->Execute("SELECT count(*) As count FROM pg_class cl JOIN pg_namespace ns ON ns.oid=relnamespace WHERE relname='pga_job' AND nspname='pgagent'");
+    if (res)
+    {
+        string val=res->GetString("count");
+        
+        if (val == "0")
+            LogMessage("Could not find the table 'pgagent.pga_job'. Have you run pgagent.sql on this database?", LOG_ERROR);
+    }
+    
+    // OK, let's get down to business 
     do
     {
         DBconn *serviceConn=DBconn::Get(serviceDBname, true);

Modified: trunk/pgadmin3/xtra/pgagent/unix.cpp
===================================================================
--- trunk/pgadmin3/xtra/pgagent/unix.cpp	2005-05-17 13:53:30 UTC (rev 4188)
+++ trunk/pgadmin3/xtra/pgagent/unix.cpp	2005-05-17 13:54:23 UTC (rev 4189)
@@ -11,6 +11,8 @@
 
 #include "pgAgent.h"
 
+#include <fcntl.h>
+
 #ifdef WIN32
 #error this file is for unix only!
 #endif
@@ -21,14 +23,54 @@
         "Usage:\n"
         "pgagent [options] <connect-string>\n"
         "options:\n"
-        "-t <poll time interval>\n"
-        "-r <retry period after connection abort (>=10s)>\n"
-        "-c <connection pool size (>=5)>\n"
+        "-t <poll time interval in seconds (default 10)>\n"
+        "-r <retry period after connection abort in seconds (>=10, default 30)>\n"
+        "-c <connection pool size (>=5, default 5)>\n"
         );
 }
 
+void LogMessage(char *msg, int level)
+{
+    switch (level)
+    {
+        case LOG_DEBUG:
+            fprintf(stderr, "DEBUG: %s\n", msg);
+            break;
+        case LOG_WARNING:
+            fprintf(stderr, "WARNING: %s\n", msg);
+            break;
+        case LOG_ERROR:
+            fprintf(stderr, "ERROR: %s\n", msg);
+            exit(1);
+            break;
+    }
 
+}
 
+// Shamelessly lifted from pg_autovacuum...
+static void daemonize(void)
+{
+    pid_t pid;
+
+    pid = fork();
+    if (pid == (pid_t) -1)
+    {
+        LogMessage("Cannot disassociate from controlling TTY", LOG_ERROR);
+        exit(1);
+    }
+    else if (pid)
+        exit(0);
+
+#ifdef HAVE_SETSID
+    if (setsid() < 0)
+    {
+        LogMessage("Cannot disassociate from controlling TTY", LOG_ERROR);
+        exit(1);
+    }
+#endif
+
+}
+
 int main(int argc, char **argv)
 {
     if (argc < 2)
@@ -37,11 +79,24 @@
         return 1;
     }
 
+    argc--;
+    argv++;
+
     setOptions(argc, argv);
 
     DBconn *conn=DBconn::InitConnection(connectString);
     if (!conn->IsValid())
-        fatal("connection not valid: " + conn->GetLastError());
+    {
+        char tmp[255];
+        snprintf(tmp, 254, "Connection not valid: %s", conn->GetLastError().c_str());
+        LogMessage(tmp, LOG_ERROR);
+    }
 
+    serviceDBname = conn->GetDBname();
+
+    daemonize();
+
+    MainLoop();
+
     return 0;
 }

Modified: trunk/pgadmin3/xtra/pgagent/win32.cpp
===================================================================
--- trunk/pgadmin3/xtra/pgagent/win32.cpp	2005-05-17 13:53:30 UTC (rev 4188)
+++ trunk/pgadmin3/xtra/pgagent/win32.cpp	2005-05-17 13:54:23 UTC (rev 4189)
@@ -49,17 +49,43 @@
     serviceIsRunning = true;
 }
 
-
-
-void LogMessage(char *msg)
+void LogMessage(char *msg, int level)
 {
     if (eventHandle)
     {
-        // ReportEvent
+        // FIXME - This path should use the event log!
+        switch (level)
+        {
+            case LOG_DEBUG:
+                fprintf(stderr, "DEBUG: %s\n", msg);
+                break;
+            case LOG_WARNING:
+                fprintf(stderr, "WARNING: %s\n", msg);
+                break;
+            case LOG_ERROR:
+                fprintf(stderr, "ERROR: %s\n", msg);
+                exit(1);
+                break;
+        }
     }
+    else
+    {
+        switch (level)
+        {
+            case LOG_DEBUG:
+                fprintf(stderr, "DEBUG: %s\n", msg);
+                break;
+            case LOG_WARNING:
+                fprintf(stderr, "WARNING: %s\n", msg);
+                break;
+            case LOG_ERROR:
+                fprintf(stderr, "ERROR: %s\n", msg);
+                exit(1);
+                break;
+        }
+    }
 }
 
-
 // The main working thread of the service
 
 unsigned int __stdcall threadProcedure(void *unused)
@@ -257,9 +283,9 @@
         "-u <user>\n"
         "-p <password>\n"
         "-d <displayname>\n"
-        "-t <poll time interval>\n"
-        "-r <retry period after connection abort (>=10s)>\n"
-        "-c <connection pool size (>=5)>\n"
+        "-t <poll time interval in seconds (default 10)>\n"
+        "-r <retry period after connection abort in seconds (>=10, default 30)>\n"
+        "-c <connection pool size (>=5, default 5)>\n"
         );
 }
 
@@ -271,13 +297,17 @@
 {
     eventHandle = RegisterEventSource(0, serviceName.c_str());
     if (!eventHandle)
-        fatal("Couldn't register event handle.");
+        LogMessage("Couldn't register event handle.", LOG_ERROR);
 
     setOptions(argc, argv);
 
     DBconn *conn=DBconn::InitConnection(connectString);
     if (!conn->IsValid())
-        fatal("connection not valid: " + conn->GetLastError());
+    {
+        char tmp[255];
+        snprintf(tmp, 254, "Connection not valid: %s", conn->GetLastError().c_str());
+        LogMessage(tmp, LOG_ERROR);
+    }
 
     serviceDBname = conn->GetDBname();
 }
@@ -376,4 +406,4 @@
     }
 
     return;
-}
\ No newline at end of file
+}


pgadmin-hackers by date

Next:From: svnDate: 2005-05-17 14:12:52
Subject: SVN Commit by dpage: r4190 - trunk/pgadmin3/xtra/pgagent
Previous:From: svnDate: 2005-05-17 13:53:30
Subject: SVN Commit by dpage: r4188 - trunk/pgadmin3/xtra/pgagent

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group