Skip to content

Commit

Permalink
Merge pull request #29 from ldanielburr/master
Browse files Browse the repository at this point in the history
Replace all uses of twisted.python.log with twisted.logger.
  • Loading branch information
glyph committed Jul 12, 2019
2 parents f8ee2e7 + c78141d commit ae528e8
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 25 deletions.
9 changes: 7 additions & 2 deletions ampoule/child.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from twisted.python import log
from twisted import logger
from twisted.internet import error
from twisted.protocols import amp
from ampoule.commands import Echo, Shutdown, Ping



log = logger.Logger()


class AMPChild(amp.AMP):
def __init__(self):
super(AMPChild, self).__init__(self)
Expand Down Expand Up @@ -33,7 +38,7 @@ def shutdown(self):
This method is needed to shutdown the child gently without
generating an exception.
"""
log.msg("Shutdown message received, goodbye.")
log.info(u'Shutdown message received, goodbye.')
self.shutdown = True
return {}
Shutdown.responder(shutdown)
Expand Down
25 changes: 19 additions & 6 deletions ampoule/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@

from zope.interface import implementer

from twisted import logger
from twisted.internet import reactor, protocol, defer, error
from twisted.python import log, reflect
from twisted.python import reflect
from twisted.protocols import amp
from twisted.python import runtime
from twisted.python.compat import set

from ampoule import iampoule



log = logger.Logger()


gen = itertools.count()

if runtime.platform.isWindows():
Expand Down Expand Up @@ -63,7 +69,7 @@ def signalProcess(self, signalID):
return self.transport.signalProcess(signalID)

def connectionMade(self):
log.msg("Subprocess %s started." % (self.name,))
log.info(u'Subprocess {n} started.', n=self.name)
self.amp.makeConnection(self)

# Transport
Expand Down Expand Up @@ -94,10 +100,10 @@ def childDataReceived(self, childFD, data):

def errReceived(self, data):
for line in data.strip().splitlines():
log.msg("FROM %s: %s" % (self.name, line))
log.error(u'FROM {n}: {l}', n=self.name, l=line)

def processEnded(self, status):
log.msg("Process: %s ended" % (self.name,))
log.info(u'Process: {n} ended', n=self.name)
self.amp.connectionLost(status)
if status.check(error.ProcessDone):
self.finished.callback('')
Expand All @@ -111,8 +117,15 @@ def main(reactor, ampChildPath):
from twisted.application import reactors
reactors.installReactor(reactor)
from twisted.python import log
log.startLogging(sys.stderr)
from twisted import logger
observer = logger.textFileLogObserver(sys.stderr)
logLevelPredicate = logger.LogLevelFilterPredicate(
defaultLogLevel=logger.LogLevel.info
)
filteringObserver = logger.FilteringLogObserver(
observer, [logLevelPredicate]
)
logger.globalLogBeginner.beginLoggingTo([filteringObserver])
from twisted.internet import reactor, stdio
from twisted.python import reflect, runtime
Expand Down
53 changes: 36 additions & 17 deletions ampoule/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,34 @@
count = functools.partial(next, itertools.count())
pop = heapq.heappop

from twisted import logger
from twisted.internet import defer, task, error
from twisted.python import log

from ampoule import commands, main



log = logger.Logger()


STATS_TEMPLATE = u"""ProcessPool stats:
workers: {w}
timeout: {t}
parent: {p}
child: {c}
max idle: {i}
recycle after: {r}
ProcessStarter:
{s}"""


try:
DIE = signal.SIGKILL
except AttributeError:
# Windows doesn't have SIGKILL, let's just use SIGTERM then
DIE = signal.SIGTERM


class ProcessPool(object):
"""
This class generalizes the functionality of a pool of
Expand Down Expand Up @@ -63,7 +80,7 @@ def __init__(self, ampChild=None, ampParent=None, min=5, max=20,
self.starter = starter
self.ampChildArgs = tuple(ampChildArgs)
if starter is None:
self.starter = main.ProcessStarter(packages=("twisted", "ampoule"))
self.starter = main.ProcessStarter(packages=("twisted",))
self.ampParent = ampParent
self.ampChild = ampChild
if ampChild is None:
Expand Down Expand Up @@ -135,17 +152,18 @@ def _addProcess(self, child, finished):
Adds the newly created child process to the pool.
"""
def fatal(reason, child):
log.msg("FATAL: Process exited %s" % (reason,))
log.error(
u'FATAL: Process exited.\n\t{r}', r=reason.getErrorMessage()
)
self._pruneProcess(child)

def dieGently(data, child):
log.msg("STOPPING: '%s'" % (data,))
log.info(u'STOPPING: {s}', s=data)
self._pruneProcess(child)

self.processes.add(child)
self.ready.add(child)
finished.addCallback(dieGently, child
).addErrback(fatal, child)
finished.addCallback(dieGently, child).addErrback(fatal, child)
self._finishCallbacks[child] = finished
self._lastUsage[child] = now()
self._calls[child] = 0
Expand Down Expand Up @@ -314,8 +332,8 @@ def stopAWorker(self, child=None):
"""
Gently stop a child so that it's not restarted anymore
@param command: an L{ampoule.child.AmpChild} type object.
@type command: L{ampoule.child.AmpChild} or None
@param child: an L{ampoule.child.AmpChild} type object.
@type child: L{ampoule.child.AmpChild} or None
"""
if child is None:
Expand Down Expand Up @@ -377,15 +395,16 @@ def _cb(_):
return defer.DeferredList(l).addCallback(_cb)

def dumpStats(self):
log.msg("ProcessPool stats:")
log.msg('\tworkers: %s' % len(self.processes))
log.msg('\ttimeout: %s' % (self.timeout))
log.msg('\tparent: %r' % (self.ampParent,))
log.msg('\tchild: %r' % (self.ampChild,))
log.msg('\tmax idle: %r' % (self.maxIdle,))
log.msg('\trecycle after: %r' % (self.recycleAfter,))
log.msg('\tProcessStarter:')
log.msg('\t\t%r' % (self.starter,))
log.info(
STATS_TEMPLATE,
w=len(self.processes),
t=self.timeout,
p=self.ampParent,
c=self.ampChild,
i=self.maxIdle,
r=self.recycleAfter,
s=self.starter
)

pp = None

Expand Down

0 comments on commit ae528e8

Please sign in to comment.