Ejemplo n.º 1
0
    def wait_for(self,
                 connection_observer,
                 connection_observer_future,
                 timeout=None):
        """
        Await for connection_observer running in background or timeout.

        :param connection_observer: The one we are awaiting for.
        :param connection_observer_future: Future of connection-observer returned from submit().
        :param timeout: Max time (in float seconds) you want to await before you give up. If None then taken from connection_observer
        :return:
        """
        self.logger.debug("go foreground: {!r} - await max. {} [sec]".format(
            connection_observer, timeout))
        start_time = connection_observer.start_time
        remain_time = connection_observer.timeout
        check_timeout_from_observer = True
        wait_tick = 0.1
        if timeout:
            remain_time = timeout
            check_timeout_from_observer = False
            wait_tick = remain_time
        while remain_time > 0.0:
            done, not_done = wait([connection_observer_future],
                                  timeout=wait_tick)
            if connection_observer_future in done:
                connection_observer_future._stop()
                result = connection_observer_future.result()
                self.logger.debug("{} returned {}".format(
                    connection_observer, result))
                return None
            if check_timeout_from_observer:
                timeout = connection_observer.timeout
            remain_time = timeout - (time.time() - start_time)

        # code below is for timed out observer
        passed = time.time() - start_time
        self.logger.debug("timed out {}".format(connection_observer))
        connection_observer_future.cancel()
        # TODO: rethink - on timeout we raise while on other exceptions we expect observers
        #       just to call  observer.set_exception() - so, no raise before calling observer.result()
        if connection_observer.is_command():
            exception = CommandTimeout(connection_observer,
                                       timeout,
                                       kind="await_done",
                                       passed_time=passed)
        else:
            exception = ConnectionObserverTimeout(connection_observer,
                                                  timeout,
                                                  kind="await_done",
                                                  passed_time=passed)
        connection_observer.set_exception(exception)
        connection_observer.on_timeout()
        connection_observer._log(
            logging.INFO,
            "'{}.{}' has timed out after '{:.2f}' seconds.".format(
                connection_observer.__class__.__module__,
                connection_observer.__class__.__name__,
                time.time() - start_time))
        return None
Ejemplo n.º 2
0
    def _timeout_observer(self, connection_observer, timeout, passed_time, runner_logger, kind="background_run"):
        """
        Set connection_observer status to timed-out
        :param connection_observer: ConnectionObserver instance (command or event)
        :param timeout: timeout
        :param passed_time: passed time
        :param runner_logger: runner logger
        :param kind: Kind of running
        :return: None
        """
        if not connection_observer.life_status.was_on_timeout_called:
            connection_observer.life_status.was_on_timeout_called = True
            if not connection_observer.done():
                if connection_observer.is_command():
                    exception = CommandTimeout(connection_observer=connection_observer,
                                               timeout=timeout, kind=kind, passed_time=passed_time)
                else:
                    exception = ConnectionObserverTimeout(connection_observer=connection_observer,
                                                          timeout=timeout, kind=kind, passed_time=passed_time)
                connection_observer.set_exception(exception)
                connection_observer.on_timeout()

                observer_info = "{}.{}".format(connection_observer.__class__.__module__, connection_observer)
                timeout_msg = "has timed out after {:.2f} seconds.".format(passed_time)
                msg = "{} {}".format(observer_info, timeout_msg)

                # levels_to_go_up: extract caller info to log where .time_out_observer has been called from
                connection_observer._log(logging.INFO, msg, levels_to_go_up=2)
                log_into_logger(runner_logger, level=logging.DEBUG,
                                msg="{} {}".format(connection_observer, timeout_msg),
                                levels_to_go_up=1)
Ejemplo n.º 3
0
    def wait_for(self,
                 connection_observer,
                 connection_observer_future,
                 timeout=10.0):
        """
        Await for connection_observer running in background or timeout.

        :param connection_observer: The one we are awaiting for.
        :param connection_observer_future: Future of connection-observer returned from submit().
        :param timeout: Max time (in float seconds) you want to await before you give up.
        :return:
        """
        self.logger.debug("awaiting {}".format(connection_observer))
        start_time = time.time()
        done, not_done = wait([connection_observer_future], timeout=timeout)
        if connection_observer_future in done:
            self.shutdown()
            result = connection_observer_future.result()
            self.logger.debug("{} returned {}".format(connection_observer,
                                                      result))
            return result
        passed = time.time() - start_time
        connection_observer.cancel()
        connection_observer_future.cancel()
        self.shutdown()
        self.logger.debug("timeouted {}".format(connection_observer))
        raise ConnectionObserverTimeout(connection_observer,
                                        timeout,
                                        kind="await_done",
                                        passed_time=passed)
