How to configure Celery¶

See also

Note

This article assumes that you are already familiar with the basics of usingCelery with Django. If not, please see Celery’s documentation.

Request Celery for your project¶

Celery needs to be set up by our infrastructure team. See Celery on DivioCloud for more information.

Your project will be set up with two new environment variables:

  • BROKER_URL
  • RABBITMQ_ERLANG_COOKIE
    Test and Live servers will have different values for both.

Configure Celery in Cloud projects¶

Aldryn Celery’s aldryn_config.py filecontains a number of settings that can be configured as environment variables.

Configure Celery for the local server¶

Set up Docker services¶

You will need to add some new services to your docker-compose.yml file.

RabbitMB¶

Set up the RabbitMQ messaging service:

  1. rabbitmq:
  2. image: rabbitmq:3.5-management
  3. hostname: rabbitmq
  4. ports:
  5. - "15672:15672"
  6. expose:
  7. - "15672"
  8. environment:
  9. RABBITMQ_ERLANG_COOKIE: <secret cookie value>

Celery worker¶

Set up the Celery worker service, with a command option to start theworker process:

  1. celeryworker:
  2. command: aldryn-celery worker

You will also need to add all of the other options already applied to theweb service in the same file, except the ports option.

To the links option, add:

  1. - "rabbitmq:rabbitmq"

In other words, the celery service should now include something like:

  1. celeryworker:
  2. command: aldryn-celery worker
  3. build: .
  4. links:
  5. - "db:postgres"
  6. - "rabbitmq:rabbitmq"
  7. volumes:
  8. - ".:/app:rw"
  9. - "./ data:/data:rw"
  10. env_file: .env-local

These are required because the celeryworker needs access to the same Pythoncomponents and environment as the web applications.

Celery beat¶

The Celery beat scheduling service needs to be set up in much the same way.

  1. celerybeat:
  2. command: aldryn-celery beat

Copy over the other options from celeryworker.

Celery cam¶

The cam service captures events for monitoring purposes.

Add:

  1. celerycam:
  2. command: aldryn-celery cam

followed once again by the other options from celeryworker.

The web service¶

Finally, to the links option in web, add:

  1. - "rabbitmq:rabbitmq"

Set up local environment variables¶

In .env-local add:

  1. RABBITMQ_ERLANG_COOKIE=<secret cookie value>
  2. BROKER_URL="amqp://guest:[[email protected]](http://docs.divio.com/cdn-cgi/l/email-protection):5672/"

Running the local project¶

docker-compose up or divio project up will now also start Celery.

Environment variable changes¶

If you change environment variables locally, the containers will need to bestopped and restarted in order to pick up the changes.

Configuration changes¶

When making project configuration changes, you will need to rebuild allthe containers:

  1. docker-compose build

Code changes¶

Unlike the web container, Celery’s containers will not be reloaded onPython code changes, so you will need to run docker-compose restart
celeryworker
manually when required (the other containers shouldn’t generallyneed to be restarted).

原文: http://docs.divio.com/en/latest/how-to/configure-celery.html