def read(self, routing_key=None, messages_read=10, visibility_timeout=-1.0, block=False, polling_interval=1.0, polling_timeout=None): messages = [] current_time = time.time() query = { '_meta.expired': False, '_meta.visible_after': { '$lt': current_time } } if routing_key: query['_meta.routing_key'] = routing_key else: query['_meta.routing_key'] = { '$exists': False } update = {} if visibility_timeout > -1.0: update = { "$set": { "_meta.visible_after": current_time + visibility_timeout } } raw_messages=[] if block: self._block_until_message_available(query, polling_interval, polling_timeout) if update: for i in range(0, messages_read): raw_message = self.queue_collection.find_and_modify(query=query, update=update) if raw_message: raw_messages.append(raw_message) else: for raw_message in self.queue_collection.find(query).limit(messages_read): raw_messages.append(raw_message) for raw_message in raw_messages: message = Message(dictionary=raw_message, queue_collection=self.queue_collection) if not message.is_expired(): messages.append(message) return messages
def push(self, method='PING', arguments={}, topic=None, expire=-1.0): message = Message() message.method = method message.arguments = arguments self.queue.write(message, routing_key=topic, expire=expire)