def on_customer_accepted(evt): customer_idurl = id_url.field(evt.data.get('idurl')) if not customer_idurl: lg.warn('unknown customer idurl in event data payload') return False customer_glob_id = global_id.idurl2glob(customer_idurl) queue_id = global_id.MakeGlobalQueueID( queue_alias='supplier-file-modified', owner_id=customer_glob_id, supplier_id=my_id.getGlobalID(), ) if not p2p_queue.is_queue_exist(queue_id): customer_key_id = global_id.MakeGlobalID(customer=customer_glob_id, key_alias='customer') if my_keys.is_key_registered(customer_key_id): try: p2p_queue.open_queue(queue_id) except Exception as exc: lg.warn('failed to open queue %s : %s' % (queue_id, str(exc))) else: lg.warn('customer key %r for supplier queue not registered' % customer_key_id) if p2p_queue.is_queue_exist(queue_id): if not p2p_queue.is_producer_exist(my_id.getGlobalID()): try: p2p_queue.add_producer(my_id.getGlobalID()) except Exception as exc: lg.warn('failed to add producer: %s' % str(exc)) if p2p_queue.is_producer_exist(my_id.getGlobalID()): if not p2p_queue.is_producer_connected(my_id.getGlobalID(), queue_id): try: p2p_queue.connect_producer(my_id.getGlobalID(), queue_id) except Exception as exc: lg.warn('failed to connect producer: %s' % str(exc)) if p2p_queue.is_producer_connected(my_id.getGlobalID(), queue_id): if not p2p_queue.is_event_publishing(my_id.getGlobalID(), 'supplier-file-modified'): try: p2p_queue.start_event_publisher( my_id.getGlobalID(), 'supplier-file-modified') except Exception as exc: lg.warn('failed to start event publisher: %s' % str(exc)) return True
def on_customer_terminated(evt): customer_idurl = evt.data.get('idurl') if not customer_idurl: lg.warn('unknown customer idurl in event data payload') return False customer_glob_id = global_id.idurl2glob(customer_idurl) queue_id = global_id.MakeGlobalQueueID( queue_alias='supplier-file-modified', owner_id=customer_glob_id, supplier_id=my_id.getGlobalID(), ) # TODO: need to decide when to stop producing # might be that other customers needs that info still if p2p_queue.is_event_publishing(my_id.getGlobalID(), 'supplier-file-modified'): try: p2p_queue.stop_event_publisher(my_id.getGlobalID(), 'supplier-file-modified') except Exception as exc: lg.warn('failed to stop event publisher: %s' % str(exc)) if p2p_queue.is_producer_connected(my_id.getGlobalID(), queue_id): try: p2p_queue.disconnect_producer(my_id.getGlobalID(), queue_id) except Exception as exc: lg.warn('failed to disconnect producer: %s' % str(exc)) if p2p_queue.is_producer_exist(my_id.getGlobalID()): try: p2p_queue.remove_producer(my_id.getGlobalID()) except Exception as exc: lg.warn('failed to remove producer: %s' % str(exc)) if p2p_queue.is_queue_exist(queue_id): try: p2p_queue.close_queue(queue_id) except Exception as exc: lg.warn('failed to stop queue %s : %s' % (queue_id, str(exc))) return True