From 74a6b5f2aa7fa4702ee9e00c0c52b22a0b39f9f7 Mon Sep 17 00:00:00 2001 From: moanos Date: Thu, 10 Oct 2024 18:35:22 +0200 Subject: [PATCH] feat: Add healthcheck --- src/fellchensammlung/tasks.py | 5 +++++ src/fellchensammlung/tools/misc.py | 19 +++++++++++++++---- src/notfellchen/celery.py | 12 ++++++++---- src/notfellchen/settings.py | 2 ++ 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/fellchensammlung/tasks.py b/src/fellchensammlung/tasks.py index 969c318..418e9c0 100644 --- a/src/fellchensammlung/tasks.py +++ b/src/fellchensammlung/tasks.py @@ -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() diff --git a/src/fellchensammlung/tools/misc.py b/src/fellchensammlung/tools/misc.py index c3ab94b..8bfe2c5 100644 --- a/src/fellchensammlung/tools/misc.py +++ b/src/fellchensammlung/tools/misc.py @@ -1,4 +1,8 @@ import datetime as datetime +import logging + +from notfellchen import settings +import requests def pluralize(number, letter="e"): @@ -11,11 +15,11 @@ def pluralize(number, letter="e"): def age_as_hr_string(age: datetime.timedelta) -> str: days = age.days - weeks = age.days/7 - months = age.days/30 - years = age.days/365 + weeks = age.days / 7 + months = age.days / 30 + years = age.days / 365 if years >= 1: - months = months - 12*years + months = months - 12 * years return f'{years:.0f} Jahr{pluralize(years)} und {months:.0f} Monat{pluralize(months)}' elif months >= 3: return f'{months:.0f} Monat{pluralize(months)}' @@ -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) diff --git a/src/notfellchen/celery.py b/src/notfellchen/celery.py index 042be7c..38b7e75 100644 --- a/src/notfellchen/celery.py +++ b/src/notfellchen/celery.py @@ -1,8 +1,7 @@ -# /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), + } diff --git a/src/notfellchen/settings.py b/src/notfellchen/settings.py index 0d8eb5a..4d6a062 100644 --- a/src/notfellchen/settings.py +++ b/src/notfellchen/settings.py @@ -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")