Wagtail maintenance mode support for easily managing available routes when in maintenance mode.
- Easily manage maintenance mode from the Wagtail admin.
- Different keys for different types of maintenance.
- Realtime updates for maintenance mode with websockets.
- No database required - purely from cache.
- Customizable maintenance mode page.
- Extra functionality for
django_redis
- Persisting the maintenance mode key in the cache.
- Locking the cache for the maintenance mode key.
-
Add 'maintenance' to your INSTALLED_APPS setting like this:
INSTALLED_APPS = [ ..., 'maintenance', ]
-
Add
maintenance.middleware.MaintenanceModeMiddleware
to your middleware settings. -
Navigate to settings in your wagtail admin menu; enjoy the new maintenance mode settings.
-
maintenance.toggle_maintenance_mode
Can toggle maintenance mode on/off. -
maintenance.see_menu_item
Can see the maintenance mode menu item. -
maintenance.see_info
Can see extra maintenance mode information, defined by the key in themaintenance.views.ping
view.
maintenance.modes
A mode is just a plain string, which is used to identify the type of maintenance mode.
We provide a single default mode which can be used for general maintenance.
def maintain(updating: str = MAINTENANCE_MODE_KEY, timeout: int = MONTH, lock_cache=False) -> Maintenance:
Return a maintenance mode context manager for the given key.
This can be used to set/unset maintenance.
It is possible to use the with
statement to automatically unset the maintenance mode after the block.
You are generally able to provide str
and maintenance.keys.Key
instances to the key
parameter.
Example:
from maintenance.modes import maintain
with modes.maintain():
# Do some long running operations which should not be interrupted by users.
pass
Check if the maintenance mode is active for the given request and keys. If no keys are provided, check all registered keys for maintenance mode.
def maintenance_mode(mode: bool, key: str = MAINTENANCE_MODE_KEY, timeout: int = DAY, blocking=False, call_hooks=False):
Set or unset the maintenance mode for the given key.
blocking
is directly passed to threading.Lock.acquire
and django_redis's cache Lock.acquire
method.
Delete any record of maintenance mode, active or not from the cache for this key.
We provide different "keys" for maintenance mode. Keys can be considered as a unique identifier for a specific type of maintenance.
from maintenance import modes, keys
class MyStatusClass(keys.Status):
"""Custom status class to check if the maintenance mode is active for the request."""
def maintaining(self):
if self.request is None:
return self.value
return self.value \
and not self.request.path.startswith("/maintenance/cannot/happen/here/")
class MyKey(keys.Key):
status_class = PriorityStatusClass
my_key = MyKey(
"my_maintenance_key",
label=_("My maintenance mode"),
help_text=_("This will be automatically triggered when importing/exporting."),
timeout=modes.DAY,
)
Then register it with wagtail hooks:
from wagtail import hooks
@hooks.register('maintenance.register_key')
def register_maintenance_mode_key():
return my_key
Return the status class for this key. This is used to check if the maintenance mode is active for the request.
Get the context for the maintenance mode template. This is used to render the maintenance mode page when the key is marked as active.
Return the template for the maintenance mode page.
Render the maintenance mode page. This is only called if this key is the first active key in the ordered list of keys. If there is another key active before this key, the other key will be used to render the maintenance mode page.
Return the json representation of this key.
This will be used to give extra information about the key to people who have permissions for it, on the maintenance.views.ping
view.
Return the order of this key as it should be checked for rendering.
Return if the maintenance mode is active for this key. Directly fetches from cache to refresh the status.
Label as it should be displayed in the Wagtail admin.
Help text to give more information about this key in the Wagtail admin.
Timeout for this key. This is used to automatically disable the maintenance mode after a certain time.
Are you running an ASGI application with channels and asigref installed? We provide a websocket consumer to notify the client of the maintenance mode in realtime.
This is the status code which is returned when the maintenance mode is active.
The default template used to render the maintenance mode page. Keys can individually override this.
To which menu do you want to register the maintenance mode menu item? Example:
MAINTENANCE_MENU_ITEM_HOOK_NAME = 'register_settings_menu_item'
The import path of the function which returns the maintenance websocket url.
By default hardcoded to return /ws/maintenance/ping/
.
A list of paths which should not be affected by the maintenance mode. We will check by converting the request.path to lower case and check if it starts with any path in the list. Keys can individually override this.