diff --git a/Runtime/Api/Analytics/GameTelemetry.cs b/Runtime/Api/Analytics/GameTelemetry.cs index 134d5730..b4972fa3 100644 --- a/Runtime/Api/Analytics/GameTelemetry.cs +++ b/Runtime/Api/Analytics/GameTelemetry.cs @@ -33,6 +33,7 @@ public class GameTelemetry : WrapperBase IAccelByteDataStorage cacheStorage; string cacheTableName; + private bool useCache; private WaitTimeCommand telemetryLoopCommand; internal TimeSpan TelemetryInterval @@ -54,6 +55,7 @@ internal GameTelemetry( ClientGameTelemetryApi inApi session = inSession; + useCache = true; cacheStorage = session.DataStorage; cacheTableName = $"GameTelemetryCache/{AccelByteSDK.Environment.Current}.cache"; @@ -100,6 +102,13 @@ public void SetBatchFrequency( TimeSpan interval ) $"Set to {minimumTelemetryInterval.TotalSeconds} seconds."); telemetryInterval = minimumTelemetryInterval; } + + if (cancelationTokenSource != null) + { + cancelationTokenSource.Cancel(); + cancelationTokenSource.Dispose(); + InitializeTelemetryLoop(); + } } /// @@ -113,6 +122,24 @@ public void SetBatchFrequency(int intervalSeconds) SetBatchFrequency(TimeSpan.FromSeconds(intervalSeconds)); } + /// + /// Enable or disable the usage of the file storage cache. + /// The cache stores all events in JSON before being sent in case + /// of crash or errors and attempts to re-send them when it can. + /// The cache is enabled by default. + /// + /// Enables or disables the usage of the cache. + public void SetUseCache(bool shouldUseCache) + { + useCache = shouldUseCache; + + if (!useCache) + { + eventList.Clear(); + DeleteCache(); + } + } + /// /// Set list of event that need to be sent immediately without the needs to jobQueue it. /// @@ -147,7 +174,11 @@ public void Send( TelemetryBody telemetryBody else { jobQueue.Enqueue(new Tuple(telemetryBody, callback)); - AppendEventToCache(telemetryBody); + + if (useCache) + { + AppendEventToCache(telemetryBody); + } if (cancelationTokenSource == null) { @@ -203,7 +234,10 @@ public void SendTelemetryBatch(ResultCallback callback) } else { - RemoveEventsFromCache(); + if (useCache) + { + RemoveEventsFromCache(); + } } foreach (var telemetryCallback in telemetryCallbacks) @@ -222,6 +256,11 @@ internal override void SetSharedMemory(ApiSharedMemory newSharedMemory) private void SendCachedTelemetry() { + if (!useCache) + { + return; + } + LoadEventsFromCache(telemetryBodies => { SendTelemetryRequest(telemetryBodies, cb => @@ -250,7 +289,11 @@ private void RunPeriodicTelemetry() } SendTelemetryBatch(callback: null); - + InitializeTelemetryLoop(); + } + + private void InitializeTelemetryLoop() + { cancelationTokenSource = new CancellationTokenSource(); telemetryLoopCommand = new WaitTimeCommand(waitTime: telemetryInterval.TotalSeconds , cancellationToken: cancelationTokenSource.Token @@ -290,6 +333,11 @@ private void TelemetryLoop() internal void LoadEventsFromCache(Action> onLoadCacheDone) { + if (!useCache) + { + onLoadCacheDone?.Invoke(null); + return; + } if (!session.IsValid()) { onLoadCacheDone?.Invoke(null); @@ -301,6 +349,12 @@ internal void LoadEventsFromCache(Action> onLoadCacheDone) internal void LoadEventsFromCache(string key, Action> onLoadCacheDone) { + if (!useCache) + { + onLoadCacheDone?.Invoke(null); + return; + } + cacheStorage.GetItem( key , tableName: cacheTableName @@ -328,6 +382,11 @@ internal void LoadEventsFromCache(string key, Action> onLoad internal void AppendEventToCache(TelemetryBody telemetryBody, Action onSaveCacheDone = null) { + if (!useCache) + { + onSaveCacheDone?.Invoke(false); + return; + } if (!session.IsValid()) { onSaveCacheDone?.Invoke(false); @@ -339,6 +398,12 @@ internal void AppendEventToCache(TelemetryBody telemetryBody, Action onSav internal void AppendEventToCache(string key, TelemetryBody telemetryBody, Action onSaveCacheDone = null) { + if (!useCache) + { + onSaveCacheDone?.Invoke(false); + return; + } + eventList.Add(telemetryBody); var telemetryEventsJson = System.Text.Encoding.UTF8.GetString(eventList.ToUtf8Json()); cacheStorage.SaveItem(key, telemetryEventsJson, onSaveCacheDone, tableName: cacheTableName); @@ -351,6 +416,10 @@ internal void DeleteCache(Action onDone = null) private void RemoveEventsFromCache() { + if (!useCache) + { + return; + } if (session != null && !session.IsValid()) { return; diff --git a/Runtime/Server/Analytics/ServerGameTelemetry.cs b/Runtime/Server/Analytics/ServerGameTelemetry.cs index cde0e619..531e15c1 100644 --- a/Runtime/Server/Analytics/ServerGameTelemetry.cs +++ b/Runtime/Server/Analytics/ServerGameTelemetry.cs @@ -191,7 +191,6 @@ private void RunPeriodicTelemetry() } SendTelemetryBatch(callback: null); - cancelationTokenSource = new System.Threading.CancellationTokenSource(); IndefiniteLoopCommand telemetryLoopCommand = new IndefiniteLoopCommand(interval: telemetryInterval.TotalSeconds , cancellationToken: cancelationTokenSource.Token