Commit e0fb94cc by Carlos Andrés Rocha

Cleanup mongodb authentication

Authenticate to the database before getting a reference to the
collection.
parent a03a11e0
...@@ -11,7 +11,7 @@ from pymongo.errors import PyMongoError ...@@ -11,7 +11,7 @@ from pymongo.errors import PyMongoError
from track.backends import BaseBackend from track.backends import BaseBackend
log = logging.getLogger('track.backends.mongodb') log = logging.getLogger(__name__)
class MongoBackend(BaseBackend): class MongoBackend(BaseBackend):
...@@ -64,14 +64,17 @@ class MongoBackend(BaseBackend): ...@@ -64,14 +64,17 @@ class MongoBackend(BaseBackend):
**extra **extra
) )
self.collection = self.connection[db_name][collection_name] database = self.connection[db_name]
if user or password: if user or password:
self.collection.database.authenticate(user, password) database.authenticate(user, password)
self.collection = database[collection_name]
self._create_indexes() self._create_indexes()
def _create_indexes(self): def _create_indexes(self):
"""Ensures the proper fields are indexed"""
# WARNING: The collection will be locked during the index # WARNING: The collection will be locked during the index
# creation. If the collection has a large number of # creation. If the collection has a large number of
# documents in it, the operation can take a long time. # documents in it, the operation can take a long time.
...@@ -83,8 +86,12 @@ class MongoBackend(BaseBackend): ...@@ -83,8 +86,12 @@ class MongoBackend(BaseBackend):
self.collection.ensure_index('event_type') self.collection.ensure_index('event_type')
def send(self, event): def send(self, event):
"""Insert the event in to the Mongo collection"""
try: try:
self.collection.insert(event, manipulate=False) self.collection.insert(event, manipulate=False)
except PyMongoError: except PyMongoError:
# The event will be lost in case of a connection error.
# pymongo will re-connect/re-authenticate automatically
# during the next event.
msg = 'Error inserting to MongoDB event tracker backend' msg = 'Error inserting to MongoDB event tracker backend'
log.exception(msg) log.exception(msg)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment