def get_topic_sub_keys_from_sub_keys(session, cluster_id, sub_key_list): topic_sub_keys = {} for item in pubsub_endpoint_queue_list_by_sub_keys(session, cluster_id, sub_key_list): sub_keys = topic_sub_keys.setdefault(item.topic_name, []) sub_keys.append(item.sub_key) return topic_sub_keys
def handle(self): sub_key = self.request.input.sub_key sub_key_list = self.request.input.sub_key_list if not (sub_key or sub_key_list): raise BadRequest( self.cid, 'Exactly one of sub_key or sub_key_list is required') if sub_key and sub_key_list: raise BadRequest( self.cid, 'Cannot provide both sub_key and sub_key_list on input') if sub_key: sub_key_list = [ sub_key ] # Otherwise, we already had sub_key_list on input so 'else' is not needed cluster_id = self.request.input.cluster_id topic_sub_keys = {} with closing(self.odb.session()) as session: # First we need a list of topics to which sub_keys were related - required by broker messages. for item in pubsub_endpoint_queue_list_by_sub_keys( session, cluster_id, sub_key_list): sub_keys = topic_sub_keys.setdefault(item.topic_name, []) sub_keys.append(item.sub_key) # Remove the subscription object which in turn cascades and removes all dependant objects session.query(PubSubSubscription).\ filter(PubSubSubscription.cluster_id==self.request.input.cluster_id).\ filter(PubSubSubscription.sub_key.in_(sub_key_list)).\ delete(synchronize_session=False) session.expire_all() session.commit() # Notify workers that this subscription needs to be deleted self.broker_client.publish({ 'topic_sub_keys': topic_sub_keys, 'action': PUBSUB.SUBSCRIPTION_DELETE.value, })