From cd21c40edf04a6f89721eed9c7c772c739a9c17e Mon Sep 17 00:00:00 2001 From: Andrew Dickinson Date: Sat, 27 Jan 2024 13:11:00 -0500 Subject: [PATCH] Add webhook support --- docker-compose.yaml | 14 ++++++++++++-- entrypoint.sh | 6 ++++++ pyproject.toml | 2 ++ src/meshdb/__init__.py | 5 +++++ src/meshdb/celery.py | 18 ++++++++++++++++++ src/meshdb/settings.py | 14 +++++++++++++- 6 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 src/meshdb/celery.py diff --git a/docker-compose.yaml b/docker-compose.yaml index febf9d6a0..6469af35c 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -19,6 +19,16 @@ services: volumes: - postgres_data:/var/lib/postgresql/data/ + redis: + healthcheck: + test: [ "CMD", "redis-cli", "--raw", "incr", "ping" ] + networks: + - api + ports: + - 6379:6379 + image: + redis + pelias: networks: - api @@ -28,6 +38,8 @@ services: depends_on: postgres: condition: service_healthy + redis: + condition: service_healthy healthcheck: test: curl http://127.0.0.1:8081/api/v1 interval: 2s @@ -67,5 +79,3 @@ volumes: networks: api: - external: true - name: traefik-net diff --git a/entrypoint.sh b/entrypoint.sh index f94efb211..964fb9dd3 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -8,6 +8,12 @@ done echo 'DB started' +# It's okay to start Celery in the background and continue without waiting, even though "migrate" +# might make DB changes we want to notify for since tasks are queued by Django Webhook and +# are executed as soon as celery starts +echo 'Staring Celery Worker...' +celery -A meshdb worker -l INFO & + echo 'Running Migrations...' python manage.py migrate diff --git a/pyproject.toml b/pyproject.toml index 90ff3f381..d63742c89 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,8 +2,10 @@ name = "nycmesh-meshdb" version = "0.1" dependencies = [ + "celery[redis]==5.3.*", "django==4.2.*", "djangorestframework==3.14.*", + "django-webhook==0.0.*", "psycopg2-binary==2.9.*", "gunicorn==21.2.*", "python-dotenv==1.0.*", diff --git a/src/meshdb/__init__.py b/src/meshdb/__init__.py index e69de29bb..5568b6d79 100644 --- a/src/meshdb/__init__.py +++ b/src/meshdb/__init__.py @@ -0,0 +1,5 @@ +# This will make sure the app is always imported when +# Django starts so that shared_task will use this app. +from .celery import app as celery_app + +__all__ = ("celery_app",) diff --git a/src/meshdb/celery.py b/src/meshdb/celery.py new file mode 100644 index 000000000..57c270381 --- /dev/null +++ b/src/meshdb/celery.py @@ -0,0 +1,18 @@ +import os + +from celery import Celery + +# Set the default Django settings module for the 'celery' program. +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "meshdb.settings") + +# Use the docker-hosted Redis container as the backend for Celery +app = Celery("meshdb", broker="redis://localhost:6379/0") + +# Using a string here means the worker doesn't have to serialize +# the configuration object to child processes. +# - namespace='CELERY' means all celery-related configuration keys +# should have a `CELERY_` prefix. +app.config_from_object("django.conf:settings", namespace="CELERY") + +# Load task modules from all registered Django apps. +app.autodiscover_tasks() diff --git a/src/meshdb/settings.py b/src/meshdb/settings.py index cf7d9f640..61e1ce6d5 100644 --- a/src/meshdb/settings.py +++ b/src/meshdb/settings.py @@ -10,8 +10,8 @@ https://docs.djangoproject.com/en/4.2/ref/settings/ """ -from pathlib import Path import os +from pathlib import Path from dotenv import load_dotenv @@ -73,6 +73,7 @@ "django.contrib.sessions", "django.contrib.messages", "django.contrib.staticfiles", + "django_webhook", "rest_framework", "rest_framework.authtoken", "meshapi", @@ -178,3 +179,14 @@ "rest_framework.authentication.TokenAuthentication", ], } + +# Allow-list models which the admin can select to send webhooks for +DJANGO_WEBHOOK = dict( + MODELS=[ + "meshapi.Building", + "meshapi.Member", + "meshapi.Install", + "meshapi.Link", + "meshapi.Sector", + ] +)