Metroid - Metro for Django (An async Azure Service Bus receiver, triggering task in Celery/RQ)
Subscribe, act, publish.
This app is intended to streamline integration with Metro for all Django+Celery users by:
settings.py
MetroidTask
basepython
>= 3.10django
>= 4.2 - For asgiref
, settingsdjango-guid
>= 3.2.0 - Storing correlation IDs for failed tasks in the database, making debugging easycelery
>= 5.3.0 - Execute tasks based on a subjectdjango-rq
>= 2.4.1 - Execute tasks based on a subjectThe python manage.py metroid
app is fully asynchronous, and has no blocking code. It utilizes Celery
to execute tasks.
It works by:
Note For a complete example, have a look in
demoproj/settings.py
.
METROID
key in settings.py
with all your subscriptions and handlers.
Example settings:METROID = {
'subscriptions': [
{
'topic_name': 'metro-demo',
'subscription_name': 'sub-metrodemo-metrodemoerfett',
'connection_string': config('CONNECTION_STRING_METRO_DEMO', None),
'handlers': [
{
'subject': 'MetroDemo/Type/GeekJokes',
'regex': False,
'handler_function': 'demoproj.demoapp.services.my_func'
}
],
},
],
'worker_type': 'celery', # default
}
The handler_function
is defined by providing the full dotted path as a string. For example,from demoproj.demoapp.services import my_func
is provided as 'demoproj.demoapp.services.my_func'
.
The handlers subject can be a regular expression or a string. If a regular expression is provided, the variable regex must be set to True. Example:
'handlers': [{'subject': r'^MetroDemo/Type/.*$','regex':True,'handler_function': my_func}],
Django-GUID
by adding the app to your installed apps, to your middlewares and configuring logging
as described here.
Make sure you enable the CeleryIntegration
:from django_guid.integrations import CeleryIntegration
DJANGO_GUID = {
'INTEGRATIONS': [
CeleryIntegration(
use_django_logging=True,
log_parent=True,
)
],
}
Your functions will be called with keyword arguments for
message
, topic_name
, subscription_name
and subject
. You function should in other words
look something like this:
@app.task(base=MetroidTask)
def my_func(*, message: dict, topic_name: str, subscription_name: str, subject: str) -> None:
def my_func(*, message: dict, topic_name: str, subscription_name: str, subject: str) -> None:
docker-compose up
python manage.py migrate
python manage.py createsuperuser
celery -A demoproj worker -l info
python manage.py metroid
demoproj/demoapp/services.py
python manage.py runserver 8000
http://localhost:8080/admin
To contribute, please see CONTRIBUTING.md