Maintenance Mode Fix (#6422)

* Custom maintenance mode backend

* Implement check and retries

* Update debug formatting
This commit is contained in:
Oliver 2024-02-07 10:52:10 +11:00 committed by GitHub
parent df5a3013e6
commit 55949e5321
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 61 additions and 2 deletions

View File

@ -0,0 +1,59 @@
"""Custom backend implementations."""
import logging
import time
from django.db.utils import IntegrityError, OperationalError, ProgrammingError
from maintenance_mode.backends import AbstractStateBackend
import common.models
import InvenTree.helpers
logger = logging.getLogger('inventree')
class InvenTreeMaintenanceModeBackend(AbstractStateBackend):
"""Custom backend for managing state of maintenance mode.
Stores the current state of the maintenance mode in the database,
using an InvenTreeSetting object.
"""
SETTING_KEY = '_MAINTENANCE_MODE'
def get_value(self) -> bool:
"""Get the current state of the maintenance mode.
Returns:
bool: True if maintenance mode is active, False otherwise.
"""
value = InvenTree.helpers.str2bool(
common.models.InvenTreeSetting.get_setting(
self.SETTING_KEY, backup_value=False, create=False, cache=False
)
)
logger.debug('Maintenance mode state: %s', value)
return value
def set_value(self, value: bool, retries: int = 5):
"""Set the state of the maintenance mode."""
logger.debug('Setting maintenance mode state: %s', value)
while retries > 0:
try:
common.models.InvenTreeSetting.set_setting(self.SETTING_KEY, value)
# Read the value back to confirm
if self.get_value() == value:
break
except (IntegrityError, OperationalError, ProgrammingError):
logger.warning(
'Failed to set maintenance mode state in database (%s retries left)',
retries,
)
time.sleep(0.1)
retries -= 1

View File

@ -1080,8 +1080,8 @@ MARKDOWNIFY = {
IGNORED_ERRORS = [Http404, django.core.exceptions.PermissionDenied]
# Maintenance mode
MAINTENANCE_MODE_RETRY_AFTER = 60
MAINTENANCE_MODE_STATE_BACKEND = 'maintenance_mode.backends.StaticStorageBackend'
MAINTENANCE_MODE_RETRY_AFTER = 10
MAINTENANCE_MODE_STATE_BACKEND = 'InvenTree.backends.InvenTreeMaintenanceModeBackend'
# Are plugins enabled?
PLUGINS_ENABLED = get_boolean_setting(