celery: basic config (quickstart); alerts startapp

I've checked that this celery config actually works (also with TASK_ALWAYS_EAGER=False)
This commit is contained in:
Klaas van Schelven
2024-01-09 19:16:07 +01:00
parent 9936c93b4e
commit f96724a4db
13 changed files with 61 additions and 1 deletions

0
alerts/__init__.py Normal file
View File

3
alerts/admin.py Normal file
View File

@@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
alerts/apps.py Normal file
View File

@@ -0,0 +1,6 @@
from django.apps import AppConfig
class AlertsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'alerts'

View File

3
alerts/models.py Normal file
View File

@@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

6
alerts/tasks.py Normal file
View File

@@ -0,0 +1,6 @@
from celery import shared_task
@shared_task
def add(x, y):
return x + y

3
alerts/tests.py Normal file
View File

@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

3
alerts/views.py Normal file
View File

@@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.

View File

@@ -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',)

20
bugsink/celery.py Normal file
View File

@@ -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}')

View File

@@ -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

View File

@@ -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):

View File

@@ -4,3 +4,4 @@ djangorestframework==3.14.*
django-tailwind==3.6.*
jsonschema==4.19.*
semver==3.0.*
Celery[librabbitmq]