private void OnError(object sender, EventSource.ExceptionEventArgs e)
        {
            var ex = e.Exception;

            LogHelpers.LogException(_log, "Encountered EventSource error", ex);
            var recoverable = true;

            if (ex is EventSource.EventSourceServiceUnsuccessfulResponseException respEx)
            {
                int status = respEx.StatusCode;
                _log.Error(HttpErrors.ErrorMessage(status, "streaming connection", "will retry"));
                RecordStreamInit(true);
                if (!HttpErrors.IsRecoverable(status))
                {
                    recoverable = false;
                    _initTask.TrySetException(ex); // sends this exception to the client if we haven't already started up
                    ((IDisposable)this).Dispose();
                }
            }

            var errorInfo = ex is EventSource.EventSourceServiceUnsuccessfulResponseException re ?
                            DataSourceStatus.ErrorInfo.FromHttpError(re.StatusCode) :
                            DataSourceStatus.ErrorInfo.FromException(ex);

            _dataSourceUpdates.UpdateStatus(recoverable ? DataSourceState.Interrupted : DataSourceState.Off,
                                            errorInfo);
        }
Ejemplo n.º 2
0
        private async Task UpdateTaskAsync()
        {
            try
            {
                var response = await _featureFlagRequestor.FeatureFlagsAsync();

                if (response.statusCode == 200)
                {
                    var flagsAsJsonString = response.jsonResponse;
                    var allData           = DataModelSerialization.DeserializeV1Schema(flagsAsJsonString);
                    _updateSink.Init(_user, allData);

                    if (_initialized.GetAndSet(true) == false)
                    {
                        _startTask.SetResult(true);
                        _log.Info("Initialized LaunchDarkly Polling Processor.");
                    }
                }
            }
            catch (UnsuccessfulResponseException ex)
            {
                var errorInfo = DataSourceStatus.ErrorInfo.FromHttpError(ex.StatusCode);

                if (HttpErrors.IsRecoverable(ex.StatusCode))
                {
                    _log.Warn(HttpErrors.ErrorMessage(ex.StatusCode, "polling request", "will retry"));
                    _updateSink.UpdateStatus(DataSourceState.Interrupted, errorInfo);
                }
                else
                {
                    _log.Error(HttpErrors.ErrorMessage(ex.StatusCode, "polling request", ""));
                    _updateSink.UpdateStatus(DataSourceState.Shutdown, errorInfo);

                    // if client is initializing, make it stop waiting
                    _startTask.TrySetResult(false);

                    ((IDisposable)this).Dispose();
                }
            }
            catch (JsonReadException ex)
            {
                _log.Error("Polling request received malformed data: {0}", LogValues.ExceptionSummary(ex));
                _updateSink.UpdateStatus(DataSourceState.Interrupted,
                                         new DataSourceStatus.ErrorInfo
                {
                    Kind = DataSourceStatus.ErrorKind.InvalidData,
                    Time = DateTime.Now
                });
            }
            catch (Exception ex)
            {
                Exception realEx = (ex is AggregateException ae) ? ae.Flatten() : ex;
                _log.Warn("Polling for feature flag updates failed: {0}", LogValues.ExceptionSummary(realEx));
                _log.Debug(LogValues.ExceptionTrace(realEx));
                _updateSink.UpdateStatus(DataSourceState.Interrupted,
                                         DataSourceStatus.ErrorInfo.FromException(realEx));
            }
        }
Ejemplo n.º 3
0
        private void OnError(object sender, EventSource.ExceptionEventArgs e)
        {
            var ex          = e.Exception;
            var recoverable = true;

            DataSourceStatus.ErrorInfo errorInfo;

            RecordStreamInit(true);

            if (ex is EventSourceServiceUnsuccessfulResponseException respEx)
            {
                int status = respEx.StatusCode;
                errorInfo = DataSourceStatus.ErrorInfo.FromHttpError(status);
                if (!HttpErrors.IsRecoverable(status))
                {
                    recoverable = false;
                    _log.Error(HttpErrors.ErrorMessage(status, "streaming connection", ""));
                }
                else
                {
                    _log.Warn(HttpErrors.ErrorMessage(status, "streaming connection", "will retry"));
                }
            }
            else
            {
                errorInfo = DataSourceStatus.ErrorInfo.FromException(ex);
                _log.Warn("Encountered EventSource error: {0}", LogValues.ExceptionSummary(ex));
                _log.Debug(LogValues.ExceptionTrace(ex));
            }

            _updateSink.UpdateStatus(recoverable ? DataSourceState.Interrupted : DataSourceState.Shutdown,
                                     errorInfo);

            if (!recoverable)
            {
                // Make _initTask complete to tell the client to stop waiting for initialization. We use
                // TrySetResult rather than SetResult here because it might have already been completed
                // (if for instance the stream started successfully, then restarted and got a 401).
                _initTask.TrySetResult(false);
                ((IDisposable)this).Dispose();
            }
        }
