Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dfdsaf #234

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions django_mongodb_engine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
# setup.py imports this file in order to read version/author/... metadata
# but does not necessarily have a Django context.
import logging
logging.error('Error while trying to get django'
' settings module.\nError was: {0}'.format(str(exc)))
#logging.error('Error while trying to get django'
# ' settings module.\nError was: {0}'.format(str(exc)))
else:
try:
# It might be irritating that django-mongodb-engine registers itself as
Expand Down Expand Up @@ -46,5 +46,5 @@
# setup.py imports this file in order to read version/author/... metadata
# but does not necessarily have a Django context.
import logging
logging.error('Error while trying to get django'
' settings module.\nError was: {0}'.format(str(exc)))
#logging.error('Error while trying to get django'
# ' settings module.\nError was: {0}'.format(str(exc)))
71 changes: 57 additions & 14 deletions django_mongodb_engine/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import datetime
import decimal
import sys
import traceback
import time

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
Expand Down Expand Up @@ -184,11 +186,17 @@ def __init__(self, *args, **kwargs):
self.introspection = DatabaseIntrospection(self)
self.validation = DatabaseValidation(self)
self.connected = False
''' this dictates if we want to close connection at the end of each request or cache it (old behavior) '''
self.close_connection = kwargs.pop('close_connection', True)
''' number of times to retry the connection on failure '''
self.conn_retries = kwargs.pop('conn_retries', 1)
''' time to sleep between retries '''
self.conn_sleep_interval = kwargs.pop('conn_sleep_interval', 5)
del self.connection

def get_collection(self, name, **kwargs):
if (kwargs.pop('existing', False) and
name not in self.connection.database.collection_names()):
name not in self.database.collection_names()):
return None
collection = self.collection_class(self.database, name, **kwargs)
if settings.DEBUG:
Expand Down Expand Up @@ -226,19 +234,49 @@ def pop(name, default=None):
for key in options.iterkeys():
options[key.lower()] = options.pop(key)

try:
self.connection = Connection(host=host, port=port, **options)
self.database = self.connection[db_name]
except TypeError:
exc_info = sys.exc_info()
raise ImproperlyConfigured, exc_info[1], exc_info[2]

if user and password:
if not self.database.authenticate(user, password):
raise ImproperlyConfigured("Invalid username or password.")

self.connected = True
connection_created.send(sender=self.__class__, connection=self)
attempts = 0
while True:
try:
try:
self.connection = Connection(host=host, port=port, **options)
self.database = self.connection[db_name]
except TypeError:
exc_info = sys.exc_info()
raise ImproperlyConfigured, exc_info[1], exc_info[2]

if user and password:
if not self.database.authenticate(user, password):
raise ImproperlyConfigured("Invalid username or password.")

self.connected = True
connection_created.send(sender=self.__class__, connection=self)
''' execute a quick sample query so that the failure will happen
on the command that is run after the switchover, auth succeeds
on secondary but commands cannot be run. This command will
throw an exception and hence we will attempt to reconnect again '''
self.database['system.indexes'].find_one()
break
except Exception as e:
print 'MongoConnectionFailure to database %s %s' % (db_name,str(e))
print traceback.format_exc()

''' Make sure we set connected to False just in case we failed on the send '''
self.connected = False

''' initialize these instance variables, so that we can delete them '''
''' this will ensure that the __get_attr__ class method properly reconnects '''
self.database = None
self.connection = None
del self.database
del self.connection

attempts += 1
if attempts < self.conn_retries:
time.sleep(self.conn_sleep_interval)
print 'MongoConnectionRetry attempt=%d' % attempts
continue
raise e


def _reconnect(self):
if self.connected:
Expand All @@ -254,4 +292,9 @@ def _rollback(self):
pass

def close(self):
if self.close_connection and self.connected:
self.connection.close()
del self.database
del self.connection
self.connected = False
pass
14 changes: 13 additions & 1 deletion django_mongodb_engine/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

from pymongo import DESCENDING

from pymongo.errors import OperationFailure

from random import randint

from djangotoolbox.db.creation import NonrelDatabaseCreation

from .utils import make_index_list
Expand Down Expand Up @@ -39,7 +43,15 @@ def ensure_index(*args, **kwargs):
print "Installing indices for %s.%s model." % \
(meta.app_label, meta.object_name)
ensure_index.first_index = False
return collection.ensure_index(*args, **kwargs)
try:
return collection.ensure_index(*args, **kwargs)
except OperationFailure, e:
# Try with a short name for the index in case the
# auto-generated index name is too long
index_name = meta.object_name + "_index_" + str(randint(1,100000))
print "Error installing index on %s: %s, trying shorter index name: %s" % (meta.object_name, str(e), index_name)
return collection.ensure_index(*args, name=index_name, **kwargs)

ensure_index.first_index = True

newstyle_indexes = getattr(meta, 'indexes', None)
Expand Down