<!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>[12037] CalendarServer/trunk/twext/application/masterchild.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/12037">12037</a></dd>
<dt>Author</dt> <dd>wsanchez@apple.com</dd>
<dt>Date</dt> <dd>2013-12-04 19:55:04 -0800 (Wed, 04 Dec 2013)</dd>
</dl>
<h3>Log Message</h3>
<pre>Echo works.
Reporting doesn't.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServertrunktwextapplicationmasterchildpy">CalendarServer/trunk/twext/application/masterchild.py</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServertrunktwextapplicationmasterchildpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/twext/application/masterchild.py (12036 => 12037)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/twext/application/masterchild.py        2013-12-05 02:50:05 UTC (rev 12036)
+++ CalendarServer/trunk/twext/application/masterchild.py        2013-12-05 03:55:04 UTC (rev 12037)
</span><span class="lines">@@ -20,6 +20,12 @@
</span><span class="cx"> child processes.
</span><span class="cx"> """
</span><span class="cx">
</span><ins>+# python -c 'from twisted.scripts.twistd import run; run()' \
+# -n -l - master --protocol=twext.protocols.echo.EchoProtocol --port=8080
+
+from __future__ import print_function
+
+
</ins><span class="cx"> __all__ = [
</span><span class="cx"> "MasterOptions",
</span><span class="cx"> "MasterServiceMaker",
</span><span class="lines">@@ -29,6 +35,8 @@
</span><span class="cx">
</span><span class="cx">
</span><span class="cx"> import sys
</span><ins>+from os import close, unlink
+from tempfile import mkstemp
</ins><span class="cx">
</span><span class="cx"> from zope.interface import implementer
</span><span class="cx">
</span><span class="lines">@@ -48,8 +56,10 @@
</span><span class="cx"> from twext.internet.sendfdport import IStatusWatcher
</span><span class="cx"> from twext.internet.sendfdport import InheritedPort
</span><span class="cx">
</span><ins>+log = Logger()
</ins><span class="cx">
</span><span class="cx">
</span><ins>+
</ins><span class="cx"> class MasterOptions(Options):
</span><span class="cx"> """
</span><span class="cx"> Options for a master process.
</span><span class="lines">@@ -60,11 +70,11 @@
</span><span class="cx"> Protocol
</span><span class="cx"> """
</span><span class="cx"> try:
</span><del>- protocol = namedClass(value)
</del><ins>+ namedClass(value)
</ins><span class="cx"> except (ValueError, AttributeError):
</span><span class="cx"> raise UsageError("Unknown protocol: {0}".format(value))
</span><span class="cx">
</span><del>- self["protocol"] = protocol
</del><ins>+ self["protocol"] = value
</ins><span class="cx">
</span><span class="cx">
</span><span class="cx"> def opt_port(self, value):
</span><span class="lines">@@ -122,9 +132,6 @@
</span><span class="cx"> def makeService(self, options):
</span><span class="cx"> service = MultiService()
</span><span class="cx">
</span><del>- port = options["port"]
- childProtocol = options["protocol"]
-
</del><span class="cx"> # Dispatcher
</span><span class="cx"> statusWatcher = StatusWatcher()
</span><span class="cx"> dispatcher = InheritedSocketDispatcher(statusWatcher)
</span><span class="lines">@@ -135,9 +142,9 @@
</span><span class="cx">
</span><span class="cx"> # TCP Service
</span><span class="cx"> tcpFactory = SpawningInheritingProtocolFactory(
</span><del>- dispatcher, spawningService, childProtocol
</del><ins>+ dispatcher, spawningService, options["protocol"]
</ins><span class="cx"> )
</span><del>- tcpService = TCPServer(port, tcpFactory)
</del><ins>+ tcpService = TCPServer(options["port"], tcpFactory)
</ins><span class="cx">
</span><span class="cx"> tcpService.setServiceParent(service)
</span><span class="cx">
</span><span class="lines">@@ -207,16 +214,27 @@
</span><span class="cx">
</span><span class="cx"> processProtocol = ChildProcessProtocol(self, inheritedSocket)
</span><span class="cx">
</span><ins>+ # Annoyingly, twistd *has* to make a pid file.
+ pidFileFD, pidFileName = mkstemp()
+ close(pidFileFD)
+ unlink(pidFileName)
+
</ins><span class="cx"> arguments = (
</span><span class="cx"> sys.executable, b"-c",
</span><span class="cx"> b"from twisted.scripts.twistd import run; run()",
</span><del>- b"--pidfile", b"/dev/null",
- b"--logfile", b"-",
</del><ins>+ b"--pidfile", pidFileName,
+ b"--nodaemon", b"--logfile", b"-",
</ins><span class="cx"> self.pluginName,
</span><span class="cx"> b"--inherited-fd=3",
</span><span class="cx"> b"--protocol", protocolName,
</span><span class="cx"> )
</span><span class="cx">
</span><ins>+ self.log.debug(
+ u"Spawning child process for protocol {protocol!r} "
+ u"with arguments: {arguments}",
+ protocol=protocolName, arguments=arguments,
+ )
+
</ins><span class="cx"> transport = reactor.spawnProcess(
</span><span class="cx"> processProtocol,
</span><span class="cx"> sys.executable, arguments, env={
</span><span class="lines">@@ -228,8 +246,8 @@
</span><span class="cx"> child = ChildProcess(transport, processProtocol)
</span><span class="cx">
</span><span class="cx"> self.log.info(
</span><del>- u"Spawned child process ({child.transport.pid}) "
- u"for protocol {protocol!r}: {arguments}",
</del><ins>+ u"Spawned child process #{child.transport.pid} "
+ u"for protocol {protocol!r}",
</ins><span class="cx"> child=child, protocol=protocolName, arguments=arguments,
</span><span class="cx"> )
</span><span class="cx">
</span><span class="lines">@@ -369,13 +387,23 @@
</span><span class="cx">
</span><span class="cx">
</span><span class="cx"> def startService(self):
</span><del>- self.wrappedProtocolFactory = ReportingWrapperFactory(
</del><ins>+ factory = ReportingWrapperFactory(
</ins><span class="cx"> self.protocolFactory, self.fd, self.createTransport
</span><span class="cx"> )
</span><ins>+ self.wrappedProtocolFactory = factory
+
+ factory.inheritedPort.startReading()
+ factory.inheritedPort.reportStatus("0")
+
</ins><span class="cx"> return super(ChildService, self).startService()
</span><span class="cx">
</span><span class="cx">
</span><span class="cx"> def stopService(self):
</span><ins>+ factory = self.wrappedProtocolFactory
+
+ factory.inheritedPort.stopReading()
+ factory.allConnectionsClosed()
+
</ins><span class="cx"> return super(ChildService, self).stopService()
</span><span class="cx">
</span><span class="cx">
</span><span class="lines">@@ -385,17 +413,23 @@
</span><span class="cx"> """
</span><span class="cx"> from twisted.internet import reactor
</span><span class="cx">
</span><del>- self.wrappedFactory.inheritedPort.reportStatus("+")
</del><ins>+ factory = self.wrappedProtocolFactory
+ factory.inheritedPort.reportStatus("+")
+ log.info("{factory.inheritedPort.statusQueue}", factory=factory)
</ins><span class="cx">
</span><span class="cx"> socketFD = socket.fileno()
</span><del>- return reactor.adoptStreamConnection(
- socketFD, getsockfam(socketFD), self.wrappedProtocolFactory
</del><ins>+ transport = reactor.adoptStreamConnection(
+ socketFD, getsockfam(socketFD), factory
</ins><span class="cx"> )
</span><ins>+ transport.startReading()
</ins><span class="cx">
</span><ins>+ return transport
</ins><span class="cx">
</span><span class="cx">
</span><ins>+
</ins><span class="cx"> class ReportingProtocolWrapper(ProtocolWrapper, object):
</span><span class="cx"> def connectionLost(self, reason):
</span><ins>+ log.info("CONNECTION LOST")
</ins><span class="cx"> self.factory.inheritedPort.reportStatus("-")
</span><span class="cx"> return super(ReportingProtocolWrapper, self).connectionLost(reason)
</span><span class="cx">
</span><span class="lines">@@ -410,19 +444,31 @@
</span><span class="cx">
</span><span class="cx">
</span><span class="cx">
</span><ins>+class Status(object):
+ def __init__(self, sentCount, ackedCount):
+ self.sentCount = sentCount
+ self.ackedCount = ackedCount
+
+ def __repr__(self):
+ return "({self.sentCount},{self.ackedCount})".format(self=self)
+
+
+
</ins><span class="cx"> @implementer(IStatusWatcher)
</span><span class="cx"> class StatusWatcher(object):
</span><span class="cx"> """
</span><del>- This enabled the dispatcher to keep track of how many connections are in
</del><ins>+ This enables the dispatcher to keep track of how many connections are in
</ins><span class="cx"> flight for each child.
</span><span class="cx"> """
</span><span class="cx"> @staticmethod
</span><span class="cx"> def initialStatus():
</span><ins>+ log.info("Status: init")
</ins><span class="cx"> return Status(sentCount=0, ackedCount=0)
</span><span class="cx">
</span><span class="cx">
</span><span class="cx"> @staticmethod
</span><span class="cx"> def newConnectionStatus(previousStatus):
</span><ins>+ log.info("Status: {0} new".format(previousStatus))
</ins><span class="cx"> return Status(
</span><span class="cx"> sentCount=previousStatus.sentCount + 1,
</span><span class="cx"> ackedCount=previousStatus.ackedCount,
</span><span class="lines">@@ -431,6 +477,7 @@
</span><span class="cx">
</span><span class="cx"> @staticmethod
</span><span class="cx"> def statusFromMessage(previousStatus, message):
</span><ins>+ log.info("Status: {0}{1!r}".format(previousStatus, message))
</ins><span class="cx"> if message == b"-":
</span><span class="cx"> return Status(
</span><span class="cx"> sentCount=previousStatus.sentCount - 1,
</span><span class="lines">@@ -447,6 +494,7 @@
</span><span class="cx">
</span><span class="cx"> @staticmethod
</span><span class="cx"> def closeCountFromStatus(previousStatus):
</span><ins>+ log.info("Status: {0} close".format(previousStatus))
</ins><span class="cx"> return (
</span><span class="cx"> previousStatus.ackedCount,
</span><span class="cx"> Status(
</span><span class="lines">@@ -458,13 +506,7 @@
</span><span class="cx">
</span><span class="cx"> @staticmethod
</span><span class="cx"> def statusesChanged(statuses):
</span><del>- # FIXME: THis isn't in IStatusWatcher, but is called by
</del><ins>+ log.info("Status changed: {0}".format(tuple(statuses)))
+ # FIXME: This isn't in IStatusWatcher, but is called by
</ins><span class="cx"> # InheritedSocketDispatcher.
</span><span class="cx"> pass
</span><del>-
-
-
-class Status(object):
- def __init__(self, sentCount, ackedCount):
- self.sentCount = sentCount
- self.ackedCount = ackedCount
</del></span></pre>
</div>
</div>
</body>
</html>