From 2bd29e2fddb1baa6ad14050e5ab2845ce8e032f0 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Mon, 1 Aug 2022 23:31:47 +0200 Subject: [PATCH] *: improve error handling for startup tasks Signed-off-by: Jens Langhammer --- authentik/admin/tasks.py | 5 ++++- authentik/blueprints/manager.py | 4 ++-- authentik/blueprints/v1/tasks.py | 4 +++- authentik/lib/sentry.py | 3 ++- authentik/outposts/tasks.py | 11 +++++++++-- authentik/providers/proxy/tasks.py | 6 +++++- 6 files changed, 25 insertions(+), 8 deletions(-) diff --git a/authentik/admin/tasks.py b/authentik/admin/tasks.py index f571d68b5..8289d4d1d 100644 --- a/authentik/admin/tasks.py +++ b/authentik/admin/tasks.py @@ -3,6 +3,7 @@ import re from django.core.cache import cache from django.core.validators import URLValidator +from django.db import DatabaseError, InternalError, ProgrammingError from packaging.version import parse from requests import RequestException from structlog.stdlib import get_logger @@ -39,7 +40,9 @@ def _set_prom_info(): ) -@CELERY_APP.task() +@CELERY_APP.task( + throws=(DatabaseError, ProgrammingError, InternalError), +) def clear_update_notifications(): """Clear update notifications on startup if the notification was for the version we're running now.""" diff --git a/authentik/blueprints/manager.py b/authentik/blueprints/manager.py index d4e98b86c..aa65cbf7b 100644 --- a/authentik/blueprints/manager.py +++ b/authentik/blueprints/manager.py @@ -3,7 +3,7 @@ from importlib import import_module from inspect import ismethod from django.apps import AppConfig -from django.db import DatabaseError, ProgrammingError +from django.db import DatabaseError, InternalError, ProgrammingError from structlog.stdlib import get_logger LOGGER = get_logger() @@ -33,5 +33,5 @@ class ManagedAppConfig(AppConfig): try: meth() LOGGER.debug("Successfully reconciled", name=name) - except (ProgrammingError, DatabaseError) as exc: + except (DatabaseError, ProgrammingError, InternalError) as exc: LOGGER.debug("Failed to run reconcile", name=name, exc=exc) diff --git a/authentik/blueprints/v1/tasks.py b/authentik/blueprints/v1/tasks.py index 528ab6ed1..e3120afd3 100644 --- a/authentik/blueprints/v1/tasks.py +++ b/authentik/blueprints/v1/tasks.py @@ -19,7 +19,9 @@ from authentik.lib.config import CONFIG from authentik.root.celery import CELERY_APP -@CELERY_APP.task() +@CELERY_APP.task( + throws=(DatabaseError, ProgrammingError, InternalError), +) @prefill_task def blueprints_discover(): """Find blueprints and check if they need to be created in the database""" diff --git a/authentik/lib/sentry.py b/authentik/lib/sentry.py index 2b5990604..271b2f2b1 100644 --- a/authentik/lib/sentry.py +++ b/authentik/lib/sentry.py @@ -8,7 +8,7 @@ from channels.middleware import BaseMiddleware from channels_redis.core import ChannelFull from django.conf import settings from django.core.exceptions import ImproperlyConfigured, SuspiciousOperation, ValidationError -from django.db import InternalError, OperationalError, ProgrammingError +from django.db import DatabaseError, InternalError, OperationalError, ProgrammingError from django.http.response import Http404 from django_redis.exceptions import ConnectionInterrupted from docker.errors import DockerException @@ -116,6 +116,7 @@ def before_send(event: dict, hint: dict) -> Optional[dict]: # Django Errors Error, ImproperlyConfigured, + DatabaseError, OperationalError, InternalError, ProgrammingError, diff --git a/authentik/outposts/tasks.py b/authentik/outposts/tasks.py index 9a4c00ddc..ce1c713a2 100644 --- a/authentik/outposts/tasks.py +++ b/authentik/outposts/tasks.py @@ -10,6 +10,7 @@ import yaml from asgiref.sync import async_to_sync from channels.layers import get_channel_layer from django.core.cache import cache +from django.db import DatabaseError, InternalError, ProgrammingError from django.db.models.base import Model from django.utils.text import slugify from docker.constants import DEFAULT_UNIX_SOCKET @@ -87,7 +88,11 @@ def outpost_service_connection_state(connection_pk: Any): cache.set(connection.state_key, state, timeout=None) -@CELERY_APP.task(bind=True, base=MonitoredTask) +@CELERY_APP.task( + bind=True, + base=MonitoredTask, + throws=(DatabaseError, ProgrammingError, InternalError), +) @prefill_task def outpost_service_connection_monitor(self: MonitoredTask): """Regularly check the state of Outpost Service Connections""" @@ -102,7 +107,9 @@ def outpost_service_connection_monitor(self: MonitoredTask): ) -@CELERY_APP.task() +@CELERY_APP.task( + throws=(DatabaseError, ProgrammingError, InternalError), +) def outpost_controller_all(): """Launch Controller for all Outposts which support it""" for outpost in Outpost.objects.exclude(service_connection=None): diff --git a/authentik/providers/proxy/tasks.py b/authentik/providers/proxy/tasks.py index 2261c2f09..a5a4dc45f 100644 --- a/authentik/providers/proxy/tasks.py +++ b/authentik/providers/proxy/tasks.py @@ -1,9 +1,13 @@ """proxy provider tasks""" +from django.db import DatabaseError, InternalError, ProgrammingError + from authentik.providers.proxy.models import ProxyProvider from authentik.root.celery import CELERY_APP -@CELERY_APP.task() +@CELERY_APP.task( + throws=(DatabaseError, ProgrammingError, InternalError), +) def proxy_set_defaults(): """Ensure correct defaults are set for all providers""" for provider in ProxyProvider.objects.all():