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

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()}})

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')

View File

@@ -285,11 +285,12 @@ To use MongoDB as a message broker you simply provide the connection information
}
The ``mongo`` dictionary can contain any of the parameters exposed by pymongo's `MongoClient <https://api.mongodb.org/python/current/api/pymongo/mongo_client.html#pymongo.mongo_client.MongoClient>`__
If you want to use a mongodb uri, you can supply it as the ``host`` parameter.
mongo_db
~~~~~~~~
When using the MongoDB broker you can optionally provide a database name to use for the queues.
Defaults to ``django-q``
Defaults to default database if available, otherwise ``django-q``
.. _bulk: