def ping_observing_task(address, ping_ip): logger = logging.getLogger('moler.user.app-code') net_addr = 'tcp://{}:{}'.format(*address) # Lowest layer of Moler's usage (you manually glue all elements): # 1. create observers net_down_detector = NetworkDownDetector(ping_ip) net_drop_found = False net_up_detector = NetworkUpDetector(ping_ip) moler_conn = ObservableConnection(decoder=lambda data: data.decode("utf-8")) # 2. virtually "start" observer by making it data-listener moler_conn.subscribe(net_down_detector.data_received) info = '{} on {} using {}'.format(ping_ip, net_addr, net_down_detector) logger.debug('observe ' + info) for _ in tcp_connection(address, moler_conn): # anytime new data comes it may change status of observer if not net_drop_found and net_down_detector.done(): net_drop_found = True net_down_time = net_down_detector.result() timestamp = time.strftime("%H:%M:%S", time.localtime(net_down_time)) logger.debug('Network {} is down from {}'.format(ping_ip, timestamp)) # 3. virtually "stop" that observer moler_conn.unsubscribe(net_down_detector.data_received) # 4. and start subsequent one (to know when net is back "up") info = '{} on {} using {}'.format(ping_ip, net_addr, net_up_detector) logger.debug('observe ' + info) moler_conn.subscribe(net_up_detector.data_received) if net_up_detector.done(): net_up_time = net_up_detector.result() timestamp = time.strftime("%H:%M:%S", time.localtime(net_up_time)) logger.debug('Network {} is back "up" from {}'.format(ping_ip, timestamp)) # 5. virtually "stop" that observer moler_conn.unsubscribe(net_up_detector.data_received) break
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): # 1. create observers net_down_detector = NetworkDownDetector(ping_ip) net_drop_found = False net_up_detector = NetworkUpDetector(ping_ip) moler_conn = ext_io_connection.moler_connection # 2. virtually "start" observer by making it data-listener moler_conn.subscribe(net_down_detector.data_received) info = '{} on {} using {}'.format(ping_ip, conn_addr, net_down_detector) logger.debug('observe ' + info) with ext_io_connection.open(): observing_timeout = 10 start_time = time.time() while time.time() < start_time + observing_timeout: # anytime new data comes it may change status of observer if not net_drop_found and net_down_detector.done(): net_drop_found = True net_down_time = net_down_detector.result() timestamp = time.strftime("%H:%M:%S", time.localtime(net_down_time)) logger.debug('Network {} is down from {}'.format( ping_ip, timestamp)) # 3. virtually "stop" that observer moler_conn.unsubscribe(net_down_detector.data_received) # 4. and start subsequent one (to know when net is back "up") info = '{} on {} using {}'.format(ping_ip, conn_addr, net_up_detector) logger.debug('observe ' + info) moler_conn.subscribe(net_up_detector.data_received) if net_up_detector.done(): net_up_time = net_up_detector.result() timestamp = time.strftime("%H:%M:%S", time.localtime(net_up_time)) logger.debug('Network {} is back "up" from {}'.format( ping_ip, timestamp)) # 5. virtually "stop" that observer moler_conn.unsubscribe(net_up_detector.data_received) break time.sleep(0.2)
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) net_down_detector.connection = ext_io_connection.moler_connection net_up_detector = NetworkUpDetector(ping_ip) net_up_detector.connection = ext_io_connection.moler_connection 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.open(): # 5. await that observer to complete net_down_time = net_down_detector.await_done(timeout=10) timestamp = time.strftime("%H:%M:%S", time.localtime(net_down_time)) logger.debug('Network {} is down from {}'.format(ping_ip, timestamp)) # 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() timestamp = time.strftime("%H:%M:%S", time.localtime(net_up_time)) logger.debug('Network {} is back "up" from {}'.format( ping_ip, timestamp))
async 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")) net_up_detector = NetworkUpDetector( ping_ip, connection=ext_io_connection.moler_connection, runner=get_runner(variant="asyncio")) 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 async with ext_io_connection: # 5. await that observer to complete try: net_down_time = await asyncio.wait_for(net_down_detector, 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 asyncio.TimeoutError: 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 = await 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))