From f96724a4db41be64fe31a3c36d663ed373088751 Mon Sep 17 00:00:00 2001 From: Klaas van Schelven Date: Tue, 9 Jan 2024 19:16:07 +0100 Subject: [PATCH] celery: basic config (quickstart); alerts startapp I've checked that this celery config actually works (also with TASK_ALWAYS_EAGER=False) --- alerts/__init__.py | 0 alerts/admin.py | 3 +++ alerts/apps.py | 6 ++++++ alerts/migrations/__init__.py | 0 alerts/models.py | 3 +++ alerts/tasks.py | 6 ++++++ alerts/tests.py | 3 +++ alerts/views.py | 3 +++ bugsink/__init__.py | 4 ++++ bugsink/celery.py | 20 ++++++++++++++++++++ bugsink/settings.py | 11 +++++++++++ ingest/views.py | 2 +- requirements.txt | 1 + 13 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 alerts/__init__.py create mode 100644 alerts/admin.py create mode 100644 alerts/apps.py create mode 100644 alerts/migrations/__init__.py create mode 100644 alerts/models.py create mode 100644 alerts/tasks.py create mode 100644 alerts/tests.py create mode 100644 alerts/views.py create mode 100644 bugsink/celery.py diff --git a/alerts/__init__.py b/alerts/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/alerts/admin.py b/alerts/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/alerts/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/alerts/apps.py b/alerts/apps.py new file mode 100644 index 0000000..0a64765 --- /dev/null +++ b/alerts/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class AlertsConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'alerts' diff --git a/alerts/migrations/__init__.py b/alerts/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/alerts/models.py b/alerts/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/alerts/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/alerts/tasks.py b/alerts/tasks.py new file mode 100644 index 0000000..8335010 --- /dev/null +++ b/alerts/tasks.py @@ -0,0 +1,6 @@ +from celery import shared_task + + +@shared_task +def add(x, y): + return x + y diff --git a/alerts/tests.py b/alerts/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/alerts/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/alerts/views.py b/alerts/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/alerts/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/bugsink/__init__.py b/bugsink/__init__.py index e69de29..ea51cc2 100644 --- a/bugsink/__init__.py +++ b/bugsink/__init__.py @@ -0,0 +1,4 @@ +# 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/bugsink/celery.py b/bugsink/celery.py new file mode 100644 index 0000000..2008063 --- /dev/null +++ b/bugsink/celery.py @@ -0,0 +1,20 @@ +import os + +from celery import Celery + +# Set the default Django settings module for the 'celery' program. +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'bugsink.settings') + +app = Celery('bugsink') + +# 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() + + +@app.task(bind=True, ignore_result=True) +def debug_task(self): + print(f'Request: {self.request!r}') diff --git a/bugsink/settings.py b/bugsink/settings.py index 74c420e..d6bb14d 100644 --- a/bugsink/settings.py +++ b/bugsink/settings.py @@ -41,6 +41,7 @@ INSTALLED_APPS = [ 'ingest', 'issues', 'events', + 'alerts', 'performance', ] @@ -137,6 +138,12 @@ STATICFILES_DIRS = [ DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' +# Generic CELERY settings (i.e. no expected config per-installation) + +CELERY_IGNORE_RESULT = True # we don't use the "result" part of celery +CELERY_BROKER_CONNECTION_TIMEOUT = 2.5 # long enough for glitches, short enough to get notified about real problems + + # ###################### SERVER-MODE SETTINGS ################# BUGSINK_DIGEST_IMMEDIATELY = True @@ -157,3 +164,7 @@ if SENTRY_DSN is not None: ) BASE_URL = "http://glitchtip:9000" # no trailing slash + + +CELERY_BROKER_URL = 'amqp://bugsink:bugsink@localhost/' +CELERY_TASK_ALWAYS_EAGER = True diff --git a/ingest/views.py b/ingest/views.py index 340bb84..30b1a0e 100644 --- a/ingest/views.py +++ b/ingest/views.py @@ -70,7 +70,7 @@ class BaseIngestAPIView(APIView): # before proceeding because it may be useful for debugging errors in the digest process. ingested_event = cls.ingest_event(now, event_data, request, project) if settings.BUGSINK_DIGEST_IMMEDIATELY: - cls.digest_event(ingested_event, event_data) + cls.digest_event(ingested_event, event_data, alerter_foo) @classmethod def ingest_event(cls, now, event_data, request, project): diff --git a/requirements.txt b/requirements.txt index e3826cf..a92fc1c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,4 @@ djangorestframework==3.14.* django-tailwind==3.6.* jsonschema==4.19.* semver==3.0.* +Celery[librabbitmq]