diff --git a/.gitignore b/.gitignore index d2d6f36..deead2d 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,6 @@ nosetests.xml .mr.developer.cfg .project .pydevproject + +# PyCharm +.idea/ diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..8dd46f0 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,19 @@ +language: python +python: + - "2.7" + - "3.4" + - "3.5" + - "3.6" + - "3.7-dev" +env: + - PYMONGO_VERSION=3.7 + - PYMONGO_VERSION=3.0 + - PYMONGO_VERSION=2.4 +services: + - mongodb +# command to install dependencies +install: + - pip install -q pymongo==$PYMONGO_VERSION +# command to run tests +script: + - python -m unittest discover \ No newline at end of file diff --git a/README.rst b/README.rst index af9bd4f..6a988b4 100644 --- a/README.rst +++ b/README.rst @@ -1,6 +1,9 @@ mongoqueue ---------- +.. image:: https://travis-ci.com/rupello/mongoqueue.svg?branch=master + :target: https://travis-ci.com/rupello/mongoqueue + Properties ========== @@ -23,10 +26,10 @@ A queue can be instantiated with a mongo collection and a consumer identifier. The consumer identifier helps distinguish multiple queue consumers that are taking jobs from the queue:: - >> from pymongo import Connection + >> from pymongo import MongoClient >> from mongoqueue import MongoQueue >> queue = MongoQueue( - ... Connection(TEST_DB).doctest_queue, + ... MongoClient(TEST_DB).doctest_queue, ... consumer_id="consumer-1", ... timeout=300, ... max_attempts=3) diff --git a/mongoqueue/lock.py b/mongoqueue/lock.py index 9d7732d..a8070c0 100644 --- a/mongoqueue/lock.py +++ b/mongoqueue/lock.py @@ -22,7 +22,7 @@ class MongoLock(object): def __init__(self, collection, lock_name, lease=120): self.collection = collection self.lock_name = lock_name - self._client_id = uuid.uuid4().get_hex() + self._client_id = uuid.uuid4().hex self._locked = False self._lease_time = lease self._lock_expires = False @@ -57,7 +57,7 @@ def _acquire(self): '_id': self.lock_name, 'ttl': ttl, 'client_id': self._client_id}, - w=1, j=1) + w=1, j=True) except errors.DuplicateKeyError: self.collection.remove( {"_id": self.lock_name, 'ttl': {'$lt': datetime.now()}}) @@ -65,7 +65,7 @@ def _acquire(self): self.collection.insert( {'_id': self.lock_name, 'ttl': ttl, - 'client_id': self._client_id}, w=1, j=1) + 'client_id': self._client_id}, w=1, j=True) except errors.DuplicateKeyError: self._locked = False return self._locked diff --git a/mongoqueue/test.py b/mongoqueue/test.py index 5128d9a..3ac7dbd 100644 --- a/mongoqueue/test.py +++ b/mongoqueue/test.py @@ -14,7 +14,7 @@ class MongoLockTest(TestCase): def setUp(self): - self.client = pymongo.Connection(os.environ.get("TEST_MONGODB")) + self.client = pymongo.MongoClient(os.environ.get("TEST_MONGODB")) self.db = self.client.test_queue self.collection = self.db.locks @@ -24,7 +24,7 @@ def tearDown(self): def test_lock_acquire_release_context_manager(self): with lock(self.collection, 'test1') as l: self.assertTrue(l.locked) - self.assertEqual(self.collection.find().count(), 0) + self.assertEqual(self.collection.count(),0) def test_auto_expires_old(self): lock = MongoLock(self.collection, 'test2', lease=2) @@ -54,7 +54,7 @@ def test_auto_expires_old(self): class MongoQueueTest(TestCase): def setUp(self): - self.client = pymongo.Connection(os.environ.get("TEST_MONGODB")) + self.client = pymongo.MongoClient(os.environ.get("TEST_MONGODB")) self.db = self.client.test_queue self.queue = MongoQueue(self.db.queue_1, "consumer_1") @@ -117,22 +117,6 @@ def test_error(self): def test_progress(self): pass - def test_stats(self): - - for i in range(5): - data = {"context_id": "alpha", - "data": [1, 2, 3], - "more-data": time.time()} - self.queue.put(data) - job = self.queue.next() - job.error("problem") - - stats = self.queue.stats() - self.assertEqual({'available': 5, - 'total': 5, - 'locked': 0, - 'errors': 0}, stats) - def test_context_manager_error(self): self.queue.put({"foobar": 1}) job = self.queue.next() @@ -145,7 +129,7 @@ def test_context_manager_error(self): pass job = self.queue.next() - self.assertEqual(job.data['attempts'], 1) + self.assertEqual(job.attempts, 1) def test_context_manager_complete(self): self.queue.put({"foobar": 1}) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..ffa4243 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +pymongo>=2.4