From 45c061292156b55265923a16601cd172c1ffdf09 Mon Sep 17 00:00:00 2001 From: Dimas Ciputra Date: Thu, 8 Aug 2024 08:19:43 +0100 Subject: [PATCH] Fix download taxa list because interface closed (#4136) --- bims/database_backend/base.py | 22 +++++++++++++++++++++- bims/tasks/email_csv.py | 5 +++-- core/settings/contrib.py | 1 - core/settings/dev_docker.py | 4 ++-- core/settings/prod_docker.py | 2 +- deployment/docker/REQUIREMENTS.txt | 3 +-- deployment/production/REQUIREMENTS.txt | 3 +-- 7 files changed, 29 insertions(+), 11 deletions(-) diff --git a/bims/database_backend/base.py b/bims/database_backend/base.py index a523df1a0..4817b02a1 100644 --- a/bims/database_backend/base.py +++ b/bims/database_backend/base.py @@ -1,11 +1,31 @@ +import time + import django.db -from psycopg2 import InterfaceError +from psycopg2 import InterfaceError, OperationalError from django_tenants.postgresql_backend.base import ( DatabaseWrapper as TenantDatabaseWrapper ) class DatabaseWrapper(TenantDatabaseWrapper): + def _cursor(self, name=None): + try: + return super()._cursor(name) + except (OperationalError, InterfaceError) as e: + if 'connection already closed' in str(e): + self.close() + self.connect() + return super()._cursor(name) + else: + raise + + def connect(self): + try: + super().connect() + except (OperationalError, InterfaceError): + time.sleep(1) + super().connect() + def create_cursor(self, name=None): try: return super().create_cursor(name=name) diff --git a/bims/tasks/email_csv.py b/bims/tasks/email_csv.py index b4899ffa0..647b12620 100644 --- a/bims/tasks/email_csv.py +++ b/bims/tasks/email_csv.py @@ -3,7 +3,7 @@ import zipfile from celery import shared_task -from django.db import DatabaseError, connection, OperationalError +from django.db import DatabaseError, connection, OperationalError, connections from openpyxl import load_workbook from bims.utils.domain import get_current_domain @@ -54,7 +54,8 @@ def send_csv_via_email( download_request.save() except (DatabaseError, OperationalError): # Attempt to reconnect and save again - connection.connect() + if not connections['default'].is_usable(): + connections['default'].connect() download_request.save() email_template = 'csv_download/csv_created' diff --git a/core/settings/contrib.py b/core/settings/contrib.py index 66e154506..ccee736ca 100644 --- a/core/settings/contrib.py +++ b/core/settings/contrib.py @@ -40,7 +40,6 @@ 'grappelli', 'django_tenants', 'tenants', - 'django_dbconn_retry', 'colorfield', 'polymorphic', diff --git a/core/settings/dev_docker.py b/core/settings/dev_docker.py index 59a284785..f1ec4496c 100644 --- a/core/settings/dev_docker.py +++ b/core/settings/dev_docker.py @@ -39,7 +39,7 @@ REPLICAS = extract_replicas(REPLICA_ENV_VAR) for index, replica in enumerate(REPLICAS, start=1): DATABASES[f'replica_{index}'] = { - 'ENGINE': os.getenv('DB_ENGINE', 'django_tenants.postgresql_backend'), + 'ENGINE': 'bims.database_backend', 'NAME': replica['NAME'], 'USER': replica['USER'], 'PASSWORD': replica['PASSWORD'], @@ -73,7 +73,7 @@ 'debug_toolbar', ] - MIDDLEWARE += ('debug_toolbar.middleware.DebugToolbarMiddleware',) + # MIDDLEWARE += ('debug_toolbar.middleware.DebugToolbarMiddleware',) DEBUG_TOOLBAR_CONFIG = { 'SHOW_TOOLBAR_CALLBACK': lambda request: True, diff --git a/core/settings/prod_docker.py b/core/settings/prod_docker.py index 328937a78..7471c5658 100644 --- a/core/settings/prod_docker.py +++ b/core/settings/prod_docker.py @@ -34,7 +34,7 @@ REPLICAS = extract_replicas(REPLICA_ENV_VAR) for index, replica in enumerate(REPLICAS, start=1): DATABASES[f'replica_{index}'] = { - 'ENGINE': os.getenv('DB_ENGINE', 'django_tenants.postgresql_backend'), + 'ENGINE': 'bims.database_backend', 'NAME': replica['NAME'], 'USER': replica['USER'], 'PASSWORD': replica['PASSWORD'], diff --git a/deployment/docker/REQUIREMENTS.txt b/deployment/docker/REQUIREMENTS.txt index c3edd87d7..d5997a366 100644 --- a/deployment/docker/REQUIREMENTS.txt +++ b/deployment/docker/REQUIREMENTS.txt @@ -96,5 +96,4 @@ openpyxl==3.1.2 celery==5.3.6 django-celery-results==2.5.1 django-tenants==3.6.1 -tenant-schemas-celery==2.2.0 -git+https://github.com/dimasciput/django-dbconn-retry.git \ No newline at end of file +tenant-schemas-celery==2.2.0 \ No newline at end of file diff --git a/deployment/production/REQUIREMENTS.txt b/deployment/production/REQUIREMENTS.txt index c3edd87d7..d5997a366 100644 --- a/deployment/production/REQUIREMENTS.txt +++ b/deployment/production/REQUIREMENTS.txt @@ -96,5 +96,4 @@ openpyxl==3.1.2 celery==5.3.6 django-celery-results==2.5.1 django-tenants==3.6.1 -tenant-schemas-celery==2.2.0 -git+https://github.com/dimasciput/django-dbconn-retry.git \ No newline at end of file +tenant-schemas-celery==2.2.0 \ No newline at end of file