DatabaseRouter provides the following functionality:
Similar to CeleryRouter, DatabaseRouter is designed for projects that require high messages volumes.
Note
DatabaseRouter depends on django-celery 3.0+. Please follow the django-celery setup instructions before proceeding.
Add rapidsms.router.db to INSTALLED_APPS, then import djcelery and invoke setup_loader():
INSTALLED_APPS = (
# Other apps here
"rapidsms.router.db",
)
import djcelery
djcelery.setup_loader()
This will register Celery tasks in rapidsms.router.db.tasks.
Set RAPIDSMS_ROUTER to use DatabaseRouter:
RAPIDSMS_ROUTER = "rapidsms.router.db.DatabaseRouter"
Run syncdb to create the necessary database tables:
python manage.py syncdb
That’s it!
The database router has one optional setting, DB_ROUTER_DEFAULT_BATCH_SIZE, to change the default maximum size of a batch of messages from 200.
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 --loglevel=info
Now your messages will be handled asynchronously with DatabaseRouter.
DatabaseRouter utilizes two database models, Message and Transmission.
The Message model contains the context of a text message. For every associated Connection, a Message has an associated Transmission.
Message(id, status, date, updated, sent, delivered, direction, text, external_id, in_response_to_id)
Required. Date/time when message was created.
Date/time when all associated transmissions were delivered (requires backend functionality).
Required. Either 'I' or 'O'.
Optional. ID of message as defined by the associated backend.
Optional. Foreign key to Message that generated this reply.
Date/time when all associated transmissions were sent.
Required. See Message status values.
Required. Message text.
Required. Last date/time the message was updated.
A Transmission represents the instance of a particular Message and Connection.
Transmission(id, message_id, connection_id, status, date, updated, sent, delivered)
Required. Foreign key to associated Connection.
Required. Date/time when transmission was created.
Date/time when transmission was delivered (requires backend functionality).
Required. Foreign key to associated Message.
Date/time when transmission was sent.
Required. See Message status values.
Required. Last date/time when transmission was updated.
Message and Transmission objects can have the following status values: