Pārlūkot izejas kodu

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

Ask Solem 8 gadi atpakaļ
vecāks
revīzija
a811066c7f
2 mainītis faili ar 17 papildinājumiem un 2 dzēšanām
  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]
         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):
         return gen_task_name(self, name, module)
 

+ 2 - 2
docs/whatsnew-4.0.rst

@@ -551,7 +551,7 @@ these manually:
     class CustomTask(Task):
         def run(self):
             print('running')
-    app.tasks.register(CustomTask())
+    app.register_task(CustomTask())
 
 The best practice is to use custom task classes only for overriding
 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):
                 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
   of the task arguments (possibly truncated) for use in logs, monitors, etc.