Ejemplo n.º 1
0
def ping_observing_task(ext_io_connection, ping_ip):
    """
    Here external-IO connection is abstract - we don't know its type.
    What we know is just that it has .moler_connection attribute.
    """
    logger = logging.getLogger('moler.user.app-code')
    conn_addr = str(ext_io_connection)

    # Layer 2 of Moler's usage (ext_io_connection + runner):
    # 3. create observers on Moler's connection
    net_down_detector = NetworkDownDetector(
        ping_ip,
        connection=ext_io_connection.moler_connection,
        runner=get_runner(variant="asyncio-in-thread"))
    net_up_detector = NetworkUpDetector(
        ping_ip,
        connection=ext_io_connection.moler_connection,
        runner=get_runner(variant="asyncio-in-thread"))

    info = '{} on {} using {}'.format(ping_ip, conn_addr, net_down_detector)
    logger.debug('observe ' + info)

    # 4. start observer (nonblocking, using as future)
    net_down_detector.start()  # should be started before we open connection
    # to not loose first data on connection

    with ext_io_connection:
        # 5. await that observer to complete
        try:
            net_down_time = net_down_detector.await_done(
                timeout=10)  # =2 --> TimeoutError
            timestamp = time.strftime("%H:%M:%S",
                                      time.localtime(net_down_time))
            logger.debug('Network {} is down from {}'.format(
                ping_ip, timestamp))
        except ConnectionObserverTimeout:
            logger.debug('Network down detector timed out')

        # 6. call next observer (blocking till completes)
        info = '{} on {} using {}'.format(ping_ip, conn_addr, net_up_detector)
        logger.debug('observe ' + info)
        # using as synchronous function (so we want verb to express action)
        detect_network_up = net_up_detector
        net_up_time = detect_network_up(
        )  # if you want timeout - see code above
        timestamp = time.strftime("%H:%M:%S", time.localtime(net_up_time))
        logger.debug('Network {} is back "up" from {}'.format(
            ping_ip, timestamp))
    logger.debug('exiting ping_observing_task({})'.format(ping_ip))
Ejemplo n.º 2
0
    def __init__(self, connection=None, runner=None):
        """
        Create instance of ConnectionObserver class
        :param connection: connection used to receive data awaited for
        """
        self.connection = connection
        self._is_running = False
        self.__is_done = False
        self._is_cancelled = False
        self._result = None
        self._exception = None
        self.runner = runner if runner else get_runner()
        self._future = None
        self.start_time = 0.0  # means epoch: 1970-01-01 00:00:00
        self.__timeout = 7.0  # default
        self.terminating_timeout = 0.0  # value for terminating connection_observer when it timeouts. Set positive value
        #                                 for command if they can do anything if timeout. Set 0 for observer or command
        #                                 if it cannot do anything if timeout.
        self.device_logger = logging.getLogger('moler.{}'.format(
            self.get_logger_name()))
        self.logger = logging.getLogger('moler.connection.{}'.format(
            self.get_logger_name()))

        self.in_terminating = False  # Set True if ConnectionObserver object is just after __timeout but it can do
        #                              something during terminating_timeout. False if the ConnectionObserver object runs
        #                              during normal timeout. For Runners only!
        self.was_on_timeout_called = False  # Set True if method on_timeout was called. False otherwise. For Runners
Ejemplo n.º 3
0
    def _get_runner(self, runner):
        """

        :param runner: Runner
        :return: Runner instance
        """
        return_runner = runner
        if return_runner is None and self.connection is not None:
            return_runner = self.connection.get_runner()
        if return_runner is None:
            return_runner = get_runner()
        return return_runner
Ejemplo n.º 4
0
    def __init__(self, connection=None, runner=None):
        """
        Create instance of ConnectionObserver class
        :param connection: connection used to receive data awaited for
        """
        super(ConnectionObserver, self).__init__()
        self.life_status = ConnectionObserverLifeStatus()
        self.connection = connection

        self._result = None
        self._exception = None
        self.runner = runner if runner else get_runner()
        self._future = None

        self.device_logger = logging.getLogger('moler.{}'.format(
            self.get_logger_name()))
        self.logger = logging.getLogger('moler.connection.{}'.format(
            self.get_logger_name()))