Example #1
0
 private void RaiseSendFailedEvent(EventBatch batch, Exception ex)
 {
     if (batch != null)
     {
         if (ex != null)
         {
             Trace.TraceError("Unable to upload batch: " + ex.ToString());
         }
         if (PackageSendFailed != null)
         {
             PackageSendFailed(this, new PackageEventArgs {
                 PackageId = batch.Id, Records = batch.JsonEvents, Exception = ex
             });
         }
     }
 }
Example #2
0
 private void RaiseSentEvent(EventBatch batch)
 {
     if (batch != null)
     {
         if (batch.JsonEvents != null)
         {
             Trace.TraceInformation("Successfully uploaded batch with {0} events.", batch.JsonEvents.Count);
         }
         if (PackageSent != null)
         {
             PackageSent(this, new PackageEventArgs {
                 PackageId = batch.Id, Records = batch.JsonEvents
             });
         }
     }
 }
Example #3
0
        private static string BuildJsonMessage(EventBatch batch, int?experimentalUnitDuration)
        {
            StringBuilder jsonBuilder = new StringBuilder();

            jsonBuilder.Append("{\"i\":\"" + batch.Id.ToString() + "\",");

            jsonBuilder.Append("\"j\":[");
            jsonBuilder.Append(String.Join(",", batch.JsonEvents));
            jsonBuilder.Append("]");

            if (experimentalUnitDuration.HasValue)
            {
                jsonBuilder.Append(",\"d\":");
                jsonBuilder.Append(experimentalUnitDuration.Value);
            }

            jsonBuilder.Append("}");

            return(jsonBuilder.ToString());
        }
Example #4
0
        /// <summary>
        /// Triggered when a batch of events is ready for upload.
        /// </summary>
        /// <param name="transformedEvents">The list of JSON-serialized event strings.</param>
        /// <returns>Task</returns>
        public override async Task UploadTransformedEvents(IList <string> transformedEvents)
        {
            EventBatch batch = new EventBatch
            {
                Id         = Guid.NewGuid(),
                JsonEvents = transformedEvents
            };

            string json = EventUploader.BuildJsonMessage(batch, this.experimentalUnitDuration);

            IHttpResponse response = null;

            if (batchConfig.UploadRetryPolicy == BatchUploadRetryPolicy.ExponentialRetry)
            {
                var retryStrategy = new ExponentialBackoff(Constants.RetryCount,
                                                           Constants.RetryMinBackoff, Constants.RetryMaxBackoff, Constants.RetryDeltaBackoff);

                RetryPolicy retryPolicy = new RetryPolicy <JoinServiceTransientErrorDetectionStrategy>(retryStrategy);

                try
                {
                    response = await retryPolicy.ExecuteAsync(async() =>
                    {
                        IHttpResponse currentResponse = null;
                        try
                        {
                            currentResponse = await httpClient.PostAsync(ServiceConstants.JoinPostAddress, json);
                        }
                        catch (TaskCanceledException e) // HttpClient throws this on timeout
                        {
                            // Convert to a different exception otherwise ExecuteAsync will see cancellation
                            throw new HttpRequestException("Request timed out", e);
                        }
                        return(currentResponse);
                    });
                }
                catch (Exception ex)
                {
                    this.RaiseSendFailedEvent(batch, ex);
                    return;
                }
            }
            else
            {
                response = await httpClient.PostAsync(ServiceConstants.JoinPostAddress, json);
            }

            if (response == null)
            {
                this.RaiseSendFailedEvent(batch, new HttpRequestException("No response received from the server."));
            }
            else if (!response.IsSuccessStatusCode)
            {
                string reason = await response.GetContentAsync();

                this.RaiseSendFailedEvent(batch, new HttpRequestException(reason));
            }
            else
            {
                this.RaiseSentEvent(batch);
            }
        }