From a89fe54923d2ff39400f6bd1bdaa68959cc43327 Mon Sep 17 00:00:00 2001 From: shtlrs Date: Fri, 7 Jun 2024 13:23:59 +0200 Subject: add a management command to close all connections --- pydis_site/apps/api/management/__init__.py | 0 pydis_site/apps/api/management/commands/__init__.py | 0 .../api/management/commands/close_db_connections.py | 19 +++++++++++++++++++ 3 files changed, 19 insertions(+) create mode 100644 pydis_site/apps/api/management/__init__.py create mode 100644 pydis_site/apps/api/management/commands/__init__.py create mode 100644 pydis_site/apps/api/management/commands/close_db_connections.py diff --git a/pydis_site/apps/api/management/__init__.py b/pydis_site/apps/api/management/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/pydis_site/apps/api/management/commands/__init__.py b/pydis_site/apps/api/management/commands/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/pydis_site/apps/api/management/commands/close_db_connections.py b/pydis_site/apps/api/management/commands/close_db_connections.py new file mode 100644 index 00000000..4de74a92 --- /dev/null +++ b/pydis_site/apps/api/management/commands/close_db_connections.py @@ -0,0 +1,19 @@ +from django.core.management.base import BaseCommand +from django.db import connections +import logging + +logger = logging.getLogger(__name__) + + +class Command(BaseCommand): + """A command to to close all the open connections to the database.""" + + help = "Closes all the open connections to the database" + + def handle(self, *args, **options) -> None: + """Handle the connection closing command invocation.""" + logger.info("Closing all database connections") + try: + connections.close_all() + except Exception as e: + logger.exception(e) -- cgit v1.2.3