[CalendarServer-changes] [14494] CalendarServer/trunk/txdav/base/datastore/subpostgres.py

source_changes at macosforge.org source_changes at macosforge.org
Mon Mar 2 15:01:45 PST 2015


Revision: 14494
          http://trac.calendarserver.org//changeset/14494
Author:   wsanchez at apple.com
Date:     2015-03-02 15:01:45 -0800 (Mon, 02 Mar 2015)
Log Message:
-----------
Lookup path to initdb and pg_ctl once.
If not found, raise InternalDataStoreError with a useful message instead of IndexError with no useful info.
Change log.warn to log.info in places that are warning you about common things.

Modified Paths:
--------------
    CalendarServer/trunk/txdav/base/datastore/subpostgres.py

Modified: CalendarServer/trunk/txdav/base/datastore/subpostgres.py
===================================================================
--- CalendarServer/trunk/txdav/base/datastore/subpostgres.py	2015-03-02 21:48:11 UTC (rev 14493)
+++ CalendarServer/trunk/txdav/base/datastore/subpostgres.py	2015-03-02 23:01:45 UTC (rev 14494)
@@ -38,6 +38,7 @@
 from twisted.internet.defer import Deferred
 from txdav.base.datastore.dbapiclient import DBAPIConnector
 from txdav.base.datastore.dbapiclient import postgresPreflight
+from txdav.common.icommondatastore import InternalDataStoreError
 
 from twisted.application.service import MultiService
 
@@ -48,6 +49,7 @@
 _MAGIC_READY_COOKIE = "database system is ready to accept connections"
 
 
+
 class _PostgresMonitor(ProcessProtocol):
     """
     A monitoring protocol which watches the postgres subprocess.
@@ -77,17 +79,17 @@
 
 
     def outReceived(self, out):
-        log.warn("received postgres stdout {out!r}", out=out)
+        log.info("received postgres stdout {out!r}", out=out)
         # self.lineReceiver.dataReceived(out)
 
 
     def errReceived(self, err):
-        log.warn("received postgres stderr {err}", err=err)
+        log.info("received postgres stderr {err}", err=err)
         self.lineReceiver.dataReceived(err)
 
 
     def processEnded(self, reason):
-        log.warn(
+        log.info(
             "postgres process ended with status {status}",
             status=reason.value.status
         )
@@ -98,7 +100,7 @@
         if reason.value.status == 0:
             self.completionDeferred.callback(None)
         else:
-            log.warn("Could not start postgres; see postgres.log")
+            log.error("Could not start postgres; see postgres.log")
             self.completionDeferred.errback(reason)
 
 
@@ -266,8 +268,17 @@
         self.schema = schema
         self.monitor = None
         self.openConnections = []
-        self._pgCtl = pgCtl
-        self._initdb = initDB
+
+        def locateCommand(name, cmd):
+            for found in which(cmd):
+                return found
+
+            raise InternalDataStoreError(
+                "Unable to locate {} command: {}".format(name, cmd)
+            )
+
+        self._pgCtl = locateCommand("pg_ctl", pgCtl)
+        self._initdb = locateCommand("initdb", initDB)
         self._reactor = reactor
         self._postgresPid = None
 
@@ -280,17 +291,6 @@
         return self._reactor
 
 
-    def pgCtl(self):
-        """
-        Locate the path to pg_ctl.
-        """
-        return which(self._pgCtl)[0]
-
-
-    def initdb(self):
-        return which(self._initdb)[0]
-
-
     def activateDelayedShutdown(self):
         """
         Call this when starting database initialization code to
@@ -427,7 +427,8 @@
                 )
             except postgres.DatabaseError as e:
                 log.error(
-                    "Unable to connect to database for schema creation: {error}",
+                    "Unable to connect to database for schema creation:"
+                    " {error}",
                     error=e
                 )
                 raise
@@ -436,7 +437,6 @@
             return createDatabaseConn, createDatabaseCursor
 
         monitor = _PostgresMonitor(self)
-        pgCtl = self.pgCtl()
         # check consistency of initdb and postgres?
 
         options = []
@@ -477,14 +477,14 @@
         if self.testMode:
             options.append("-c log_statement=all")
 
