Пример #1
0
 def track_event(self, event, params={}, collection=None):
     """
     Dispatch a track event request into the queue.
 
     If the Publisher object hasn't been intialized yet, do so. If any error
     occurs during sending of the message, close the Publisher so it will be
     open automatically the next time somedy tracks an event. This will prevent
     a short-term network failure to disable one thread from commucating with
     the queue at the cost of retrying the connection every time.
     """
     global publisher
     if publisher is None:
         # no connection or there was an error last time
         # reinitiate the Publisher
         publisher = _get_carrot_object(Publisher)
 
     try:
         # put the message into the queue including current time
         publisher.send((event, time(), params, collection))
     except:
         # something went wrong, probably a connection error or something. Close
         # the carrot connection and set it to None so that the next request
         # will try and reopen it.
         _close_carrot_object(publisher)
         publisher = None
         raise
Пример #2
0
def collect_events():
    """
    Collect all events waiting in the queue and store them in the database.
    """    
    consumer = None
    collection = None
    backend_cls = _get_backend_cls()
    
    try:
        consumer = _get_carrot_object(Consumer, queue=settings.QUEUE)
        connection = Connection(host=settings.MONGODB_HOST, port=settings.MONGODB_PORT)

        for message in consumer.iterqueue():
            e, t, p, c = message.decode()
            backend_cls.save(event=e, time=t, params=p, collection=c)
            message.ack()

    finally:
        _close_carrot_object(consumer)
        if collection:
            try:
                collection.connection.close()
            except:
                pass