Ejemplo n.º 4
0
        private async Task UpdateTaskAsync()
        {
            _log.Info("Polling LaunchDarkly for feature flag updates");
            try
            {
                var allData = await _featureRequestor.GetAllDataAsync();

                if (allData is null)
                {
                    // This means it was cached, and alreadyInited was true
                    _dataSourceUpdates.UpdateStatus(DataSourceState.Valid, null);
                }
                else
                {
                    if (_dataSourceUpdates.Init(allData.Value))
                    {
                        _dataSourceUpdates.UpdateStatus(DataSourceState.Valid, null);

                        if (!_initialized.GetAndSet(true))
                        {
                            _initTask.SetResult(true);
                            _log.Info("First polling request successful");
                        }
                    }
                }
            }
            catch (UnsuccessfulResponseException ex)
            {
                _log.Error(HttpErrors.ErrorMessage(ex.StatusCode, "polling request", "will retry"));
                var errorInfo = DataSourceStatus.ErrorInfo.FromHttpError(ex.StatusCode);
                if (HttpErrors.IsRecoverable(ex.StatusCode))
                {
                    _dataSourceUpdates.UpdateStatus(DataSourceState.Interrupted, errorInfo);
                }
                else
                {
                    _dataSourceUpdates.UpdateStatus(DataSourceState.Off, errorInfo);
                    try
                    {
                        // if client is initializing, make it stop waiting
                        _initTask.SetResult(true);
                    }
                    catch (InvalidOperationException)
                    {
                        // the task was already set - nothing more to do
                    }
                    ((IDisposable)this).Dispose();
                }
            }
            catch (JsonReadException ex)
            {
                _log.Error("Polling request received malformed data: {0}", LogValues.ExceptionSummary(ex));
                _dataSourceUpdates.UpdateStatus(DataSourceState.Interrupted,
                                                new DataSourceStatus.ErrorInfo
                {
                    Kind = DataSourceStatus.ErrorKind.InvalidData,
                    Time = DateTime.Now
                });
            }
            catch (Exception ex)
            {
                Exception realEx = (ex is AggregateException ae) ? ae.Flatten() : ex;
                LogHelpers.LogException(_log, "Polling for feature flag updates failed", realEx);

                _dataSourceUpdates.UpdateStatus(DataSourceState.Interrupted,
                                                DataSourceStatus.ErrorInfo.FromException(realEx));
            }
        }
        public async Task <EventSenderResult> SendEventDataAsync(EventDataKind kind, string data, int eventCount)
        {
            Uri    uri;
            string description;
            string payloadId;

            if (kind == EventDataKind.DiagnosticEvent)
            {
                uri         = _diagnosticUri;
                description = "diagnostic event";
                payloadId   = null;
            }
            else
            {
                uri         = _eventsUri;
                description = string.Format("{0} event(s)", eventCount);
                payloadId   = Guid.NewGuid().ToString();
            }

            _logger.Debug("Submitting {0} to {1} with json: {2}", description, uri.AbsoluteUri, data);

            for (var attempt = 0; attempt < MaxAttempts; attempt++)
            {
                if (attempt > 0)
                {
                    await Task.Delay(_retryInterval);
                }

                using (var cts = new CancellationTokenSource(_timeout))
                {
                    string errorMessage = null;
                    bool   canRetry     = false;
                    bool   mustShutDown = false;

                    try
                    {
                        using (var request = PrepareRequest(uri, payloadId))
                            using (var stringContent = new StringContent(data, Encoding.UTF8, "application/json"))
                            {
                                request.Content = stringContent;
                                Stopwatch timer = new Stopwatch();
                                using (var response = await _httpClient.SendAsync(request, cts.Token))
                                {
                                    timer.Stop();
                                    _logger.Debug("Event delivery took {0} ms, response status {1}",
                                                  timer.ElapsedMilliseconds, (int)response.StatusCode);
                                    if (response.IsSuccessStatusCode)
                                    {
                                        DateTimeOffset?respDate = response.Headers.Date;
                                        return(new EventSenderResult(DeliveryStatus.Succeeded,
                                                                     respDate.HasValue ? (DateTime?)respDate.Value.DateTime : null));
                                    }
                                    else
                                    {
                                        errorMessage = HttpErrors.ErrorMessageBase((int)response.StatusCode);
                                        canRetry     = HttpErrors.IsRecoverable((int)response.StatusCode);
                                        mustShutDown = !canRetry;
                                    }
                                }
                            }
                    }
                    catch (TaskCanceledException e)
                    {
                        if (e.CancellationToken == cts.Token)
                        {
                            // Indicates the task was cancelled deliberately somehow; in this case don't retry
                            _logger.Warn("Event sending task was cancelled");
                            return(new EventSenderResult(DeliveryStatus.Failed, null));
                        }
                        else
                        {
                            // Otherwise this was a request timeout.
                            errorMessage = "Timed out";
                            canRetry     = true;
                        }
                    }
                    catch (Exception e)
                    {
                        errorMessage = string.Format("Error ({0})", LogValues.ExceptionSummary(e));
                        canRetry     = true;
                    }
                    string nextStepDesc = canRetry ?
                                          (attempt == MaxAttempts - 1 ? "will not retry" : "will retry after one second") :
                                          "giving up permanently";
                    _logger.Warn(errorMessage + " sending {0}; {1}", description, nextStepDesc);
                    if (mustShutDown)
                    {
                        return(new EventSenderResult(DeliveryStatus.FailedAndMustShutDown, null));
                    }
                    if (!canRetry)
                    {
                        break;
                    }
                }
            }
            return(new EventSenderResult(DeliveryStatus.Failed, null));
        }