Browse Source

Fixed problem where Celery closed db connections as a part of the task_postrun signal, even when a task.request.is_eager

Ian Wilson 11 years ago
parent
commit
321e75ce76
4 changed files with 21 additions and 7 deletions
  1. 1 0
      CONTRIBUTORS.txt
  2. 5 2
      celery/fixups/django.py
  3. 14 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

+ 5 - 2
celery/fixups/django.py

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

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

@@ -140,12 +140,21 @@ 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 the db connection
+                task.request.is_eager = True
+                with patch.object(f, 'close_database'):
+                    f.on_task_postrun(task)
+                    self.assertFalse(f.close_database.called)
+                    self.assertTrue(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>