private void DoWork() { while (!ShouldStop) { try { _logger.InfoFormat("Mobile Analytics Manager is trying to deliver events in background thread."); IDictionary <string, MobileAnalyticsManager> instanceDictionary = MobileAnalyticsManager.CopyOfInstanceDictionary; foreach (string appId in instanceDictionary.Keys) { MobileAnalyticsManager manager = null; try { manager = MobileAnalyticsManager.GetInstance(appId); manager.BackgroundDeliveryClient.AttemptDelivery(); } catch (System.Exception e) { _logger.Error(e, "An exception occurred in Mobile Analytics Delivery Client."); if (null != manager) { MobileAnalyticsErrorEventArgs eventArgs = new MobileAnalyticsErrorEventArgs(this.GetType().Name, "An exception occurred when deliverying events to Amazon Mobile Analytics.", e, new List <Amazon.MobileAnalytics.Model.Event>()); manager.OnRaiseErrorEvent(eventArgs); } } } Thread.Sleep(BackgroundSubmissionWaitTime * 1000); } catch (System.Exception e) { _logger.Error(e, "An exception occurred in Mobile Analytics Manager."); } } }
/// <summary> /// Enqueues the events for delivery. The event is stored in an instance of <see cref="Amazon.MobileAnalytics.MobileAnalyticsManager.Internal.IEventStore"/>. /// </summary> /// <param name="eventObject">Event object.<see cref="Amazon.MobileAnalytics.Model.Event"/></param> public void EnqueueEventsForDelivery(Amazon.MobileAnalytics.Model.Event eventObject) { #if BCL35 ThreadPool.QueueUserWorkItem(new WaitCallback(delegate { #elif PCL || BCL45 Task.Run(() => { #endif string eventString = null; try { eventString = JsonMapper.ToJson(eventObject); } catch (Exception e) { _logger.Error(e, "An exception occurred when converting low level client event to json string."); List <Amazon.MobileAnalytics.Model.Event> eventList = new List <Amazon.MobileAnalytics.Model.Event>(); eventList.Add(eventObject); MobileAnalyticsErrorEventArgs eventArgs = new MobileAnalyticsErrorEventArgs(this.GetType().Name, "An exception occurred when converting low level client event to json string.", e, eventList); _maManager.OnRaiseErrorEvent(eventArgs); } if (null != eventString) { try { _eventStore.PutEvent(eventString, _appID); } catch (Exception e) { _logger.Error(e, "Event {0} was not stored.", eventObject.EventType); MobileAnalyticsErrorEventArgs eventArgs = new MobileAnalyticsErrorEventArgs(this.GetType().Name, "An exception occurred when storing event into event store.", e, new List <Amazon.MobileAnalytics.Model.Event>()); _maManager.OnRaiseErrorEvent(eventArgs); } _logger.DebugFormat("Event {0} is queued for delivery", eventObject.EventType); } #if BCL35 })); #elif PCL || BCL45 });
/// <summary> /// Sends Mobile Analytics events to server on background thread. /// </summary> private void DoWork() { while (!_shouldStop) { try { _logger.InfoFormat("Mobile Analytics Manager is trying to deliver events in background thread."); IDictionary <string, MobileAnalyticsManager> instanceDictionary = MobileAnalyticsManager.CopyOfInstanceDictionary; foreach (string appId in instanceDictionary.Keys) { MobileAnalyticsManager manager = null; try { manager = MobileAnalyticsManager.GetInstance(appId); manager.BackgroundDeliveryClient.AttemptDelivery(); } catch (System.Exception e) { _logger.Error(e, "An exception occurred in Mobile Analytics Delivery Client : {0}", e.ToString()); if (null != manager) { MobileAnalyticsErrorEventArgs eventArgs = new MobileAnalyticsErrorEventArgs(this.GetType().Name, "An exception occurred when deliverying events to Amazon Mobile Analytics.", e, new List <Amazon.MobileAnalytics.Model.Event>()); manager.OnRaiseErrorEvent(eventArgs); } } } Thread.Sleep(BackgroundSubmissionWaitTime * 1000); } catch (ThreadAbortException tae) { _logger.DebugFormat("Mobile Analytics Manager thread aborted"); } catch (System.Exception e) { _logger.Error(e, "An exception occurred in Mobile Analytics Manager."); } UnityRequestQueue.Instance.ExecuteOnMainThread(() => { if (Application.isEditor && !Application.isPlaying) { AbortBackgroundThread(); } }); } }
private async Task DoWorkAsync(int millisecondsDelay) { while (!ShouldStop) { await Task.Delay(millisecondsDelay).ConfigureAwait(false); if (ShouldStop) { break; } try { _logger.InfoFormat("Mobile Analytics Manager is trying to deliver events in background thread."); IDictionary <string, MobileAnalyticsManager> instanceDictionary = MobileAnalyticsManager.CopyOfInstanceDictionary; foreach (string appId in instanceDictionary.Keys) { MobileAnalyticsManager manager = null; try { manager = MobileAnalyticsManager.GetInstance(appId); await manager.BackgroundDeliveryClient.AttemptDeliveryAsync().ConfigureAwait(false); } catch (System.Exception e) { _logger.Error(e, "An exception occurred in Mobile Analytics Delivery Client : {0}", e.ToString()); if (null != manager) { MobileAnalyticsErrorEventArgs eventArgs = new MobileAnalyticsErrorEventArgs(this.GetType().Name, "An exception occurred when deliverying events to Amazon Mobile Analytics.", e, new List <Amazon.MobileAnalytics.Model.Event>()); manager.OnRaiseErrorEvent(eventArgs); } } } } catch (System.Exception e) { _logger.Error(e, "An exception occurred in Mobile Analytics Manager : {0}", e.ToString()); } } }
/// <summary> /// Submits a single batch of events to the service. /// </summary> /// <param name="rowIds">Row identifiers. The list of rowId, that is unique identifier of each event.</param> /// <param name="eventList">The list of events that need to be submitted.</param> private void SubmitEvents(List <string> rowIds, List <Amazon.MobileAnalytics.Model.Event> eventList) { PutEventsRequest putRequest = new PutEventsRequest(); putRequest.Events = eventList; putRequest.ClientContext = Convert.ToBase64String( System.Text.Encoding.UTF8.GetBytes(_clientContext.ToJsonString())); putRequest.ClientContextEncoding = "base64"; _logger.DebugFormat("Client Context is : {0}", _clientContext.ToJsonString()); PutEventsResponse resp = null; try { resp = _mobileAnalyticsLowLevelClient.PutEvents(putRequest); } catch (AmazonMobileAnalyticsException e) { _logger.Error(e, "An AmazonMobileAnalyticsException occurred while sending Amazon Mobile Analytics request: error code is {0} ; error type is {1} ; request id is {2} ; status code is {3} ; error message is {4}", e.ErrorCode, e.ErrorType, e.RequestId, e.StatusCode, e.Message); // Delete events in any of the three error codes. if (e.StatusCode == HttpStatusCode.BadRequest && (e.ErrorCode.Equals("ValidationException", StringComparison.CurrentCultureIgnoreCase) || e.ErrorCode.Equals("SerializationException", StringComparison.CurrentCultureIgnoreCase) || e.ErrorCode.Equals("BadRequestException", StringComparison.CurrentCultureIgnoreCase))) { MobileAnalyticsErrorEventArgs eventArgs = new MobileAnalyticsErrorEventArgs(this.GetType().Name, "Amazon Mobile Analytics Service returned an error.", e, eventList); _maManager.OnRaiseErrorEvent(eventArgs); _logger.InfoFormat("The error code is not retriable. Delete {0} events from local storage.", rowIds.Count); _eventStore.DeleteEvent(rowIds); } else { MobileAnalyticsErrorEventArgs eventArgs = new MobileAnalyticsErrorEventArgs(this.GetType().Name, "Amazon Mobile Analytics Service returned an error.", e, new List <Amazon.MobileAnalytics.Model.Event>()); _maManager.OnRaiseErrorEvent(eventArgs); } } catch (AmazonServiceException e) { _logger.Error(e, "An AmazonServiceException occurred while sending Amazon Mobile Analytics request: error code is {0} ; error type is {1} ; request id is {2} ; status code is {3} ; error message is {4} ", e.ErrorCode, e.ErrorType, e.RequestId, e.StatusCode, e.Message); MobileAnalyticsErrorEventArgs eventArgs = new MobileAnalyticsErrorEventArgs(this.GetType().Name, "Amazon Web Service returned an error.", e, new List <Amazon.MobileAnalytics.Model.Event>()); _maManager.OnRaiseErrorEvent(eventArgs); } catch (Exception e) { _logger.Error(e, "An exception occurred while sending Amazon Mobile Analytics request."); MobileAnalyticsErrorEventArgs eventArgs = new MobileAnalyticsErrorEventArgs(this.GetType().Name, "An exception occurred when sending request to Amazon Mobile Analytics.", e, new List <Amazon.MobileAnalytics.Model.Event>()); _maManager.OnRaiseErrorEvent(eventArgs); } finally { if (resp != null && resp.HttpStatusCode == HttpStatusCode.Accepted) { _logger.InfoFormat("Mobile Analytics client successfully delivered {0} events to service. Delete those events from local storage.", rowIds.Count); _eventStore.DeleteEvent(rowIds); } lock (_deliveryLock) { _deliveryInProgress = false; } } }