CeleryRouter

New in version 0.10.0.

CeleryRouter uses Celery to queue incoming and outgoing messages.

BlockingRouter processes messages synchronously in the main HTTP thread. This is fine for most scenarios, but in some cases you may wish to process messages outside of the HTTP request/response cycle to be more efficient. CeleryRouter is a custom router that allows you to queue messages for background processing. It’s designed for projects that require high message volumes and greater concurrency.

Installation

Note

CeleryRouter depends on django-celery 3.0+. Please follow the setup instructions in Scheduling Tasks with Celery before proceeding.

Add rapidsms.router.celery to INSTALLED_APPS, then import djcelery and invoke setup_loader():

 INSTALLED_APPS = (
     # Other apps here
     "rapidsms.router.celery"
 )
 import djcelery
 djcelery.setup_loader()

This will register Celery tasks in rapidsms.router.celery.tasks.

Set RAPIDSMS_ROUTER to use CeleryRouter:

RAPIDSMS_ROUTER = "rapidsms.router.celery.CeleryRouter"

That’s it!

Celery workers

Finally, you’ll need to run the celery worker command (in a separate shell from runserver) to begin consuming queued tasks:

python manage.py celery worker -lDEBUG

Now your messages will be handled asynchronously with CeleryRouter.

Configuration

Eager backends

Sometimes your project may require the use of a synchronous backend. If this is the case, you can configure specific backends to utilize Celery’s eager functionality with the router.celery.eager backend setting. For example, here’s how you can force the httptester backend to be eager:

 INSTALLED_BACKENDS = {
     "message_tester": {
         "ENGINE": "rapidsms.contrib.httptester.backend",
         "router.celery.eager": True,
     },
 }

Using this setting means that the task will be executed in the current process, and not by an asynchronous worker. Please see the Celery documentation for more information on calling tasks.

Logging

Note

Please see the Django logging documentation for further information regarding general logging configuration.

All logging specific to CeleryRouter is handled through the rapidsms.router.celery name. For example, if you have a file handler defined, you can capture all messages using the following configuration:

LOGGING_CONFIG = {
    'rapidsms.router.celery': {
        'handlers': ['file'],
        'level': 'DEBUG',
    },
}