check for default database on mongo connection

This commit is contained in:
Ilan Steemers
2015-09-26 13:01:15 +02:00
parent 8bdbcdaf55
commit b8bb186471
3 changed files with 15 additions and 5 deletions
+12 -3
View File
@@ -1,11 +1,12 @@
from datetime import timedelta
from time import sleep
from bson import ObjectId
from django.utils import timezone
from pymongo import MongoClient
from pymongo.errors import ConfigurationError
from django_q.brokers import Broker
from django_q.conf import Conf
@@ -17,12 +18,20 @@ def _timeout():
class Mongo(Broker):
def __init__(self, list_key=Conf.PREFIX):
super(Mongo, self).__init__(list_key)
self.collection = self.connection[Conf.MONGO_DB][list_key]
self.collection = self.get_collection()
@staticmethod
def get_connection(list_key=Conf.PREFIX):
return MongoClient(**Conf.MONGO)
def get_collection(self):
if not Conf.MONGO_DB:
try:
Conf.MONGO_DB = self.connection.get_default_database()[1]
except ConfigurationError:
Conf.MONGO_DB = 'django-q'
return self.connection[Conf.MONGO_DB][self.list_key]
def queue_size(self):
return self.collection.count({'lock': {'$lte': _timeout()}})
+1 -1
View File
@@ -52,7 +52,7 @@ class Conf(object):
# MongoDB broker
MONGO = conf.get('mongo', None)
MONGO_DB = conf.get('mongo_db', 'django-q')
MONGO_DB = conf.get('mongo_db', None)
# Name of the cluster or site. For when you run multiple sites on one redis server
PREFIX = conf.get('name', 'default')