[Twisted-Python] service takedown (one newbie to other newbies, methinks)

Bill la Forge laforge49 at yahoo.co.in
Sat May 22 04:58:29 EDT 2004


One problem with BlahMultiService is that a service 
may need to defer on the stopService, though it seems
reasonable to expect an immediate return when
canceling
a service when startup has not yet completed.

So I'm adding a method--cancelService--which requires
an immediate return.

Another issue, I'm duplicating the code from 
twisted.application.internet._VolatileDataService,
because of the leading '_'. ;-(

Anyway, here's what I've come up with... --b

# moderately tested, minimally configurable
class TieredService(service.MultiService):
    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
    volitile=['shutdownTrigger','started']
    def startService(self):
        self.started=[]
        for service in self:
            try:
                service.startService()
            except:
                self.cancelService()
                return
            else:
                self.started.append(service)
       
self.shutdownTrigger=reactor.addSystemEventTrigger('before','shutdown',self.cancelService)
        reactor.callWhenRunning(self.nowRunning)
        service.Service.startService(self)
    def nowRunning(self):
       
reactor.removeSystemEventTrigger(self.shutdownTrigger)
        self.shutdownTrigger=None
        self.servicesStarted=None
    def cancelService(self):
        service.Service.stopService(self)
        for svc in self.started[::-1]:
            svc.cancelService()
        if self.stopOnStartFailure:
            reactor.stop()



--- Jp Calderone <exarkun at divmod.com> wrote:
>    I think a better solution would be to write an
> IServiceCollection 
> (MultiService subclass, probably) that knows how to
> deal with exceptions 
> raised from child services' startService methods.
> 
>    The idea is the same, but the framework gets to
> handle more of the 
> redundant boilerplate (untested):
> 
>      class BlahMultiService(MultiService):
>          def startService(self):
>              self.running = True
>              started = []
>              for svc in self:
>                  try:
>                      svc.startService()
>                  except:
>                      err = Failure()
>                      for svc in started[::-1]:
>                          try:
>                              svc.stopService()
>                          except:
>                              log.err()
>                      self.startupHadError(err)
> 
>                  else:
>                      started.append(svc)
> 
>          def startupHadError(self, failure):
>              # Could raise an exception here, or log
> it, or
>              # shut down the reactor, or whatever is
> appropriate
> 
>    Jp


________________________________________________________________________
Yahoo! India Matrimony: Find your partner online. http://yahoo.shaadi.com/india-matrimony/




More information about the Twisted-Python mailing list