Package: metroid
MetroidSubscribe, act, publish.Metroid - Metro for DjangoThis app is intended to streamline integration with Metro for all Django+Celery users by:Asynchronous handling of subscriptions and messages with one commandExecute Celery tasks based on message topics, defined insettings.pyRetry failed tasks through your admin dashboard when using theMetroidTaskbaseOverviewpython>= 3.10django>= 4.2 - Forasgiref, settingsdjango-guid>= 3.2.0 - Storing correlation IDs for failed tasks in the database, making debugging easyChoose one:celery>= 5.3.0 - Execute tasks based on a subjectdjango-rq>= 2.4.1 - Execute tasks based on a subjectImplementationThepython manage.py metroidapp is fully asynchronous, and has no blocking code. It utilizesCeleryto execute tasks.It works by:Going through all your configured subscriptions and start a new async connection for each one of themMetro sends messages on the subscriptionsThis app filters out messages matching subjects you have defined, and queues a celery task to execute
the function as specified for that subject3.1. If no task is found for that subject, the message is marked as completeThe message is marked as complete after the Celery task has successfully been queuedIf the task is failed, an entry is automatically created in your databaseAll failed tasks can be retried manually through the admin dashboardConfigure and install this packageNoteFor a complete example, have a look indemoproj/settings.py.Create aMETROIDkey 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}Thehandler_functionis defined by providing the full dotted path as a string. For example,from demoproj.demoapp.services import my_funcis 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}],ConfigureDjango-GUIDby adding the app to your installed apps, to your middlewares and configuring logging
as describedhere.
Make sure you enable theCeleryIntegration:fromdjango_guid.integrationsimportCeleryIntegrationDJANGO_GUID={'INTEGRATIONS':[CeleryIntegration(use_django_logging=True,log_parent=True,)],}Creating your own handler functionsYour functions will be called with keyword arguments formessage,topic_name,subscription_nameandsubject. You function should in other words
look something like this:Celery@app.task(base=MetroidTask)defmy_func(*,message:dict,topic_name:str,subscription_name:str,subject:str)->None:rqdefmy_func(*,message:dict,topic_name:str,subscription_name:str,subject:str)->None:Running the projectEnsure you have redis running:docker-composeupRun migrationspythonmanage.pymigrateCreate an admin accountpythonmanage.pycreatesuperuserStart a worker:celery-Ademoprojworker-linfoRun the subscriber:pythonmanage.pymetroidSend messages to Metro. Example code can be found indemoproj/demoapp/services.pyRun the webserver:pythonmanage.pyrunserver8000See failed messages underhttp://localhost:8080/adminTo contribute, please seeCONTRIBUTING.md