Example #1
0
    def toggle(self, key, user, default=False):

        default = self._config.get_default(key, default)

        if self._offline:
            return default

        def cb(feature):
            if feature is None:
                val = default
            else:
                val = _evaluate(feature, user)
                if val is None:
                    val = default
            self._send({"kind": "feature", "key": key, "user": user, "value": val})
            return val

        if self._config.stream and self._store.initialized:
            return cb(self._store.get(key))
        else:
            # noinspection PyBroadException
            try:
                return self._feature_requester.get(key, cb)
            except Exception:
                log.exception("Unhandled exception. Returning default value for flag.")
                return cb(None)
Example #2
0
 def do_toggle(should_retry):
     # noinspection PyBroadException,PyUnresolvedReferences
     try:
         val = self._toggle(key)
         return val
     except ProtocolError as e:
         inner = e.args[1]
         if inner.errno == errno.ECONNRESET and should_retry:
             log.warning('ProtocolError exception caught while getting flag. Retrying.')
             return do_toggle(False)
         else:
             log.exception('Unhandled exception. Returning default value for flag.')
             return None
     except Exception:
         log.exception('Unhandled exception. Returning default value for flag.')
         return None
Example #3
0
 def run(should_retry):
     # noinspection PyBroadException
     try:
         val = yield self._toggle(key)
         defer.returnValue(val)
     except ProtocolError as e:
         inner = e.args[1]
         if inner.errno == errno.ECONNRESET and should_retry:
             log.warning('ProtocolError exception caught while getting flag. Retrying.')
             d = yield run(False)
             defer.returnValue(d)
         else:
             log.exception('Unhandled exception. Returning default value for flag.')
             defer.returnValue(None)
     except Exception:
         log.exception('Unhandled exception. Returning default value for flag.')
         defer.returnValue(None)
Example #4
0
 def do_send(should_retry):
     # noinspection PyBroadException
     try:
         if isinstance(events, dict):
             body = [events]
         else:
             body = events
         hdrs = _headers(self._api_key)
         uri = self._config.base_uri + '/api/events/bulk'
         r = self._session.post(uri, headers=hdrs, timeout=(self._config.connect, self._config.read),
                                data=json.dumps(body))
         r.raise_for_status()
     except ProtocolError as e:
         inner = e.args[1]
         if inner.errno == errno.ECONNRESET and should_retry:
             log.warning('ProtocolError exception caught while sending events. Retrying.')
             do_send(False)
         else:
             log.exception('Unhandled exception in event consumer. Analytics events were not processed.')
     except:
         log.exception('Unhandled exception in event consumer. Analytics events were not processed.')
Example #5
0
    def run(self):
        if not self._running:
            log.info("Starting PollingUpdateProcessor with request interval: " + str(self._config.poll_interval))
            self._running = True
            while self._running:
                start_time = time.time()
                try:
                    all_data = self._requester.get_all_data()
                    self._store.init(all_data)
                    if not self._ready.is_set() is True and self._store.initialized is True:
                        log.info("PollingUpdateProcessor initialized ok")
                        self._ready.set()
                except UnsuccessfulResponseException as e:
                    log.error(http_error_message(e.status, "polling request"))
                    if not is_http_error_recoverable(e.status):
                        self._ready.set() # if client is initializing, make it stop waiting; has no effect if already inited
                        self.stop()
                except Exception as e:
                    log.exception(
                        'Error: Exception encountered when updating flags. %s' % e)

                elapsed = time.time() - start_time
                if elapsed < self._config.poll_interval:
                    time.sleep(self._config.poll_interval - elapsed)