mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Maintenance Mode Fix (#6422)
* Custom maintenance mode backend * Implement check and retries * Update debug formatting
This commit is contained in:
parent
df5a3013e6
commit
55949e5321
59
InvenTree/InvenTree/backends.py
Normal file
59
InvenTree/InvenTree/backends.py
Normal 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
|
@ -1080,8 +1080,8 @@ MARKDOWNIFY = {
|
|||||||
IGNORED_ERRORS = [Http404, django.core.exceptions.PermissionDenied]
|
IGNORED_ERRORS = [Http404, django.core.exceptions.PermissionDenied]
|
||||||
|
|
||||||
# Maintenance mode
|
# Maintenance mode
|
||||||
MAINTENANCE_MODE_RETRY_AFTER = 60
|
MAINTENANCE_MODE_RETRY_AFTER = 10
|
||||||
MAINTENANCE_MODE_STATE_BACKEND = 'maintenance_mode.backends.StaticStorageBackend'
|
MAINTENANCE_MODE_STATE_BACKEND = 'InvenTree.backends.InvenTreeMaintenanceModeBackend'
|
||||||
|
|
||||||
# Are plugins enabled?
|
# Are plugins enabled?
|
||||||
PLUGINS_ENABLED = get_boolean_setting(
|
PLUGINS_ENABLED = get_boolean_setting(
|
||||||
|
Loading…
Reference in New Issue
Block a user