Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all tasks and periodic tasks in Celery [duplicate]

Possible Duplicate:
How can I find all subclasses of a given class in Python?

In my Django project, I have some subclass of Celery's Task and PeriodicTask:

class CustomTask(Task):
    # stuff

class CustomPeriodicTask(PeriodicTask):
    # stuff

I need all Task classes to add some custom logging configuration. So I thought I can us __subclasses__, but this does not work:

>>> Task.__subclasses__()
[<unbound PeriodicTask>, <class handle_register of <Celery default:0xa1cc3cc>>]

Is it somehow possible to get all my Task and PeriodicTask subclasses in a dynamic way?

like image 483
Martin Avatar asked Sep 29 '12 10:09

Martin


1 Answers

Celery maintains a registry of all tasks. This is how the worker can lookup a task by name when it receives a task message:

from celery import current_app
all_task_names = current_app.tasks.keys()
all_tasks = current_app.tasks.values()
foo_task = current_app.tasks['tasks.foo']

all_task_classes = [type(task) for task in current_app.tasks.itervalues()]

The task registry is only populated as the modules containing tasks are imported. If you have not imported all modules you can do like the celery worker does, and import all configured task module sources:

current_app.loader.import_default_modules()

NOTE: import_default_modules doesn't exist before Celery 2.5, back then you'd have to do:

from celery.loaders import current_loader
current_loader().init_worker()

NOTE2: Are you sure you want to edit all task classes like this? Celery comes with a number of ways to configure task classes, e.g.:

CELERY_ANNOTATIONS = {
     '*': {
         'loglevel': logging.DEBUG,
         'logger': logging.getLogger('foo'),
     },
}

will set the 'loglevel' and 'logger' attributes of all tasks. Alternatively you can use a common base class:

class MyTask(Task):
    abstract = True   # means this base task won't be registered
    loglevel = logging.DEBUG
    logger = logging.getLogger('foo'),
like image 165
asksol Avatar answered Oct 29 '22 10:10

asksol