-        log.warn(
+        log.info(
             "Requesting postgres start via {cmd} {opts}",
-            cmd=pgCtl, opts=options
+            cmd=self._pgCtl, opts=options
         )
         self.reactor.spawnProcess(
-            monitor, pgCtl,
+            monitor, self._pgCtl,
             [
-                pgCtl,
+                self._pgCtl,
                 "start",
                 "-l", self.logFile,
                 "-t 86400",  # Give plenty of time for a long cluster upgrade
@@ -517,12 +517,12 @@
             We started postgres; we're responsible for stopping it later.
             Call pgCtl status to get the pid.
             """
-            log.warn("{cmd} exited", cmd=pgCtl)
+            log.info("{cmd} exited", cmd=self._pgCtl)
             self.shouldStopDatabase = True
             d = Deferred()
             statusMonitor = CapturingProcessProtocol(d, None)
             self.reactor.spawnProcess(
-                statusMonitor, pgCtl, [pgCtl, "status"],
+                statusMonitor, self._pgCtl, [self._pgCtl, "status"],
                 env=self.env, path=self.workingDir.path,
                 uid=self.uid, gid=self.gid,
             )
@@ -537,7 +537,7 @@
             d = Deferred()
             statusMonitor = CapturingProcessProtocol(d, None)
             self.reactor.spawnProcess(
-                statusMonitor, pgCtl, [pgCtl, "status"],
+                statusMonitor, self._pgCtl, [self._pgCtl, "status"],
                 env=self.env, path=self.workingDir.path,
                 uid=self.uid, gid=self.gid,
             )
@@ -565,11 +565,10 @@
         env.update(PGDATA=clusterDir.path,
                    PGHOST=self.host,
                    PGUSER=self.spawnedDBUser)
-        initdb = self.initdb()
 
         if self.socketDir:
             if not self.socketDir.isdir():
-                log.warn("Creating {dir}", dir=self.socketDir.path)
+                log.info("Creating {dir}", dir=self.socketDir.path)
                 self.socketDir.createDirectory()
 
             if self.uid and self.gid:
@@ -578,11 +577,11 @@
             os.chmod(self.socketDir.path, 0770)
 
         if not self.dataStoreDirectory.isdir():
-            log.warn("Creating {dir}", dir=self.dataStoreDirectory.path)
+            log.info("Creating {dir}", dir=self.dataStoreDirectory.path)
             self.dataStoreDirectory.createDirectory()
 
         if not self.workingDir.isdir():
-            log.warn("Creating {dir}", dir=self.workingDir.path)
+            log.info("Creating {dir}", dir=self.workingDir.path)
             self.workingDir.createDirectory()
 
         if self.uid and self.gid:
@@ -591,11 +590,12 @@
 
         if not clusterDir.isdir():
             # No cluster directory, run initdb
-            log.warn("Running initdb for {dir}", dir=clusterDir.path)
+            log.info("Running initdb for {dir}", dir=clusterDir.path)
             dbInited = Deferred()
             self.reactor.spawnProcess(
                 CapturingProcessProtocol(dbInited, None),
-                initdb, [initdb, "-E", "UTF8", "-U", self.spawnedDBUser],
+                self._initdb,
+                [self._initdb, "-E", "UTF8", "-U", self.spawnedDBUser],
                 env=env, path=self.workingDir.path,
                 uid=self.uid, gid=self.gid,
             )
@@ -612,7 +612,7 @@
             dbInited.addCallback(doCreate)
 
         else:
-            log.warn("Cluster already exists at {dir}", dir=clusterDir.path)
+            log.info("Cluster already exists at {dir}", dir=clusterDir.path)
             self.startDatabase()
 
 
@@ -634,11 +634,10 @@
             # database.  (This also happens in command-line tools.)
             if self.shouldStopDatabase:
                 monitor = _PostgresMonitor()
-                pgCtl = self.pgCtl()
                 # FIXME: why is this 'logfile' and not self.logfile?
                 self.reactor.spawnProcess(
-                    monitor, pgCtl,
-                    [pgCtl, "-l", "logfile", "stop"],
+                    monitor, self._pgCtl,
+                    [self._pgCtl, "-l", "logfile", "stop"],
                     env=self.env, path=self.workingDir.path,
                     uid=self.uid, gid=self.gid,
                 )
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20150302/c4f6df77/attachment-0001.html>


More information about the calendarserver-changes mailing list