<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[14494] CalendarServer/trunk/txdav/base/datastore/subpostgres.py</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.calendarserver.org//changeset/14494">14494</a></dd>
<dt>Author</dt> <dd>wsanchez@apple.com</dd>
<dt>Date</dt> <dd>2015-03-02 15:01:45 -0800 (Mon, 02 Mar 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>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.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServertrunktxdavbasedatastoresubpostgrespy">CalendarServer/trunk/txdav/base/datastore/subpostgres.py</a></li>
</ul>

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

</body>
</html>