feat: Add healthcheck

This commit is contained in:
moanos [he/him] 2024-10-10 18:35:22 +02:00
parent e38234b736
commit 74a6b5f2aa
4 changed files with 30 additions and 8 deletions

View File

@ -1,5 +1,6 @@
from notfellchen.celery import app as celery_app
from .tools.admin import clean_locations, deactivate_unchecked_adoption_notices
from .tools.misc import healthcheck_ok
from .models import Location, AdoptionNotice
@ -17,3 +18,7 @@ def task_deactivate_unchecked():
def add_adoption_notice_location(pk):
instance = AdoptionNotice.objects.get(pk=pk)
Location.add_location_to_object(instance)
@celery_app.task(name="tools.healthcheck")
def task_healthcheck():
healthcheck_ok()

View File

@ -1,4 +1,8 @@
import datetime as datetime
import logging
from notfellchen import settings
import requests
def pluralize(number, letter="e"):
@ -23,3 +27,10 @@ def age_as_hr_string(age: datetime.timedelta) -> str:
return f'{weeks:.0f} Woche{pluralize(weeks, "n")}'
else:
return f'{days:.0f} Tag{pluralize(days)}'
def healthcheck_ok():
try:
requests.get(settings.HEALTHCHECK_URL, timeout=10)
except requests.RequestException as e:
logging.error("Ping to healthcheck-server failed: %s" % e)

View File

@ -1,8 +1,7 @@
# <your_project>/celery.py
import os
from celery import Celery
from celery.schedules import crontab
from notfellchen import settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'notfellchen.settings')
@ -19,7 +18,12 @@ app.conf.beat_schedule = {
},
'daily-deactivation': {
'task': 'admin.deactivate_unchecked',
'schedule': 30,
}
'schedule': crontab(hour=1),
},
}
if settings.HEALTHCHECK_URL is not None:
# If a healthcheck is configured, this will send a daily ping to the healthchecks server
app.conf.beat_schedule['daily-healthcheck'] = {'task': 'tools.healthcheck',
'schedule': crontab(hour=2),
}

View File

@ -84,6 +84,8 @@ LOCALE_PATHS = [os.path.join(BASE_DIR, 'locale')]
CELERY_BROKER_URL = config.get("celery", "broker", fallback="redis://localhost:6379/0")
CELERY_RESULT_BACKEND = config.get("celery", "backend", fallback="redis://localhost:6379/0")
""" MONITORING """
HEALTHCHECKS_URL = config.get("monitoring", "healthchecks_url", fallback=None)
""" GEOCODING """
GEOCODING_API_URL = config.get("geocoding", "api_url", fallback="https://nominatim.hyteck.de/search")