Browse Source

Adds app.register_task method to support Celery 1.0 style task-classes. Closes #3615

Ask Solem 8 years ago
parent
commit
a811066c7f
2 changed files with 17 additions and 2 deletions
  1. 15 0
      celery/app/base.py
  2. 2 2
      docs/whatsnew-4.0.rst

+ 15 - 0
celery/app/base.py

@@ -478,6 +478,21 @@ class Celery(object):
             task = self._tasks[name]
             task = self._tasks[name]
         return task
         return task
 
 
+    def register_task(self, task):
+        """Utility for registering a task-based class.
+
+        Note:
+            This is here for compatibility with old Celery 1.0
+            style task classes, you should not need to use this for
+            new projects.
+        """
+        if not task.name:
+            task_cls = type(task)
+            task.name = self.gen_task_name(
+                task_cls.__name__, task_cls.__module__)
+        self.tasks[task.name] = task
+        return task
+
     def gen_task_name(self, name, module):
     def gen_task_name(self, name, module):
         return gen_task_name(self, name, module)
         return gen_task_name(self, name, module)
 
 

+ 2 - 2
docs/whatsnew-4.0.rst

@@ -551,7 +551,7 @@ these manually:
     class CustomTask(Task):
     class CustomTask(Task):
         def run(self):
         def run(self):
             print('running')
             print('running')
-    app.tasks.register(CustomTask())
+    app.register_task(CustomTask())
 
 
 The best practice is to use custom task classes only for overriding
 The best practice is to use custom task classes only for overriding
 general behavior, and then using the task decorator to realize the task:
 general behavior, and then using the task decorator to realize the task:
@@ -760,7 +760,7 @@ some long-requested features:
 
 
             def run(self, fun, *args, **kwargs):
             def run(self, fun, *args, **kwargs):
                 return fun(*args, **kwargs)
                 return fun(*args, **kwargs)
-        call_as_task = app.tasks.register(call_as_task())
+        call_as_task = app.register_task(call_as_task())
 
 
 - New ``argsrepr`` and ``kwargsrepr`` fields contain textual representations
 - New ``argsrepr`` and ``kwargsrepr`` fields contain textual representations
   of the task arguments (possibly truncated) for use in logs, monitors, etc.
   of the task arguments (possibly truncated) for use in logs, monitors, etc.