def initialize_client(self) -> None: """ Initializes client and starts communication with central unleash server(s). This kicks off: * Client registration * Provisioning poll * Stats poll :return: """ # Setup fl_args = { "url": self.unleash_url, "app_name": self.unleash_app_name, "instance_id": self.unleash_instance_id, "custom_headers": self.unleash_custom_headers, "custom_options": self.unleash_custom_options, "cache": self.cache, "features": self.features, "strategy_mapping": self.strategy_mapping, "project": self.unleash_project_name } metrics_args = { "url": self.unleash_url, "app_name": self.unleash_app_name, "instance_id": self.unleash_instance_id, "custom_headers": self.unleash_custom_headers, "custom_options": self.unleash_custom_options, "features": self.features, "ondisk_cache": self.cache } # Register app if not self.unleash_disable_registration: register_client(self.unleash_url, self.unleash_app_name, self.unleash_instance_id, self.unleash_metrics_interval, self.unleash_custom_headers, self.unleash_custom_options, self.strategy_mapping) fetch_and_load_features(**fl_args) # Start periodic jobs self.scheduler.start() self.fl_job = self.scheduler.add_job( fetch_and_load_features, trigger=IntervalTrigger( seconds=int(self.unleash_refresh_interval)), kwargs=fl_args) if not self.unleash_disable_metrics: self.metric_job = self.scheduler.add_job( aggregate_and_send_metrics, trigger=IntervalTrigger( seconds=int(self.unleash_metrics_interval)), kwargs=metrics_args) self.is_initialized = True
def initialize_client(self, disable_registration=False) -> None: """ Initializes client and starts communication with central unleash server(s). This kicks off: * Client registration * Provisioning poll * Stats poll :return: """ # Setup fl_args = { "url": self.unleash_url, "app_name": self.unleash_app_name, "instance_id": self.unleash_instance_id, "custom_headers": self.unleash_custom_headers, "custom_options": self.unleash_custom_options, "cache": self.cache, "features": self.features, "strategy_mapping": self.strategy_mapping } # Register app if not self.unleash_disable_registration and not disable_registration: register_client(self.unleash_url, self.unleash_app_name, self.unleash_instance_id, self.unleash_metrics_interval, self.unleash_custom_headers, self.unleash_custom_options, self.strategy_mapping) fetch_and_load_features(**fl_args) self.is_initialized = True
def test_register_client_failure(): responses.add(responses.POST, FULL_REGISTER_URL, json={}, status=500) result = register_client(URL, APP_NAME, INSTANCE_ID, METRICS_INTERVAL, CUSTOM_HEADERS, DEFAULT_STRATEGY_MAPPING) assert len(responses.calls) == 1 assert not result
def test_register_client_success(): responses.add(responses.POST, FULL_REGISTER_URL, json={}, status=202) result = register_client(URL, APP_NAME, INSTANCE_ID, METRICS_INTERVAL, CUSTOM_HEADERS) assert len(responses.calls) == 1 assert result
def test_register_client(payload, status, expected): responses.add(responses.POST, FULL_REGISTER_URL, status=status, **payload) result = register_client(URL, APP_NAME, INSTANCE_ID, METRICS_INTERVAL, CUSTOM_HEADERS, CUSTOM_OPTIONS, DEFAULT_STRATEGY_MAPPING) assert len(responses.calls) == 1 assert result is expected
def initialize_client(self) -> None: """ Initializes client and starts communication with central unleash server(s). This kicks off: * Client registration * Provisioning poll * Stats poll :return: """ # Setup fl_args = [self.unleash_url, self.unleash_app_name, self.unleash_instance_id, self.unleash_custom_headers, self.cache, self.features] metrics_args = [self.unleash_url, self.unleash_app_name, self.unleash_instance_id, self.unleash_custom_headers, self.features, self.metrics_last_sent_time] # Register app register_client(self.unleash_url, self.unleash_app_name, self.unleash_instance_id, self.unleash_metrics_interval, self.unleash_custom_headers) fetch_and_load_features(*fl_args) # Start periodic jobs self.scheduler.start() self.fl_job = self.scheduler.add_job(fetch_and_load_features, trigger=IntervalTrigger(seconds=int(self.unleash_refresh_interval)), args=fl_args) self.metric_job = self.scheduler.add_job(aggregate_and_send_metrics, trigger=IntervalTrigger(seconds=int(self.unleash_metrics_interval)), args=metrics_args) self.is_initialized = True
def test_register_client_exception(): responses.add(responses.POST, FULL_REGISTER_URL, body=ConnectionError("Test connection error."), status=200) result = register_client(URL, APP_NAME, INSTANCE_ID, METRICS_INTERVAL, CUSTOM_HEADERS, DEFAULT_STRATEGY_MAPPING) assert len(responses.calls) == 1 assert not result
def initialize_client(self, fetch_toggles: bool = True) -> None: """ Initializes client and starts communication with central unleash server(s). This kicks off: * Client registration * Provisioning poll * Stats poll If `fetch_toggles` is `False`, feature toggle polling will be turned off and instead the client will only load features from the cache. This is usually used to cater the multi-process setups, e.g. Django, Celery, etc. This will raise an exception on registration if the URL is invalid. It is done automatically if called inside a context manager as in: .. code-block:: python with UnleashClient( url="https://foo.bar", app_name="myClient1", instance_id="myinstanceid" ) as client: pass """ # Only perform initialization steps if client is not initialized. if not self.is_initialized: try: # Setup metrics_args = { "url": self.unleash_url, "app_name": self.unleash_app_name, "instance_id": self.unleash_instance_id, "custom_headers": self.unleash_custom_headers, "custom_options": self.unleash_custom_options, "features": self.features, "cache": self.cache } # Register app if not self.unleash_disable_registration: register_client(self.unleash_url, self.unleash_app_name, self.unleash_instance_id, self.unleash_metrics_interval, self.unleash_custom_headers, self.unleash_custom_options, self.strategy_mapping) if fetch_toggles: job_args = { "url": self.unleash_url, "app_name": self.unleash_app_name, "instance_id": self.unleash_instance_id, "custom_headers": self.unleash_custom_headers, "custom_options": self.unleash_custom_options, "cache": self.cache, "features": self.features, "strategy_mapping": self.strategy_mapping, "project": self.unleash_project_name, } job_func: Callable = fetch_and_load_features else: job_args = { "cache": self.cache, "feature_toggles": self.features, "strategy_mapping": self.strategy_mapping, } job_func = load_features job_func(**job_args) # type: ignore # Start periodic jobs self.scheduler.start() self.fl_job = self.scheduler.add_job( job_func, trigger=IntervalTrigger( seconds=int(self.unleash_refresh_interval), jitter=self.unleash_refresh_jitter, ), kwargs=job_args) if not self.unleash_disable_metrics: self.metric_job = self.scheduler.add_job( aggregate_and_send_metrics, trigger=IntervalTrigger( seconds=int(self.unleash_metrics_interval), jitter=self.unleash_metrics_jitter, ), kwargs=metrics_args) except Exception as excep: # Log exceptions during initialization. is_initialized will remain false. LOGGER.warning( "Exception during UnleashClient initialization: %s", excep) raise excep else: # Set is_iniialized to true if no exception is encountered. self.is_initialized = True else: warnings.warn( "Attempted to initialize an Unleash Client instance that has already been initialized." )