Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions netbox/netbox/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,23 @@ def __init__(self, get_response):

def __call__(self, request):
if get_config().MAINTENANCE_MODE:
self._prevent_db_write_operations()
self._set_session_type(
allow_write=request.path_info.startswith(settings.MAINTENANCE_EXEMPT_PATHS)
)

return self.get_response(request)

@staticmethod
def _prevent_db_write_operations():
def _set_session_type(allow_write):
"""
Prevent any write-related database operations.

Args:
allow_write (bool): If True, write operations will be permitted.
"""
with connection.cursor() as cursor:
cursor.execute(
'SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY;'
)
mode = 'READ WRITE' if allow_write else 'READ ONLY'
cursor.execute(f'SET SESSION CHARACTERISTICS AS TRANSACTION {mode};')

def process_exception(self, request, exception):
"""
Expand Down
5 changes: 5 additions & 0 deletions netbox/netbox/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,11 @@ def _setting(name, default=None):
f'/{BASE_PATH}metrics',
)

# All URLs starting with a string listed here are exempt from maintenance mode enforcement
MAINTENANCE_EXEMPT_PATHS = (
f'/{BASE_PATH}admin/',
)

SERIALIZATION_MODULES = {
'json': 'utilities.serializers.json',
}
Expand Down