Skip to content

Commit

Permalink
revise
Browse files Browse the repository at this point in the history
  • Loading branch information
oakbani committed Sep 26, 2019
1 parent 4bb2cfb commit c9e5ef4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
24 changes: 17 additions & 7 deletions optimizely/config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ def __init__(self,
notification_center=notification_center)
self._config = None
self.validate_schema = not skip_json_validation
self.blocking_timeout = None
self._event = threading.Event()
self._configReadyEvent = threading.Event()
self._set_config(datafile)

def _set_config(self, datafile):
Expand Down Expand Up @@ -134,8 +133,9 @@ def _set_config(self, datafile):

if previous_revision == config.get_revision():
return

self._config = config
self._event.set()
self._configReadyEvent.set()
self.notification_center.send_notifications(enums.NotificationTypes.OPTIMIZELY_CONFIG_UPDATE)
self.logger.debug(
'Received new datafile and updated config. '
Expand All @@ -148,8 +148,7 @@ def get_config(self):
Returns:
ProjectConfig. None if not set.
"""
if self.blocking_timeout is not None:
self._event.wait(self.blocking_timeout)

return self._config


Expand All @@ -174,7 +173,7 @@ def __init__(self,
datafile: Optional JSON string representing the project.
update_interval: Optional floating point number representing time interval in seconds
at which to request datafile and set ProjectConfig.
blocking_timeout: Optional Time in seconds to block the config call until config object
blocking_timeout: Optional Time in seconds to block the get_config call until config object
has been initialized.
url: Optional string representing URL from where to fetch the datafile. If set it supersedes the sdk_key.
url_template: Optional string template which in conjunction with sdk_key
Expand Down Expand Up @@ -233,6 +232,17 @@ def get_datafile_url(sdk_key, url, url_template):

return url

def get_config(self):
""" Returns instance of ProjectConfig. Returns immediately if project config is ready otherwise
blocks maximum for value of blocking_timeout in seconds.
Returns:
ProjectConfig. None if not set.
"""

self._configReadyEvent.wait(self.blocking_timeout)
return self._config

def set_update_interval(self, update_interval):
""" Helper method to set frequency at which datafile has to be polled and ProjectConfig updated.
Expand Down Expand Up @@ -268,7 +278,7 @@ def set_blocking_timeout(self, blocking_timeout):
blocking_timeout = enums.ConfigManager.DEFAULT_BLOCKING_TIMEOUT
self.logger.debug('Set config blocking timeout to default value {}.'.format(blocking_timeout))

if not isinstance(blocking_timeout, (numbers.Integral, int)):
if not isinstance(blocking_timeout, (numbers.Integral, float)):
raise optimizely_exceptions.InvalidInputException(
'Invalid blocking timeout "{}" provided.'.format(blocking_timeout)
)
Expand Down
2 changes: 1 addition & 1 deletion optimizely/helpers/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class AudienceEvaluationLogs(object):

class ConfigManager(object):
DATAFILE_URL_TEMPLATE = 'https://cdn.optimizely.com/datafiles/{sdk_key}.json'
# Default time in seconds to block the 'config' method call until 'config' instance has been initialized.
# Default time in seconds to block the 'get_config' method call until 'config' instance has been initialized.
DEFAULT_BLOCKING_TIMEOUT = 15
# Default config update interval of 5 minutes
DEFAULT_UPDATE_INTERVAL = 5 * 60
Expand Down

0 comments on commit c9e5ef4

Please sign in to comment.