feat(devops): Provide user with option to disable downloading feature flags

ref: #675 #575
This commit is contained in:
2025-03-14 23:04:57 +09:30
parent b22f4e2ea9
commit 14c193d72a
6 changed files with 32 additions and 5 deletions

View File

@ -11,6 +11,7 @@ feature_flag = {
'url': 'https://127.0.0.1:8002/api/v2/public/1/flags/2844', # URL to your Centurion ERP instance
'user_agent': 'My Django Application Name', # The name of your Django Application
'cache_dir': str(BASE_DIR) + '/', # Directory name (with trailing slash `/`) where the cached flags will be stored
'disable_downloading': False # Prevent downloading feature flags
'unique_id': 'unique ID for application', # Unique ID for this instance of your Django application
'version': '1.0.0', # The Version of Your Django Application
} # Note: All key values are strings

View File

@ -42,6 +42,7 @@ class CenturionFeatureFlagging:
user_agent (str): User Agent to report to Centurion Instance this
should be the name of your application
cache_dir (str): Directory where the feature flag cache file is saved.
disable_downloading (bool): Prevent the downloaing of feature flags
unique_id (str, optional): Unique ID of the application that is
reporting to Centurion ERP
version (str, optional): The version of your application
@ -58,32 +59,42 @@ class CenturionFeatureFlagging:
"""Date the feature flag file was last saved"""
_cache_dir: str = None
"""Directory name (with trailing slash `/`) where the feature flags will be saved/cached."""
_disable_downloading: bool = False
"""Prevent check-in and subsequent downloading from remote Centurion instance"""
_feature_flags: list = None
_feature_flag_filename: str = 'feature_flags.json'
""" File name for the cached feture flags"""
_headers: dict = {
"Accept": "application/json",
}
_last_modified: datetime = None
""" Last modified date/time of the feature flags"""
_response: requests.Response = None
"""Cached response from fetched feature flags"""
_ssl_verify: bool = True
"""Verify the SSL certificate of the remote Centurion ERP instance"""
_url: str = None
""" url of the centurion ERP instance"""
def __init__(
self,
url,
user_agent,
url: str,
user_agent: str,
cache_dir: str,
unique_id = None,
version = None,
disable_downloading: bool = False,
unique_id: str = None,
version: str = None,
):
if not str(cache_dir).endswith('/'):
@ -95,6 +106,8 @@ class CenturionFeatureFlagging:
self._cache_dir = cache_dir
self._disable_downloading = disable_downloading
if version is not None:
@ -207,7 +220,10 @@ class CenturionFeatureFlagging:
if feature_flag_file.is_file():
if feature_flag_file.lstat().st_mtime > datetime.now().timestamp() - (4 * 3580): # -20 second buffer
if(
feature_flag_file.lstat().st_mtime > datetime.now().timestamp() - (4 * 3580) # -20 second buffer
or self._disable_downloading
):
# Only open file if less than 4 hours old
with open(feature_flag_path, 'r') as saved_feature_flags:
@ -221,6 +237,10 @@ class CenturionFeatureFlagging:
response = None
if self._disable_downloading: # User has disabled downloading.
url = None
while(url is not None):
try:

View File

@ -24,6 +24,7 @@ class Command(BaseCommand):
url = settings.feature_flag['url'],
user_agent = settings.feature_flag['user_agent'],
cache_dir =settings.feature_flag['cache_dir'],
disable_downloading = settings.feature_flag.get('disable_downloading', False),
unique_id = settings.feature_flag.get('unique_id', None),
version = settings.feature_flag.get('version', None),
)

View File

@ -18,6 +18,7 @@ class FeatureFlagMiddleware:
url = settings.feature_flag['url'],
user_agent = settings.feature_flag['user_agent'],
cache_dir =settings.feature_flag['cache_dir'],
disable_downloading = settings.feature_flag.get('disable_downloading', False),
unique_id = settings.feature_flag.get('unique_id', None),
version = settings.feature_flag.get('version', None),
)

View File

@ -21,6 +21,7 @@ if getattr(settings,'feature_flag', None):
url = settings.feature_flag['url'],
user_agent = settings.feature_flag['user_agent'],
cache_dir =settings.feature_flag['cache_dir'],
disable_downloading = settings.feature_flag.get('disable_downloading', False),
unique_id = settings.feature_flag.get('unique_id', None),
version = settings.feature_flag.get('version', None),
)

View File

@ -52,6 +52,7 @@ class APIRootView(
url = settings.feature_flag['url'],
user_agent = settings.feature_flag['user_agent'],
cache_dir =settings.feature_flag['cache_dir'],
disable_downloading = settings.feature_flag.get('disable_downloading', False),
unique_id = settings.feature_flag.get('unique_id', None),
version = settings.feature_flag.get('version', None),
)
@ -73,6 +74,7 @@ class SimpleRouter(
url = settings.feature_flag['url'],
user_agent = settings.feature_flag['user_agent'],
cache_dir =settings.feature_flag['cache_dir'],
disable_downloading = settings.feature_flag.get('disable_downloading', False),
unique_id = settings.feature_flag.get('unique_id', None),
version = settings.feature_flag.get('version', None),
)
@ -98,6 +100,7 @@ class DefaultRouter(
url = settings.feature_flag['url'],
user_agent = settings.feature_flag['user_agent'],
cache_dir =settings.feature_flag['cache_dir'],
disable_downloading = settings.feature_flag.get('disable_downloading', False),
unique_id = settings.feature_flag.get('unique_id', None),
version = settings.feature_flag.get('version', None),
)