Skip to content

Commit

Permalink
Fix download taxa list because interface closed (#4136)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimasciput authored Aug 8, 2024
1 parent 2541302 commit 45c0612
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 11 deletions.
22 changes: 21 additions & 1 deletion bims/database_backend/base.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
5 changes: 3 additions & 2 deletions bims/tasks/email_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand Down
1 change: 0 additions & 1 deletion core/settings/contrib.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
'grappelli',
'django_tenants',
'tenants',
'django_dbconn_retry',

'colorfield',
'polymorphic',
Expand Down
4 changes: 2 additions & 2 deletions core/settings/dev_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion core/settings/prod_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
3 changes: 1 addition & 2 deletions deployment/docker/REQUIREMENTS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
tenant-schemas-celery==2.2.0
3 changes: 1 addition & 2 deletions deployment/production/REQUIREMENTS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
tenant-schemas-celery==2.2.0

0 comments on commit 45c0612

Please sign in to comment.