Ejemplo n.º 4
0
    def wait_for(self,
                 connection_observer,
                 connection_observer_future,
                 timeout=None):
        """
        Await for connection_observer running in background or timeout.

        :param connection_observer: The one we are awaiting for.
        :param connection_observer_future: Future of connection-observer returned from submit().
        :param timeout: Max time (in float seconds) you want to await before you give up. If None then taken from connection_observer
        :return:
        """
        self.logger.debug("go foreground: {!r} - await max. {} [sec]".format(
            connection_observer, timeout))
        start_time = time.time()
        remain_time = connection_observer.timeout
        check_timeout_from_observer = True
        wait_tick = 0.1
        if timeout:
            remain_time = timeout
            check_timeout_from_observer = False
            wait_tick = remain_time
        while remain_time > 0.0:
            done, not_done = wait([connection_observer_future],
                                  timeout=wait_tick)
            if connection_observer_future in done:
                self.shutdown()
                result = connection_observer_future.result()
                self.logger.debug("{} returned {}".format(
                    connection_observer, result))
                return result
            if check_timeout_from_observer:
                timeout = connection_observer.timeout
            remain_time = timeout - (time.time() - start_time)
        moler_conn = connection_observer.connection
        moler_conn.unsubscribe(connection_observer.data_received)
        passed = time.time() - start_time
        self.logger.debug("timeouted {}".format(connection_observer))
        connection_observer.cancel()
        connection_observer_future.cancel()
        self.shutdown()
        connection_observer.on_timeout()
        if hasattr(connection_observer, "command_string"):
            raise CommandTimeout(connection_observer,
                                 timeout,
                                 kind="await_done",
                                 passed_time=passed)
        else:
            raise ConnectionObserverTimeout(connection_observer,
                                            timeout,
                                            kind="await_done",
                                            passed_time=passed)
Ejemplo n.º 5
0
 def _validate_start(self, *args, **kwargs):
     # check base class invariants first
     if not self.connection:
         # only if we have connection we can expect some data on it
         # at the latest "just before start" we need connection
         raise NoConnectionProvided(self)
     # ----------------------------------------------------------------------
     # We intentionally do not check if connection is open here.
     # In such case net result anyway will be failed/timeouted observer -
     # - so, user will need to investigate "why".
     # Checking connection state would benefit in early detection of:
     # "error condition - no chance to succeed since connection is closed".
     # However, drawback is a requirement on connection to have is_open() API
     # We choose minimalistic dependency over better troubleshooting support.
     # ----------------------------------------------------------------------
     if self.timeout <= 0.0:
         raise ConnectionObserverTimeout(self, self.timeout, "before run", "timeout is not positive value")
Ejemplo n.º 6
0
def time_out_observer(connection_observer,
                      timeout,
                      passed_time,
                      runner_logger,
                      kind="background_run"):
    """Set connection_observer status to timed-out"""
    if not connection_observer.life_status.was_on_timeout_called:
        connection_observer.life_status.was_on_timeout_called = True
        if not connection_observer.done():
            if hasattr(connection_observer, "command_string"):
                exception = CommandTimeout(
                    connection_observer=connection_observer,
                    timeout=timeout,
                    kind=kind,
                    passed_time=passed_time)
            else:
                exception = ConnectionObserverTimeout(
                    connection_observer=connection_observer,
                    timeout=timeout,
                    kind=kind,
                    passed_time=passed_time)
            # TODO: secure_data_received() may change status of connection_observer
            # TODO: and if secure_data_received() runs inside threaded connection - we have race
            connection_observer.set_exception(exception)

            connection_observer.on_timeout()

            observer_info = "{}.{}".format(
                connection_observer.__class__.__module__, connection_observer)
            timeout_msg = "has timed out after {:.2f} seconds.".format(
                passed_time)
            msg = "{} {}".format(observer_info, timeout_msg)

            # levels_to_go_up: extract caller info to log where .time_out_observer has been called from
            connection_observer._log(logging.INFO, msg, levels_to_go_up=2)
            log_into_logger(runner_logger,
                            level=logging.DEBUG,
                            msg="{} {}".format(connection_observer,
                                               timeout_msg),
                            levels_to_go_up=1)