[Twisted-Python] service takedown (one newbie to other newbies, methinks)
Bill la Forge
laforge49 at yahoo.co.in
Sat May 22 06:58:04 EDT 2004
OK, I've got a better test environment for Tiered,
and found yet another bug. ;-)
I've also done a bit of refactoring. A terminology
change, actually. When the TieredService is started,
it initializes subordinate services. It only starts
them when the reactor is actually running.
The idea is that its easy to immediately cancel
a service which is initialized, but not yet actively
being used. And I'm thinking (feedback requested!),
once a service is started, it is fair game to go
ahead and use it. Thus the delay in starting
subordinate services until the need for cancelation
is truely past.
Bill
from twisted.application import service
from twisted.internet import reactor
from twisted.python import log
class SubordinateService(service.Service):
def __getstate__(self):
d = service.Service.__getstate__(self)
for attr in self.volatile:
if d.has_key(attr):
del d[attr]
return d
volatile=[]
def initService(self):
"""
Initialize the service, prior to starting.
"""
def cancelService(self):
"""
Immediate cancelation of the subordinate
service
which has been initialized but not started.
"""
class TieredService(service.MultiService):
"""
Subordinate services should raise an
exception when unable to init.
"""
def __init__(self,stopOnStartFailure=False):
service.MultiService.__init__(self)
self.stopOnStartFailure=stopOnStartFailure
def __getstate__(self):
d = service.Service.__getstate__(self)
for attr in self.volatile:
if d.has_key(attr):
del d[attr]
return d
volatile=['shutdownTrigger','initialized']
def startService(self):
self.initialized=[]
for svc in self:
try:
svc.initService()
except:
log.err()
self._cancelService()
return
else:
self.initialized.append(svc)
self.shutdownTrigger=reactor \
.addSystemEventTrigger(
'before',
'shutdown',
self._cancelService)
reactor.callWhenRunning(self._nowRunning)
service.Service.startService(self)
def _cancelService(self):
"""
Immediate cancelation of the
subordinate service(s).
"""
service.Service.stopService(self)
for svc in self.initialized[::-1]:
svc.cancelService()
if self.stopOnStartFailure:
reactor.stop()
def _nowRunning(self):
reactor.removeSystemEventTrigger(
self.shutdownTrigger)
self.shutdownTrigger=None
self.initialized=None
service.MultiService.startService(self)
________________________________________________________________________
Yahoo! India Matrimony: Find your partner online. http://yahoo.shaadi.com/india-matrimony/
More information about the Twisted-Python
mailing list