
Metroid
Subscribe, act, publish.
Metroid - Metro for Django
This app is intended to streamline integration with Metro for all Django+Celery users by:
- Asynchronous handling of subscriptions and messages with one command
- Execute Celery tasks based on message topics, defined in
settings.py - Retry failed tasks through your admin dashboard when using the
MetroidTaskbase
Overview
python>= 3.10django>= 4.2 - Forasgiref, settingsdjango-guid>= 3.2.0 - Storing correlation IDs for failed tasks in the database, making debugging easy- Choose one:
celery>= 5.3.0 - Execute tasks based on a subjectdjango-rq>= 2.4.1 - Execute tasks based on a subject
Implementation
The python manage.py metroid app is fully asynchronous, and has no blocking code. It utilizes Celery to execute tasks.
It works by:
- Going through all your configured subscriptions and start a new async connection for each one of them
- Metro sends messages on the subscriptions
- This app filters out messages matching subjects you have defined, and queues a celery task to execute
the function as specified for that subject
3.1. If no task is found for that subject, the message is marked as complete - The message is marked as complete after the Celery task has successfully been queued
- If the task is failed, an entry is automatically created in your database
- All failed tasks can be retried manually through the admin dashboard
Configure and install this package
Note For a complete example, have a look in
demoproj/settings.py.
- Create a
METROIDkey insettings.pywith 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}],
- Configure
Django-GUIDby adding the app to your installed apps, to your middlewares and configuring logging as described here. Make sure you enable theCeleryIntegration:
from django_guid.integrations import CeleryIntegration
DJANGO_GUID = {
'INTEGRATIONS': [
CeleryIntegration(
use_django_logging=True,
log_parent=True,
)
],
}
Creating your own handler functions
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:
Celery
@app.task(base=MetroidTask)
def my_func(*, message: dict, topic_name: str, subscription_name: str, subject: str) -> None:
rq
def my_func(*, message: dict, topic_name: str, subscription_name: str, subject: str) -> None:
Running the project
- Ensure you have redis running:
docker-compose up
- Run migrations
python manage.py migrate
- Create an admin account
python manage.py createsuperuser
- Start a worker:
celery -A demoproj worker -l info
- Run the subscriber:
python manage.py metroid
- Send messages to Metro. Example code can be found in
demoproj/demoapp/services.py - Run the webserver:
python manage.py runserver 8000
- See failed messages under
http://localhost:8080/admin
To contribute, please see CONTRIBUTING.md
Metadata
Release files for metroid 1.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| metroid-1.4.0.tar.gz | 15.0 kB | Without provenance | Details for metroid-1.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| metroid-1.4.0-py3-none-any.whl | Python 3 | none | any | Details for metroid-1.4.0-py3-none-any.whl |
Total release size: 33.1 kB
Release files / metroid-1.4.0.tar.gz
| Download URL | metroid-1.4.0.tar.gz |
|---|---|
| Size | 15.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
166b17b1f84bf2bcfbea17a0695b8ece0ac18f1f490c07d6b761ec9b100650b0
|
|
BLAKE2b-256 checksum How to use checksums |
fae9246f0d800ad901d77342e03d8ed0641600038f7e3e85214283b4a49a6be8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.9.18 Linux/6.2.0-1018-azure
|
Release files / metroid-1.4.0-py3-none-any.whl
| Download URL | metroid-1.4.0-py3-none-any.whl |
|---|---|
| Size | 18.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a108154d7e02116941b8ac10f6077a4224c5143d4e65f30427ecd33c75cd002e
|
|
BLAKE2b-256 checksum How to use checksums |
7e180b24a18a8738462152941150e7dbd6724dae64f10847cefa56f8ef1e2946
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.9.18 Linux/6.2.0-1018-azure
|
Security for metroid 1.4.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.