From b8bb186471eff6837d11a86c57908dd1077dc64d Mon Sep 17 00:00:00 2001 From: Ilan Steemers Date: Sat, 26 Sep 2015 13:01:15 +0200 Subject: [PATCH] check for default database on mongo connection --- django_q/brokers/mongo.py | 15 ++++++++++++--- django_q/conf.py | 2 +- docs/configure.rst | 3 ++- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/django_q/brokers/mongo.py b/django_q/brokers/mongo.py index be510e0..73020fb 100644 --- a/django_q/brokers/mongo.py +++ b/django_q/brokers/mongo.py @@ -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()}}) diff --git a/django_q/conf.py b/django_q/conf.py index 1ce949c..9ca2146 100644 --- a/django_q/conf.py +++ b/django_q/conf.py @@ -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') diff --git a/docs/configure.rst b/docs/configure.rst index 50024dd..8a3d17e 100644 --- a/docs/configure.rst +++ b/docs/configure.rst @@ -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 `__ +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: