def start(self): self._state = self.RUNNING if self.register_signals: self.register_signal_handler() self.on_start() try: for component in self.components: self._debug('Starting %s...', qualname(component)) if component is not None: component.start() self._running += 1 self._debug('%s OK!', qualname(component)) except SystemTerminate as exc: self._debug('Terminating server: %r', exc, exc_info=True) self.terminate() except Exception as exc: self._error('Unrecoverable error: %r', exc, exc_info=True) self.stop() except (KeyboardInterrupt, SystemExit): self._debug('Terminating from keyboard') self.stop() self._debug('Controller started!') self.after_start() # we can't simply execute Event.wait because signal processing will # not work in this case while self.wait_for_shutdown and not self._shutdown_complete.is_set(): self._shutdown_complete.wait(1e100)
def start(self): self._state = self.RUNNING if self.register_signals: self.register_signal_handler() self.on_start() try: for component in self.components: self._debug('Starting %s...', qualname(component)) if component is not None: self._starting.append(component) component.start() self._running += 1 self._debug('%s OK!', qualname(component)) except SystemTerminate as exc: self._debug('Terminating server: %r', exc, exc_info=True) self.terminate() except Exception as exc: self._error('Unrecoverable error: %r', exc, exc_info=True) self.terminate() except (KeyboardInterrupt, SystemExit): self._debug('Terminating from keyboard') self.stop() finally: self._starting = [] self._info('Controller %r started!', self) self.after_start() self._signals.wait() self._info('Controller %r stopped!', self)
def _shutdown(self, warm=True): what = 'Stopping' if warm else 'Terminating' if self._state in (self.CLOSED, self.TERMINATED): return try: if not self.is_running or \ self._running != len(self.components): # Call abort on starting components. for component in self._starting[:]: self._debug('Aborting %s...', qualname(component)) if hasattr(component, 'abort'): component.abort() # Not fully started, can safely exit. self._state = self.TERMINATED return self._state = self.CLOSED for component in reversed(self.components): self._debug('%s %s...', what, qualname(component)) if component: stop = component.stop if not warm: stop = getattr(component, 'terminate', None) or stop stop() self.on_shutdown() self._debug('Whole controller stopped!') self._state = self.TERMINATED except Exception as exc: self._error('Unrecoverable error: %r', exc, exc_info=True) finally: self._signals.set()
def _shutdown(self, warm=True): what = 'Stopping' if warm else 'Terminating' if self._state in (self.CLOSED, self.TERMINATED): return if self._state != self.RUNNING or self._running != len(self.components): # Not fully started, can safely exit. self._state = self.TERMINATED self._shutdown_complete.set() return self._state = self.CLOSED for component in reversed(self.components): self._debug('%s %s...', what, qualname(component)) if component: stop = component.stop if not warm: stop = getattr(component, 'terminate', None) or stop stop() self.on_shutdown() self._debug('Controller stopped!') self._state = self.TERMINATED self._shutdown_complete.set()
def _shutdown(self, warm=True): what = 'Stopping' if warm else 'Terminating' if self._state in (self.CLOSED, self.TERMINATED): return if self._state != self.RUNNING or self._running != len( self.components): # Not fully started, can safely exit. self._state = self.TERMINATED self._shutdown_complete.set() return self._state = self.CLOSED for component in reversed(self.components): self._debug('%s %s...', what, qualname(component)) if component: stop = component.stop if not warm: stop = getattr(component, 'terminate', None) or stop stop() self.on_shutdown() self._debug('Controller stopped!') self._state = self.TERMINATED self._shutdown_complete.set()
def __reduce__(self): service_name, processor_cls, handler_cls = self processor_cls = qualname(processor_cls) handler_cls = qualname(handler_cls) return (self.__class__, (service_name, processor_cls, handler_cls))