def crawl_moksha_topics(self, entry_point='moksha.widget'): """ Iterate over all moksha widgets looking for topics """ topics = defaultdict(list) for entry in pkg_resources.iter_entry_points(entry_point): entry_class = entry.load() for topic in listify(getattr(entry_class, 'topic', [])): topics[to_unicode(topic)].append(entry_class) for topic in listify(getattr(entry_class, 'topics', [])): topics[to_unicode(topic)].append(entry_class) return topics
def amqp_subscribe(topic): """ Return a javascript callback that subscribes to a given topic, or a list of topics. """ sub = """ moksha.debug("Subscribing to the '%(topic)s' topic"); moksha_amqp_queue.subscribe({ exchange: 'amq.topic', remote_queue: moksha_amqp_remote_queue, binding_key: '%(topic)s', callback: moksha_amqp_on_message, }); """ return ''.join([sub % {'topic': t} for t in listify(topic)])
def __init__(self): self.hub = MokshaHub() self.log = log if self.hub.amqp_broker and not self.hub.stomp_broker: for topic in listify(self.topic): log.debug('Subscribing to consumer topic %s' % topic) if isinstance(self.hub, AMQPLibHub): # AMQPLibHub specific queue_name = str(uuid.uuid4()) self.hub.queue_declare(queue=queue_name, exclusive=True) self.hub.exchange_bind(queue_name, binding_key=topic) if self.jsonify: self.hub.queue_subscribe(queue_name, self._consume_json) else: self.hub.queue_subscribe(queue_name, self._consume) else: # Assume we're using Qpid then. server_queue_name = 'moksha_consumer_' + self.hub.session.name self.hub.queue_declare(queue=server_queue_name, exclusive=True) self.hub.exchange_bind(server_queue_name, binding_key=topic) local_queue_name = 'moksha_consumer_' + self.hub.session.name self.hub.local_queue = self.hub.session.incoming( local_queue_name) self.hub.message_subscribe(queue=server_queue_name, destination=local_queue_name) self.hub.local_queue.start() if self.jsonify: self.hub.local_queue.listen(self._consume_json) else: self.hub.local_queue.listen(self._consume) # If the consumer specifies an 'app', then setup `self.engine` to # be a SQLAlchemy engine, along with a configured DBSession app = getattr(self, 'app', None) self.engine = self.DBSession = None if app: log.debug("Setting up individual engine for consumer") self.engine = create_app_engine(app) self.DBSession = sessionmaker(bind=self.engine)()
def __init__(self): self.hub = MokshaHub() self.log = log if self.hub.amqp_broker and not self.hub.stomp_broker: for topic in listify(self.topic): log.debug('Subscribing to consumer topic %s' % topic) if isinstance(self.hub, AMQPLibHub): # AMQPLibHub specific queue_name = str(uuid.uuid4()) self.hub.queue_declare(queue=queue_name, exclusive=True) self.hub.exchange_bind(queue_name, binding_key=topic) if self.jsonify: self.hub.queue_subscribe(queue_name, self._consume_json) else: self.hub.queue_subscribe(queue_name, self._consume) else: # Assume we're using Qpid then. server_queue_name = 'moksha_consumer_' + self.hub.session.name self.hub.queue_declare(queue=server_queue_name, exclusive=True) self.hub.exchange_bind(server_queue_name, binding_key=topic) local_queue_name = 'moksha_consumer_' + self.hub.session.name self.hub.local_queue = self.hub.session.incoming(local_queue_name) self.hub.message_subscribe(queue=server_queue_name, destination=local_queue_name) self.hub.local_queue.start() if self.jsonify: self.hub.local_queue.listen(self._consume_json) else: self.hub.local_queue.listen(self._consume) # If the consumer specifies an 'app', then setup `self.engine` to # be a SQLAlchemy engine, along with a configured DBSession app = getattr(self, 'app', None) self.engine = self.DBSession = None if app: log.debug("Setting up individual engine for consumer") self.engine = create_app_engine(app) self.DBSession = sessionmaker(bind=self.engine)()