Browse Source

Use app.gen_task_name everywhere & updated docs

Dmitry Malinovsky 10 years ago
parent
commit
ca3d280e3f
3 changed files with 51 additions and 4 deletions
  1. 1 2
      celery/app/__init__.py
  2. 1 2
      celery/task/base.py
  3. 49 0
      docs/userguide/tasks.rst

+ 1 - 2
celery/app/__init__.py

@@ -17,7 +17,6 @@ from celery._state import (
     get_current_task as current_task,
     connect_on_app_finalize, set_default_app, _get_active_apps, _task_stack,
 )
-from celery.utils import gen_task_name
 
 from .base import Celery, AppPickler
 
@@ -140,7 +139,7 @@ def shared_task(*args, **kwargs):
             def task_by_cons():
                 app = current_app()
                 return app.tasks[
-                    name or gen_task_name(app, fun.__name__, fun.__module__)
+                    name or app.gen_task_name(fun.__name__, fun.__module__)
                 ]
             return Proxy(task_by_cons)
         return __inner

+ 1 - 2
celery/task/base.py

@@ -18,7 +18,6 @@ from celery.app.task import Context, Task as BaseTask, _reprtask
 from celery.five import class_property, reclassmethod, with_metaclass
 from celery.local import Proxy
 from celery.schedules import maybe_schedule
-from celery.utils import gen_task_name
 from celery.utils.log import get_task_logger
 
 __all__ = ['Context', 'Task', 'TaskType', 'PeriodicTask', 'task']
@@ -86,7 +85,7 @@ class TaskType(type):
         # - Automatically generate missing/empty name.
         task_name = attrs.get('name')
         if not task_name:
-            attrs['name'] = task_name = gen_task_name(app, name, task_module)
+            attrs['name'] = task_name = app.gen_task_name(name, task_module)
 
         if not attrs.get('_decorated'):
             # non decorated tasks must also be shared in case

+ 49 - 0
docs/userguide/tasks.rst

@@ -215,6 +215,55 @@ on the automatic naming:
     def add(x, y):
         return x + y
 
+.. _task-name-generator-info:
+
+Changing the automatic naming behavior
+--------------------------------------
+
+.. versionadded:: 3.2
+
+There are some cases when the default automatic naming is not suitable.
+Consider you have many tasks within many different modules::
+
+    project/
+           /__init__.py
+           /celery.py
+           /moduleA/
+                   /__init__.py
+                   /tasks.py
+           /moduleB/
+                   /__init__.py
+                   /tasks.py
+
+Using the default automatic naming, each task will have a generated name
+like `moduleA.tasks.taskA`, `moduleA.tasks.taskB`, `moduleB.tasks.test`
+and so on. You may want to get rid of having `tasks` in all task names.
+As pointed above, you can explicitly give names for all tasks, or you
+can change the automatic naming behavior by overriding
+:meth:`~@Celery.gen_task_name`. Continuing with the example, `celery.py`
+may contain:
+
+.. code-block:: python
+
+    from celery import Celery
+
+    class MyCelery(Celery):
+
+        def gen_task_name(self, name, module):
+            if module.endswith('.tasks'):
+                module = module[:-6]
+            return super(MyCelery, self).gen_task_name(name, module)
+
+    app = MyCelery('main')
+
+So each task will have a name like `moduleA.taskA`, `moduleA.taskB` and
+`moduleB.test`.
+
+.. warning::
+
+    Make sure that your `gen_task_name` is a pure function, which means
+    that for the same input it must always return the same output.
+
 .. _task-request-info:
 
 Context