Skip to content

Commit df7a7cf

Browse files
Merge pull request #43 from hellohaptik/develop
Develop to Master
2 parents 1539b69 + 8ef4097 commit df7a7cf

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

FeatureToggle/__init__.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Python Imports
22
import redis
3+
from redis.exceptions import LockError, BusyLoadingError, ConnectionError, RedisError
34
import pickle
45
from typing import Dict, Any, Optional
56

@@ -88,10 +89,14 @@ def update_cache(data: Dict[str, Any]) -> None:
8889
FeatureToggles.__cache.set(
8990
consts.FEATURES_URL, pickle.dumps(data)
9091
)
92+
except (LockError, BusyLoadingError, ConnectionError, RedisError) as redis_err:
93+
error_msg = f'Redis Exception occurred while updating the redis cache: {str(redis_err)}'
94+
LOGGER.info(error_msg)
95+
raise Exception(error_msg)
9196
except Exception as err:
92-
raise Exception(
93-
f'Exception occurred while updating the redis cache: {str(err)}'
94-
)
97+
error_msg = f'Unknown Exception occurred while updating the redis cache: {str(err)}'
98+
LOGGER.info(error_msg)
99+
raise Exception(error_msg)
95100
LOGGER.info(f'[Feature Toggles] Cache Updated')
96101

97102
@staticmethod
@@ -148,7 +153,8 @@ def is_enabled_for_domain(feature_name: str,
148153
(bool): True if Feature is enabled else False
149154
"""
150155
feature_toggles = FeatureToggles.fetch_feature_toggles()
151-
LOGGER.info(f"Enable_for_domain_FT_cache_info: {FeatureToggles.fetch_feature_toggles.__wrapped__.cache_info()}")
156+
LOGGER.info(f"Enable_for_domain_FT_cache_info: "
157+
f"{FeatureToggles.fetch_feature_toggles.__wrapped__.cache_info()}")
152158
return domain_name in feature_toggles.get(feature_name, {}).get('domain_names', [])
153159

154160
@staticmethod
@@ -163,7 +169,8 @@ def is_enabled_for_partner(feature_name: str,
163169
(bool): True if Feature is enabled else False
164170
"""
165171
feature_toggles = FeatureToggles.fetch_feature_toggles()
166-
LOGGER.info(f"Enable_for_partner_FT_cache_info: {FeatureToggles.fetch_feature_toggles.__wrapped__.cache_info()}")
172+
LOGGER.info(f"Enable_for_partner_FT_cache_info: "
173+
f"{FeatureToggles.fetch_feature_toggles.__wrapped__.cache_info()}")
167174
return partner_name in feature_toggles.get(feature_name, {}).get('partner_names', [])
168175

169176
@staticmethod
@@ -178,7 +185,8 @@ def is_enabled_for_business(feature_name: str,
178185
(bool): True if Feature is enabled else False
179186
"""
180187
feature_toggles = FeatureToggles.fetch_feature_toggles()
181-
LOGGER.info(f"Enable_for_business_FT_cache_info: {FeatureToggles.fetch_feature_toggles.__wrapped__.cache_info()}")
188+
LOGGER.info(f"Enable_for_business_FT_cache_info: "
189+
f"{FeatureToggles.fetch_feature_toggles.__wrapped__.cache_info()}")
182190
return business_via_name in feature_toggles.get(feature_name, {}).get('business_via_names', [])
183191

184192
@staticmethod
@@ -193,7 +201,8 @@ def is_enabled_for_expert(feature_name: str,
193201
(bool): True if Feature is enabled else False
194202
"""
195203
feature_toggles = FeatureToggles.fetch_feature_toggles()
196-
LOGGER.info(f"Enable_for_expert_FT_cache_info: {FeatureToggles.fetch_feature_toggles.__wrapped__.cache_info()}")
204+
LOGGER.info(f"Enable_for_expert_FT_cache_info: "
205+
f"{FeatureToggles.fetch_feature_toggles.__wrapped__.cache_info()}")
197206
return expert_email in feature_toggles.get(feature_name, {}).get('expert_emails', [])
198207

199208
@staticmethod
@@ -208,7 +217,8 @@ def is_enabled_for_team(feature_name: str,
208217
(bool): True if feature is enabled else False
209218
"""
210219
feature_toggles = FeatureToggles.fetch_feature_toggles()
211-
LOGGER.info(f"Enable_for_team_FT_cache_info: {FeatureToggles.fetch_feature_toggles.__wrapped__.cache_info()}")
220+
LOGGER.info(f"Enable_for_team_FT_cache_info: "
221+
f"{FeatureToggles.fetch_feature_toggles.__wrapped__.cache_info()}")
212222
return team_id in feature_toggles.get(feature_name, {}).get('team_ids', [])
213223

214224
@staticmethod
@@ -226,19 +236,18 @@ def fetch_feature_toggles():
226236
}
227237
"""
228238
# TODO: Remove the cas and environment name from the feature toggles while returning the response
239+
response = {}
229240
LOGGER.info(f'Loading Feature Toggles from Redis')
230-
LOGGER.info(f"Efetch_feature_toggles_cache_info: {FeatureToggles.fetch_feature_toggles.__wrapped__.cache_info()}")
241+
LOGGER.info(f"Fetch_feature_toggles_cache_info:"
242+
f"{FeatureToggles.fetch_feature_toggles.__wrapped__.cache_info()}")
231243
if FeatureToggles.__cache is None:
232-
raise Exception(
233-
'To update cache Feature Toggles class needs to be initialised'
234-
)
235-
236-
feature_toggles = pickle.loads(
237-
FeatureToggles.__cache.get(consts.FEATURES_URL)
238-
)
239-
response = {}
244+
LOGGER.error('To update cache Feature Toggles class needs to be initialised')
245+
return response
240246

241247
try:
248+
feature_toggles = pickle.loads(
249+
FeatureToggles.__cache.get(consts.FEATURES_URL)
250+
)
242251
if feature_toggles:
243252
for feature_toggle in feature_toggles:
244253
full_feature_name = feature_toggle['name']
@@ -286,5 +295,5 @@ def fetch_feature_toggles():
286295
response[full_feature_name]['team_ids'] = team_ids
287296
except Exception as err:
288297
# Handle this exception from where this util gets called
289-
raise Exception(f'An error occurred while parsing the response: {str(err)}')
298+
LOGGER.error(f'An error occurred while parsing the response: {str(err)}')
290299
return response

0 commit comments

Comments
 (0)