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)
        def do_send(should_retry):
            # noinspection PyBroadException
            try:
                if isinstance(events, dict):
                    body = [events]
                else:
                    body = events

                json_body = jsonpickle.encode(body, unpicklable=False)
                log.debug('Sending events payload: ' + json_body)
                hdrs = _headers(self.sdk_key)
                uri = self._config.events_uri
                r = self._session.post(uri,
                                       headers=hdrs,
                                       timeout=(self._config.connect_timeout,
                                                self._config.read_timeout),
                                       data=json_body)
                r.raise_for_status()
            except ProtocolError as e:
                if e.args is not None and len(
                        e.args) > 1 and e.args[1] is not None:
                    inner = e.args[1]
                    if inner.errno is not None and 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 #3
0
 def do_send(should_retry):
     # noinspection PyBroadException
     try:
         if isinstance(events, dict):
             body = [events]
         else:
             body = events
         hdrs = _headers(self._sdk_key)
         r = yield self._session.post(
             self._config.events_uri,
             headers=hdrs,
             timeout=(self._config.connect_timeout,
                      self._config.read_timeout),
             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.'
             )
             yield 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 #4
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 #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()
                    break
                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)
Example #6
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 HTTPError as e:
                    log.error('Received unexpected status code %d from polling request' % e.response.status_code)
                    if e.response.status_code == 401:
                        log.error('Received 401 error, no further polling requests will be made since SDK key is invalid')
                        self.stop()
                    break
                except:
                    log.exception(
                        'Error: Exception encountered when updating flags.')

                elapsed = time.time() - start_time
                if elapsed < self._config.poll_interval:
                    time.sleep(self._config.poll_interval - elapsed)
 def run(self):
     log.info("Starting event consumer")
     self._running = True
     while self._running:
         try:
             self.send()
         except Exception:
             log.exception('Unhandled exception in event consumer')
Example #8
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 #9
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 #10
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 #11
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)