Permalink
Browse files

Fixes loader so DB connection is not closed for eager tasks.

Closes #116.
  • Loading branch information...
1 parent 0bdaca8 commit 6dcfc6f92882b56f572cab02441eb183cc365c81 @mlavin mlavin committed with Mar 7, 2012
Showing with 41 additions and 3 deletions.
  1. +7 −2 djcelery/loaders.py
  2. +3 −1 tests/someapp/models.py
  3. +9 −0 tests/someapp/tasks.py
  4. +22 −0 tests/someapp/tests.py
View
@@ -87,9 +87,14 @@ def on_process_cleanup(self):
self.close_database()
self.close_cache()
- def on_task_init(self, *args, **kwargs):
+ def on_task_init(self, task_id, task):
"""Called before every task."""
- self.close_database()
+ try:
+ is_eager = task.request.is_eager
+ except AttributeError:
+ is_eager = False
+ if not is_eager:
+ self.close_database()
def on_worker_init(self):
"""Called when the worker starts.
View
@@ -1,3 +1,5 @@
from django.db import models # noqa
-# Create your models here.
+
+class Thing(models.Model):
+ name = models.CharField(max_length=10)
View
@@ -1,6 +1,15 @@
from celery.task import task
+from django.db.models import get_model
+
@task(name="c.unittest.SomeAppTask")
def SomeAppTask(**kwargs):
return 42
+
+
+@task(name="c.unittest.SomeModelTask")
+def SomeModelTask(pk):
+ model = get_model("someapp", "Thing")
+ thing = model.objects.get(pk=pk)
+ return thing.name
View
@@ -0,0 +1,22 @@
+from __future__ import absolute_import
+
+from django.test.testcases import TestCase as DjangoTestCase
+
+from someapp.models import Thing
+from someapp.tasks import SomeModelTask
+
+
+class SimpleTest(DjangoTestCase):
+
+ def setUp(self):
+ self.thing = Thing.objects.create(name=u"Foo")
+
+ def test_apply_task(self):
+ "Apply task function."
+ result = SomeModelTask.apply(kwargs={'pk': self.thing.pk})
+ self.assertEqual(result.get(), self.thing.name)
+
+ def test_task_function(self):
+ "Run task function."
+ result = SomeModelTask(pk=self.thing.pk)
+ self.assertEqual(result, self.thing.name)

0 comments on commit 6dcfc6f

Please sign in to comment.