def _start_updating(self): # Start the ProxyUpdater self._proxy_task = PeriodicTask(self._update_period, self._try_update) register_shutdown_handler(self._proxy_task.stop) self._proxy_task.start() # Start the proxy metrics publisher task. self._proxy_metrics_task = PeriodicTask(self._update_period, self._publish_proxy_metrics) self._proxy_metrics_task.start() register_shutdown_handler(self._proxy_metrics_task.stop)
class ThreadFlushEngine(FlushEngine): """Class that uses a thread to periodically flush. """ def __init__(self, period, flush_fn): super(ThreadFlushEngine, self).__init__(period, flush_fn) self._thread = PeriodicTask(self._period, self._flush_fn) def start(self): """Override of base method. """ self._thread.start() def stop(self): """Override of base method. """ self._thread.stop()
def __init__(self, period, flush_fn): super(ThreadFlushEngine, self).__init__(period, flush_fn) self._thread = PeriodicTask(self._period, self._flush_fn)
class ProxyUpdater(object): def __init__(self, backend, config, update_period, max_update_frequency): self._backend = ProxyBackendProvider.get_backend(backend, config, self._on_update) self._update_period = int(update_period) self._max_update_frequency = int(max_update_frequency) self._needs_update = True self._last_updated = datetime(1970, 1, 1) self._updating = False def _on_update(self): self._needs_update = True def _publish_proxy_metrics(self): """Publishes proxy metrics if appropriate. """ if self._backend.metrics_publisher: self._backend.metrics_publisher.publish() def _should_update(self, as_of): # Needs an update, isn't already updating, and hasn't updated too recently time_since_last = as_of - self._last_updated return self._needs_update \ and not self._updating \ and not time_since_last < timedelta(seconds=self._max_update_frequency) @property def blueprints(self): """ Flask blueprints describing managed APIs. """ return self._backend.blueprints def set_up(self): self._start_discovery(weight_adjustment_start=None) self._update(restart_proxy=False) def start(self, weight_adjustment_delay_seconds): now = datetime.now() delay = timedelta(seconds=weight_adjustment_delay_seconds) weight_adjustment_start = now + delay self._start_discovery(weight_adjustment_start) self._start_updating() def _start_updating(self): # Start the ProxyUpdater self._proxy_task = PeriodicTask(self._update_period, self._try_update) register_shutdown_handler(self._proxy_task.stop) self._proxy_task.start() # Start the proxy metrics publisher task. self._proxy_metrics_task = PeriodicTask(self._update_period, self._publish_proxy_metrics) self._proxy_metrics_task.start() register_shutdown_handler(self._proxy_metrics_task.stop) def _start_discovery(self, weight_adjustment_start): self._backend.start_discovery(weight_adjustment_start) def _try_update(self, as_of=None): try: increment_counter(_METRIC_UPDATE_ATTEMPTED) METRIC_UPDATE_ATTEMPTED.inc() if not as_of: as_of = datetime.now() do_update = self._should_update(as_of) if do_update: self._needs_update = False self._updating= True try: self._update(restart_proxy=True) except Exception as e: increment_counter(_METRIC_UPDATE_ATTEMPT_FAILED) METRIC_UPDATE_ATTEMPT_FAILED.labels(type=e.args[0]).inc() logger.exception('Failed to update configuration.') finally: self._updating = False except Exception as e: increment_counter(_METRIC_UPDATE_ATTEMPT_FAILED) METRIC_UPDATE_ATTEMPT_FAILED.labels(type=e.args[0]).inc() logger.exception('Error updating.') def _update(self, restart_proxy=True): self._backend.update(restart_proxy) METRIC_UPDATE_ATTEMPT_SUCCEEDED.inc()
class ProxyUpdater(object): def __init__(self, backend, config, update_period, max_update_frequency): self._backend = ProxyBackendProvider.get_backend(backend, config, self._on_update) self._update_period = int(update_period) self._max_update_frequency = int(max_update_frequency) self._needs_update = True self._last_updated = datetime(1970, 1, 1) self._updating = False def _on_update(self): self._needs_update = True def _publish_proxy_metrics(self): """Publishes proxy metrics if appropriate. """ if self._backend.metrics_publisher: self._backend.metrics_publisher.publish() def _should_update(self, as_of): # Needs an update, isn't already updating, and hasn't updated too recently time_since_last = as_of - self._last_updated return self._needs_update \ and not self._updating \ and not time_since_last < timedelta(seconds=self._max_update_frequency) @property def blueprints(self): """ Flask blueprints describing managed APIs. """ return self._backend.blueprints def set_up(self): self._start_discovery(weight_adjustment_start=None) self._update(restart_proxy=False) def start(self, weight_adjustment_delay_seconds): now = datetime.now() delay = timedelta(seconds=weight_adjustment_delay_seconds) weight_adjustment_start = now + delay self._start_discovery(weight_adjustment_start) self._start_updating() def _start_updating(self): # Start the ProxyUpdater self._proxy_task = PeriodicTask(self._update_period, self._try_update) register_shutdown_handler(self._proxy_task.stop) self._proxy_task.start() # Start the proxy metrics publisher task. self._proxy_metrics_task = PeriodicTask(self._update_period, self._publish_proxy_metrics) self._proxy_metrics_task.start() register_shutdown_handler(self._proxy_metrics_task.stop) def _start_discovery(self, weight_adjustment_start): self._backend.start_discovery(weight_adjustment_start) def _try_update(self, as_of=None): try: increment_counter(_METRIC_UPDATE_ATTEMPTED) if not as_of: as_of = datetime.now() do_update = self._should_update(as_of) if do_update: self._needs_update = False self._updating= True try: self._update(restart_proxy=True) except Exception: logger.exception('Failed to update configuration.') finally: self._updating = False except Exception: increment_counter(_METRIC_UPDATE_ATTEMPT_FAILED) logger.exception('Error updating.') def _update(self, restart_proxy=True): self._backend.update(restart_proxy)