浏览代码

Merge branch 'ianawilson/eager_db_close_postrun'

Ask Solem 11 年之前
父节点
当前提交
b045dbe864
共有 4 个文件被更改,包括 21 次插入12 次删除
  1. 1 0
      CONTRIBUTORS.txt
  2. 4 7
      celery/fixups/django.py
  3. 15 5
      celery/tests/fixups/test_django.py
  4. 1 0
      docs/AUTHORS.txt

+ 1 - 0
CONTRIBUTORS.txt

@@ -145,3 +145,4 @@ Alexander Smirnov, 2013/08/30
 Matt Robenolt, 2013/08/31
 Jameel Al-Aziz, 2013/10/04
 Fazleev Maksim, 2013/10/08
+Ian A Wilson, 2013/10/18

+ 4 - 7
celery/fixups/django.py

@@ -157,15 +157,12 @@ class DjangoFixup(object):
         if not getattr(sender.request, 'is_eager', False):
             self.close_database()
 
-    def on_task_postrun(self, **kwargs):
-        """Does everything necessary for Django to work in a long-living,
-        multiprocessing environment.
-
-        """
+    def on_task_postrun(self, sender, **kwargs):
         # See http://groups.google.com/group/django-users/
         #            browse_thread/thread/78200863d0c07c6d/
-        self.close_database()
-        self.close_cache()
+        if not getattr(sender.request, 'is_eager', False):
+            self.close_database()
+            self.close_cache()
 
     def close_database(self, **kwargs):
         if self._close_old_connections:

+ 15 - 5
celery/tests/fixups/test_django.py

@@ -140,12 +140,22 @@ class test_DjangoFixup(AppCase):
                 self.assertFalse(f.close_database.called)
 
     def test_on_task_postrun(self):
+        task = Mock()
         with self.fixup_context(self.app) as (f, _, _):
-            with patch.object(f, 'close_database'):
-                with patch.object(f, 'close_cache'):
-                    f.on_task_postrun()
-                    f.close_database.assert_called_with()
-                    f.close_cache.assert_called_with()
+            with patch.object(f, 'close_cache'):
+                task.request.is_eager = False
+                with patch.object(f, 'close_database'):
+                    f.on_task_postrun(task)
+                    self.assertTrue(f.close_database.called)
+                    self.assertTrue(f.close_cache.called)
+
+            # when a task is eager, do not close connections
+            with patch.object(f, 'close_cache'):
+                task.request.is_eager = True
+                with patch.object(f, 'close_database'):
+                    f.on_task_postrun(task)
+                    self.assertFalse(f.close_database.called)
+                    self.assertFalse(f.close_cache.called)
 
     def test_close_database(self):
         with self.fixup_context(self.app) as (f, _, _):

+ 1 - 0
docs/AUTHORS.txt

@@ -55,6 +55,7 @@ Gunnlaugur Thor Briem <gunnlaugur@gmail.com>
 Hari <haridara@gmail.com>
 Harm Verhagen <harm.verhagen@gmail.com>
 Honza Kral <honza.kral@gmail.com>
+Ian A Wilson <ian@ianawilson.com>
 Ignas Mikalajūnas <ignas.mikalajunas@gmail.com>
 Ionel Maries Cristian <contact@ionelmc.ro>
 Ionut Turturica <jonozzz@yahoo.com>