private static void DispatchOperations() { ThreadOperation operation = _ops.Dequeue(); if (operation == null) { return; } SeembaMixpanel.Log($"Dispatching new operation: {operation.GetAction()}"); Value data = operation.GetWhat(); switch (operation.GetAction()) { case ThreadOperation.ThreadOperationAction.ENQUEUE_EVENTS: EnqueueMixpanelQueue(MixpanelStorage.TrackPersistentQueue, data); break; case ThreadOperation.ThreadOperationAction.ENQUEUE_PEOPLE: EnqueueMixpanelQueue(MixpanelStorage.EngagePersistentQueue, data); break; case ThreadOperation.ThreadOperationAction.FLUSH: if (_isBgThreadRunning) { IEnumerator trackEnum = SendData(MixpanelStorage.TrackPersistentQueue, Config.TrackUrl); IEnumerator engageEnum = SendData(MixpanelStorage.EngagePersistentQueue, Config.EngageUrl); while (trackEnum.MoveNext()) { } ; while (engageEnum.MoveNext()) { } ; } else { Controller.GetInstance().StartCoroutine(SendData(MixpanelStorage.TrackPersistentQueue, Config.TrackUrl)); Controller.GetInstance().StartCoroutine(SendData(MixpanelStorage.EngagePersistentQueue, Config.EngageUrl)); } break; case ThreadOperation.ThreadOperationAction.CLEAR_QUEUE: MixpanelStorage.TrackPersistentQueue.Clear(); MixpanelStorage.EngagePersistentQueue.Clear(); break; case ThreadOperation.ThreadOperationAction.KILL_THREAD: _isBgThreadRunning = false; _bgThread.Abort(); // Will throw an exception break; default: break; } }
/// <summary> /// Checks whether Mixpanel is initialized or not. If it is not, every API will be no-op. /// </summary> public static bool IsInitialized() { bool initialized = Controller.IsInitialized(); if (!initialized) { SeembaMixpanel.Log("Mixpanel is not initialized"); } return(initialized); }
private static void InitializeBeforeSceneLoad() { SeembaSDKMixpanelSettings.LoadSettings(); if (Config.ManualInitialization) { return; } Initialize(); SeembaMixpanel.Log($"Track Queue Depth: {MixpanelStorage.TrackPersistentQueue.CurrentCountOfItemsInQueue}"); SeembaMixpanel.Log($"Engage Queue Depth: {MixpanelStorage.EngagePersistentQueue.CurrentCountOfItemsInQueue}"); }
private IEnumerator Start() { MigrateFrom1To2(); DontDestroyOnLoad(this); StartCoroutine(PopulatePools()); Worker.StartWorkerThread(); TrackIntegrationEvent(); SeembaMixpanel.Log($"Mixpanel Component Started"); while (true) { yield return(new WaitForSecondsRealtime(Config.FlushInterval)); DoFlush(); } }
internal static IEnumerator SendData(PersistentQueue persistentQueue, string url) { if (persistentQueue.CurrentCountOfItemsInQueue == 0) { yield break; } int depth = persistentQueue.CurrentCountOfItemsInQueue; int numBatches = (depth / Config.BatchSize) + (depth % Config.BatchSize != 0 ? 1 : 0); for (int i = 0; i < numBatches; i++) { if (_stopThread) { yield break; } Value batch = SeembaMixpanel.ArrayPool.Get(); using (PersistentQueueSession session = persistentQueue.OpenSession()) { int count = 0; while (count < Config.BatchSize) { byte[] data = session.Dequeue(); if (data == null) { break; } try { batch.Add(JsonUtility.FromJson <Value>(Encoding.UTF8.GetString(data))); } catch (Exception e) { SeembaMixpanel.LogError($"There was an error processing event [{count}] from the internal object pool: " + e); } ++count; } if (count == 0) { yield break; } string payload = Convert.ToBase64String(Encoding.UTF8.GetBytes(batch.ToString())); SeembaMixpanel.Log($"Sending Request - '{url}' with payload '{payload}'"); bool successful = false; int responseCode = -1; string response = null; if (_isBgThreadRunning) { try { var content = new StringContent("data=" + payload, Encoding.UTF8, "application/json"); var responseRequest = _client.PostAsync(url, content).Result; responseCode = (int)responseRequest.StatusCode; response = responseRequest.Content.ReadAsStringAsync().Result; } catch (Exception e) { SeembaMixpanel.LogError("There was an error sending the request: " + e); } } else { WWWForm form = new WWWForm(); form.AddField("data", payload); UnityWebRequest request = UnityWebRequest.Post(url, form); yield return(request.SendWebRequest()); while (!request.isDone) { yield return(new WaitForEndOfFrame()); } responseCode = (int)request.responseCode; response = request.downloadHandler.text; } SeembaMixpanel.Log($"Response - '{url}' was '{response}'"); successful = responseCode == (int)HttpStatusCode.OK; if (successful) { _retryCount = 0; session.Flush(); SeembaMixpanel.Put(batch); } else { _retryCount += 1; double retryIn = Math.Pow(2, _retryCount) * 60000; retryIn = Math.Min(retryIn, 10 * 60 * 1000); // limit 10 min SeembaMixpanel.Log("Retrying request in " + retryIn / 1000 + " seconds (retryCount=" + _retryCount + ")"); _retryTimer = new System.Threading.Timer((obj) => { ForceFlushOp(); _retryTimer.Dispose(); }, null, (int)retryIn, System.Threading.Timeout.Infinite); yield break; } } } }
void OnDestroy() { SeembaMixpanel.Log($"Mixpanel Component Destroyed"); Worker.StopWorkerThread(); }