CeleryRouter

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 queue messages for background processing. It’s designed for projects that require high messages 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:

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

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! Now all incoming and outgoing messages will be queued using Celery.

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',
    },
}

Currently, there are only two child loggers: one for the router and one for the Celery task. You can capture their messages independently like so:

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

BlockingRouter

CeleryRouter uses BlockingRouter to route messages. If you want to capture all router messages, make sure to add, in addition to the CeleryRouter loggers, blockingrouter:

LOGGING_CONFIG = {
    'blockingrouter': {
        'handlers': ['file'],
        'level': 'DEBUG',
    }
}

Previous topic

Frequently Asked Questions

This Page