Browse Source

CELERY_TASK_RESULT_EXPIRES is now prepared by individual backends, not post_config_merge

Ask Solem 14 years ago
parent
commit
be8010c843

+ 0 - 3
celery/app/base.py

@@ -215,9 +215,6 @@ class BaseApp(object):
                     "exchange": c["CELERY_DEFAULT_EXCHANGE"],
                     "exchange_type": c["CELERY_DEFAULT_EXCHANGE_TYPE"],
                     "binding_key": c["CELERY_DEFAULT_ROUTING_KEY"]}}
-        if isinstance(c["CELERY_TASK_RESULT_EXPIRES"], int):
-            c["CELERY_TASK_RESULT_EXPIRES"] = timedelta(
-                    seconds=c["CELERY_TASK_RESULT_EXPIRES"])
 
         # Install backend cleanup periodic task.
         c["CELERYBEAT_SCHEDULE"] = maybe_promise(c["CELERYBEAT_SCHEDULE"])

+ 3 - 1
celery/backends/cassandra.py

@@ -16,6 +16,7 @@ from datetime import datetime
 from celery.backends.base import BaseDictBackend
 from celery.exceptions import ImproperlyConfigured
 from celery.utils.serialization import pickle
+from celery.utils.timeutils import maybe_timedelta
 from celery import states
 
 
@@ -52,7 +53,8 @@ class CassandraBackend(BaseDictBackend):
                             name="celery.backends.cassandra")
 
         self.result_expires = kwargs.get("result_expires") or \
-                                self.app.conf.CELERY_TASK_RESULT_EXPIRES
+                                maybe_timedelta(
+                                    self.app.conf.CELERY_TASK_RESULT_EXPIRES)
 
         if not pycassa:
             raise ImproperlyConfigured(

+ 3 - 1
celery/backends/database.py

@@ -5,6 +5,7 @@ from celery.backends.base import BaseDictBackend
 from celery.db.models import Task, TaskSet
 from celery.db.session import ResultSession
 from celery.exceptions import ImproperlyConfigured
+from celery.utils.timeutils import maybe_timedelta
 
 
 def _sqlalchemy_installed():
@@ -25,7 +26,8 @@ class DatabaseBackend(BaseDictBackend):
             engine_options=None, **kwargs):
         super(DatabaseBackend, self).__init__(**kwargs)
         self.result_expires = result_expires or \
-                                self.app.conf.CELERY_TASK_RESULT_EXPIRES
+                                maybe_timedelta(
+                                    self.app.conf.CELERY_TASK_RESULT_EXPIRES)
         self.dburi = dburi or self.app.conf.CELERY_RESULT_DBURI
         self.engine_options = dict(engine_options or {},
                         **self.app.conf.CELERY_RESULT_ENGINE_OPTIONS or {})

+ 3 - 1
celery/backends/mongodb.py

@@ -10,6 +10,7 @@ from celery import states
 from celery.backends.base import BaseDictBackend
 from celery.exceptions import ImproperlyConfigured
 from celery.utils.serialization import pickle
+from celery.utils.timeutils import maybe_timedelta
 
 
 class Bunch:
@@ -34,7 +35,8 @@ class MongoBackend(BaseDictBackend):
 
         """
         self.result_expires = kwargs.get("result_expires") or \
-                                self.app.conf.CELERY_TASK_RESULT_EXPIRES
+                                maybe_timedelta(
+                                    self.app.conf.CELERY_TASK_RESULT_EXPIRES)
 
         if not pymongo:
             raise ImproperlyConfigured(

+ 0 - 13
celery/tests/test_app.py

@@ -124,19 +124,6 @@ class test_App(unittest.TestCase):
         self.app.IS_WINDOWS = True
         self.assertFalse(self.app.log.supports_color())
 
-    def test_task_result_expires_converted_to_timedelta(self):
-        self.app.config_from_object(Object(CELERY_TASK_RESULT_EXPIRES=100))
-        self.assertEqual(self.app.conf.CELERY_TASK_RESULT_EXPIRES,
-                         timedelta(seconds=100))
-
-        self.assertIn("celery.backend_cleanup",
-                      self.app.conf.CELERYBEAT_SCHEDULE)
-
-    def test_backend_cleanup_not_installed(self):
-        self.app.config_from_object(Object(CELERY_TASK_RESULT_EXPIRES=None))
-        self.assertNotIn("celery.backend_cleanup",
-                         self.app.conf.CELERYBEAT_SCHEDULE)
-
     def test_compat_setting_CARROT_BACKEND(self):
         self.app.config_from_object(Object(CARROT_BACKEND="set_by_us"))
         self.assertEqual(self.app.conf.BROKER_BACKEND, "set_by_us")

+ 7 - 0
celery/utils/timeutils.py

@@ -20,6 +20,13 @@ TIME_UNITS = (("day", 60 * 60 * 24, lambda n: int(math.ceil(n))),
               ("second", 1, lambda n: "%.2f" % n))
 
 
+def maybe_timedelta(delta):
+    """Coerces integer to timedelta if `delta` is an integer."""
+    if isinstance(delta, int):
+        return timedelta(seconds=int)
+    return delta
+
+
 def timedelta_seconds(delta):  # pragma: no cover
     """Convert :class:`datetime.timedelta` to seconds.