Skip to content

Commit

Permalink
Add webhook support
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew-Dickinson committed Jan 27, 2024
1 parent 662ef24 commit cd21c40
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 3 deletions.
14 changes: 12 additions & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -67,5 +79,3 @@ volumes:

networks:
api:
external: true
name: traefik-net
6 changes: 6 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.*",
Expand Down
5 changes: 5 additions & 0 deletions src/meshdb/__init__.py
Original file line number Diff line number Diff line change
@@ -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",)
18 changes: 18 additions & 0 deletions src/meshdb/celery.py
Original file line number Diff line number Diff line change
@@ -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()
14 changes: 13 additions & 1 deletion src/meshdb/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -73,6 +73,7 @@
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"django_webhook",
"rest_framework",
"rest_framework.authtoken",
"meshapi",
Expand Down Expand Up @@ -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",
]
)

0 comments on commit cd21c40

Please sign in to comment.