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

Jp Calderone exarkun at divmod.com
Fri May 21 12:56:48 EDT 2004


Bill la Forge wrote:
> If a service starts successfully, once initialization completes
> successfully, it's stopService will be called for proper cleanup.
>  
> However, if another service fails to successfully initialize, and it
> is kind enough to call reactor.stop() rather than throw an exception,
> then the first service can, at the end of its initialization, call
> reactor.addSystemEventTrigger('before','shutdown',self.failedStartup),
> then failedStartup() will be called and there is STILL a chance to
> cleanup.
>  

   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




More information about the Twisted-Python mailing list