From 5f27417111e9acc3274bd658cd5d3c0a85de566c Mon Sep 17 00:00:00 2001 From: Jorge Batista Date: Mon, 27 Feb 2023 21:40:32 +0000 Subject: [PATCH 1/2] allows setting max retries on the settings --- django_dbconn_retry/apps.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/django_dbconn_retry/apps.py b/django_dbconn_retry/apps.py index 04f9dad..1213b56 100644 --- a/django_dbconn_retry/apps.py +++ b/django_dbconn_retry/apps.py @@ -4,6 +4,7 @@ from django.db import utils as django_db_utils from django.db.backends.base import base as django_db_base from django.dispatch import Signal +from django.conf import settings from typing import Union, Tuple, Callable, List # noqa. flake8 #118 @@ -51,7 +52,7 @@ def ensure_connection_with_retries(self: django_db_base.BaseDatabaseWrapper) -> self.connect() except Exception as e: if isinstance(e, _operror_types): - if hasattr(self, "_connection_retries") and self._connection_retries >= 1: + if hasattr(self, "_connection_retries") and self._connection_retries >= getattr(settings, "MAX_DBCONN_RETRIES", 1): _log.error("Reconnecting to the database didn't help %s", str(e)) del self._in_connecting post_reconnect.send(self.__class__, dbwrapper=self) From 899b75c52b13d1e9927f38846713f675f7545c90 Mon Sep 17 00:00:00 2001 From: Jorge Batista Date: Mon, 27 Feb 2023 21:50:52 +0000 Subject: [PATCH 2/2] added the increment --- django_dbconn_retry/apps.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/django_dbconn_retry/apps.py b/django_dbconn_retry/apps.py index 1213b56..915b8c0 100644 --- a/django_dbconn_retry/apps.py +++ b/django_dbconn_retry/apps.py @@ -60,7 +60,10 @@ def ensure_connection_with_retries(self: django_db_base.BaseDatabaseWrapper) -> else: _log.info("Database connection failed. Refreshing...") # mark the retry - self._connection_retries = 1 + if not hasattr(self, "_connection_retries"): + self._connection_retries = 0 + + self._connection_retries += 1 # ensure that we retry the connection. Sometimes .closed isn't set correctly. self.connection = None del self._in_connecting @@ -70,8 +73,7 @@ def ensure_connection_with_retries(self: django_db_base.BaseDatabaseWrapper) -> self.ensure_connection() post_reconnect.send(self.__class__, dbwrapper=self) else: - _log.debug("Database connection failed, but not due to a known error for dbconn_retry %s", - str(e)) + _log.debug("Database connection failed, but not due to a known error for dbconn_retry %s", str(e)) del self._in_connecting raise else: