mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Ensure bootstrapping of config on start
This commit is contained in:
@ -560,6 +560,13 @@ class ConfigRevision(models.Model):
|
||||
return self.data[item]
|
||||
return super().__getattribute__(item)
|
||||
|
||||
def cache(self):
|
||||
"""
|
||||
Cache the configuration data.
|
||||
"""
|
||||
cache.set('config', self.data, None)
|
||||
cache.set('config_version', self.pk, None)
|
||||
|
||||
@admin.display(boolean=True)
|
||||
def is_active(self):
|
||||
return cache.get('config_version') == self.pk
|
||||
|
@ -2,7 +2,6 @@ import logging
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.core.cache import cache
|
||||
from django.db.models.signals import m2m_changed, post_save, pre_delete
|
||||
from django.dispatch import receiver, Signal
|
||||
from django_prometheus.models import model_deletes, model_inserts, model_updates
|
||||
@ -173,5 +172,4 @@ def update_config(sender, instance, **kwargs):
|
||||
"""
|
||||
Update the cached NetBox configuration when a new ConfigRevision is created.
|
||||
"""
|
||||
cache.set('config', instance.data, None)
|
||||
cache.set('config_version', instance.pk, None)
|
||||
instance.cache()
|
||||
|
@ -43,8 +43,9 @@ class Config:
|
||||
must be re-instantiated each time it's necessary to check for updates to the cached config.
|
||||
"""
|
||||
def __init__(self):
|
||||
self.config = cache.get('config') or {}
|
||||
self.version = cache.get('config_version')
|
||||
self._populate_from_cache()
|
||||
if not self.config or not self.version:
|
||||
self._populate_from_db()
|
||||
self.defaults = {param.name: param.default for param in PARAMS}
|
||||
logger.debug("Loaded configuration data from cache")
|
||||
|
||||
@ -64,6 +65,19 @@ class Config:
|
||||
|
||||
raise AttributeError(f"Invalid configuration parameter: {item}")
|
||||
|
||||
def _populate_from_cache(self):
|
||||
"""Populate config data from Redis cache"""
|
||||
self.config = cache.get('config') or {}
|
||||
self.version = cache.get('config_version')
|
||||
|
||||
def _populate_from_db(self):
|
||||
"""Cache data from latest ConfigRevision, then populate from cache"""
|
||||
from extras.models import ConfigRevision
|
||||
revision = ConfigRevision.objects.last()
|
||||
revision.cache()
|
||||
logger.debug("Filled cache with data from latest ConfigRevision")
|
||||
self._populate_from_cache()
|
||||
|
||||
|
||||
class ConfigItem:
|
||||
"""
|
||||
|
Reference in New Issue
Block a user