django-tasks icon indicating copy to clipboard operation
django-tasks copied to clipboard

Add initial support for a Celery backend

Open matiasb opened this issue 7 months ago • 5 comments

This is an initial PoC for a Celery backend implementation (still in progress, probably requiring some extra work, besides tests).

How to use it:

  • Using this django_tasks branch in your Django project environment, make sure to also install celery: $ pip install celery

  • Update your settings.py to set the Celery backend:

TASKS = {
    "default": {
        "BACKEND": "django_tasks.backends.celery.CeleryBackend"
    }
}
  • You can also set extra celery config in settings.py (otherwise it will just use the default values, which should be ok), by defining a CELERY_* prefixed setting (e.g. to define broker_url, you should add a setting for CELERY_BROKER_URL)

  • If you don't set a broker URL, the expected one would be a local RabbitMQ. You can run it using docker like this: $ docker run -d -p 5672:5672 rabbitmq

  • You shouldn't need to change any django_tasks related code in your project.

  • Finally, to run the Celery worker: $ DJANGO_SETTINGS_MODULE=<your_project.settings> celery -A django_tasks.backends.celery.app worker -l INFO (this uses a simple default Celery app (see app.py below) pulling config from Django settings; it can be customized per project if needed)

Your tasks should now be queued into RabbitMQ and picked/run by the Celery worker. (FWIW, I have been using this in a simple personal project, things seem to work for me so far).

A few items to discuss:

  • Should it be possible to run the worker via a management command? (and then, setting the env var for settings shouldn't be necessary)
  • Should it be possible to config a subset of the Celery config through django-tasks? (to eventually handle those general enough for all backends in the same way)
  • Right now this tries to keep the simplicity from the current implementation, just wrapping the minimal bits to queue tasks through Celery (and use the Celery worker to handle them on the other side), but eventually it could make sense to get deeper into Celery internals to allow for more flexible and/or complex scenarios (if needed).
  • Any other feedback, suggestions or expectations.

matiasb avatar Jun 28 '24 20:06 matiasb