def Shutdown(self, wait_secs=10):
    """Shuts down the broker server.

    Args:
      wait_secs: (float) The maximum time to wait for the broker to shutdown.

    Raises:
      BrokerError: If shutdown failed.
    """
    if self._process:
      try:
        execution_utils.KillSubprocess(self._process)
        self._process = None
        if self._comm_thread:
          self._comm_thread.join()
          self._comm_thread = None
      except RuntimeError as e:
        log.warn('Failed to shutdown broker: %s' % e)
        raise BrokerError('Broker failed to shutdown: %s' % e)
    else:
      # Invoke the /shutdown handler.
      try:
        self._SendJsonRequest('POST', '/shutdown')
      except RequestSocketError as e:
        if e.errno not in (SocketConnRefusedErrno(), SocketConnResetErrno()):
          raise
        # We may get an exception reading the response to the shutdown
        # request, because the shutdown may preempt the response.

    if not _Await(lambda: not self.IsRunning(), wait_secs):
      log.warn('Failed to shutdown broker: still running after {0}s'.format(
          wait_secs))
      raise BrokerError('Broker failed to shutdown: timed-out')

    log.info('Shutdown broker.')
Beispiel #2
0
    def Shutdown(self, wait_secs=10):
        """Shuts down the broker server."""
        if self._process:
            try:
                execution_utils.KillSubprocess(self._process)
                self._process = None
                if self._comm_thread:
                    self._comm_thread.join()
                    self._comm_thread = None
            except RuntimeError as e:
                log.warn('Failed to shutdown broker: %s' % e)
                raise BrokerError('Broker failed to shutdown: %s' % e)
        else:
            # Invoke the /shutdown handler.
            try:
                self._SendJsonRequest('POST', '/shutdown')
            except IOError:
                raise BrokerError('Broker failed to shutdown: '
                                  'failed to send shutdown request')
            except httplib.HTTPException:
                # We may get an exception reading the response to the shutdown request,
                # because the shutdown may preempt the response.
                pass

        if not _Await(lambda: not self.IsRunning(), wait_secs):
            log.warn(
                'Failed to shutdown broker: still running after {0}s'.format(
                    wait_secs))
            raise BrokerError('Broker failed to shutdown: timed-out')

        log.info('Shutdown broker.')
Beispiel #3
0
 def _KillSubprocessIgnoreErrors(self, process):
   try:
     execution_utils.KillSubprocess(process)
   except RuntimeError:
     # Attempting to kill processes that are no longer running on Windows
     # yields an error which we can safely ignore.
     pass
Beispiel #4
0
 def Wait(self):
   self.thread.join(self.timeout)
   if self.exc_info:
     exceptions.reraise(self.exc_info[1], tb=self.exc_info[2])
   if self.thread.isAlive():
     execution_utils.KillSubprocess(self.p)
     # Give the thread a chance to clean up if it can, now that we killed the
     # subprocess.
     self.thread.join(_ProcessRunner.fallback_timeout)
     timeout_message = 'The process timed out: {0}'.format(
         ' '.join(self.args))
     raise TimeoutError(result=self.result, msg=timeout_message)
Beispiel #5
0
 def Close(self):
   if self.__runner and self.__runner.p:
     execution_utils.KillSubprocess(self.__runner.p)
     return True
   return False