Skip to content

Commit

Permalink
Skip custom logging logic when app is shutting down (#96)
Browse files Browse the repository at this point in the history
* Skip custom logging logic when app is shutting down

* Remove unused  statements from TDKLogger

* added/moved to appIsQuitting in main tdk instance

* added appIsQuitting to base service

* don't attempt event transmission on app quit (only persist - will be sent on next launch)

* lifted analytics flush frequency settings to config

* remove connect settings from packager

---------

Co-authored-by: Yuri Visser <[email protected]>
  • Loading branch information
lefarchi and aphexyuri authored Aug 22, 2024
1 parent c3b157f commit 82eedd1
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 16 deletions.
1 change: 0 additions & 1 deletion Assets/Treasure/TDK-Private/Editor/TDKPackagerCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public static void ExportPackage()
"Assets/Treasure/TDK/Runtime/Utils",

// Resources
"Assets/Treasure/TDK/Resources/TDKConnectSettings.asset",
"Assets/Treasure/TDK/Resources/TDKConnectThemeData.asset",

"Assets/package.json",
Expand Down
1 change: 0 additions & 1 deletion Assets/Treasure/TDK-Private/Editor/TDKPackagerFull.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public static void ExportPackage()
"Assets/Treasure/TDK/ConnectInternal",
"Assets/Treasure/TDK/ConnectPrefabs",
"Assets/Treasure/TDK/Editor",
"Assets/Treasure/TDK/Resources/TDKConnectSettings.asset",
"Assets/Treasure/TDK/Resources/TDKConnectThemeData.asset",
"Assets/Treasure/TDK/Runtime",

Expand Down
2 changes: 2 additions & 0 deletions Assets/Treasure/TDK/Runtime/Infrastructure/TDKBaseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace Treasure
// why do we need empty game objects for services? it could be independent from unity
public class TDKBaseService : MonoBehaviour
{
protected bool appIsQuitting = false;

public virtual void Awake()
{
DontDestroyOnLoad(this);
Expand Down
14 changes: 8 additions & 6 deletions Assets/Treasure/TDK/Runtime/Infrastructure/TDKLogger.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using UnityEngine;
using System;
using System.Collections;
using System.Diagnostics;

namespace Treasure
{
Expand Down Expand Up @@ -36,6 +34,10 @@ public static void Log(string message)
}

private static void LogByLevel(TDKConfig.LoggerLevelValue loggerLevelValue, string message) {
if (TDK.Instance.appIsQuitting) {
Debug.Log($"[{loggerLevelValue}] {message}");
return;
}
if (TDK.Instance.AppConfig.LoggerLevel > loggerLevelValue) {
return;
}
Expand All @@ -44,13 +46,13 @@ private static void LogByLevel(TDKConfig.LoggerLevelValue loggerLevelValue, stri
switch (loggerLevelValue)
{
case TDKConfig.LoggerLevelValue.ERROR:
UnityEngine.Debug.LogError(message);
Debug.LogError(message);
break;
case TDKConfig.LoggerLevelValue.WARNING:
UnityEngine.Debug.LogWarning(message);
Debug.LogWarning(message);
break;
default:
UnityEngine.Debug.Log(message);
Debug.Log(message);
break;
}
#else
Expand All @@ -61,7 +63,7 @@ private static void LogByLevel(TDKConfig.LoggerLevelValue loggerLevelValue, stri
private static void LogMessageInternal(string message)
{
try {
UnityEngine.Debug.Log(string.Format("[{0}] {1}", Time.realtimeSinceStartup, message));
Debug.Log(string.Format("[{0}] {1}", Time.realtimeSinceStartup, message));
} catch {
// no-op
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ public static class AnalyticsConstants
// event cache values
public const int MAX_CACHE_EVENT_COUNT = 10;
public const int MAX_CACHE_SIZE_KB = 64;
public const int MEMORY_CACHE_FLUSH_TIME_SECONDS = 10;
public const int DISK_CACHE_FLUSH_TIME_SECONDS = 60;

// persistent store values
public const string PERSISTENT_DIRECTORY_NAME = "AnalyticsStore";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private async Task StartBackgroundDiskCacheFlush() {
break;
}
}
await Task.Delay(TimeSpan.FromSeconds(AnalyticsConstants.DISK_CACHE_FLUSH_TIME_SECONDS));
await Task.Delay(TimeSpan.FromSeconds(TDK.Instance.AppConfig.AnalyticsDiscFlushFrequencySeconds));
}
}

Expand All @@ -89,8 +89,8 @@ private async void TerminateCacheFlushing()
private void ResetFlushTimer()
{
_flushCacheTimer?.Change(
TimeSpan.FromSeconds(AnalyticsConstants.MEMORY_CACHE_FLUSH_TIME_SECONDS),
TimeSpan.FromSeconds(AnalyticsConstants.MEMORY_CACHE_FLUSH_TIME_SECONDS)
TimeSpan.FromSeconds(TDK.Instance.AppConfig.AnalyticsMemoryFlushFrequencySeconds),
TimeSpan.FromSeconds(TDK.Instance.AppConfig.AnalyticsMemoryFlushFrequencySeconds)
);
}

Expand All @@ -105,8 +105,13 @@ private async Task FlushMemoryCache()

if(memoryCacheCopy.Count > 0)
{
// Send the batch of events for io
var success = await SendEventBatch(memoryCacheCopy);
var success = false;

if(!appIsQuitting)
{
// Send the batch of events for io
success = await SendEventBatch(memoryCacheCopy);
}

// If the request failed, persist the payload to disk in a separate task
if(!success)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public override void Awake()
#if !UNITY_WEBGL
void OnApplicationQuit()
{
appIsQuitting = true;
TerminateCacheFlushing();
}
#endif
Expand Down
6 changes: 6 additions & 0 deletions Assets/Treasure/TDK/Runtime/TDK.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@ public partial class TDK : MonoBehaviour

public TDKConfig AppConfig { get; private set; }

internal bool appIsQuitting = false;

void OnApplicationPause(bool isPaused)
{
Analytics?.OnApplicationPause_Analytics(isPaused);
}

void OnApplicationQuit() {
appIsQuitting = true;
}

public static TDK Instance
{
get
Expand Down
7 changes: 6 additions & 1 deletion Assets/Treasure/TDK/Runtime/TDKConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public class AnalyticsConfig
{
public string _devApiUrl = "https://darkmatter.spellcaster.lol/ingress";
public string _prodApiUrl = "https://darkmatter.treasure.lol/ingress";
public int _memoryFlushFrequencySeconds = 10;
public int _diskFlushFrequencySeconds = 30;
}

public enum Env { DEV, PROD }
Expand Down Expand Up @@ -95,7 +97,8 @@ public Env Environment
public int SessionMinDurationLeftSec => _connect._sessionMinDurationLeftSec;

public string AnalyticsApiUrl => Environment == Env.DEV ? _analytics._devApiUrl : _analytics._prodApiUrl;

public int AnalyticsMemoryFlushFrequencySeconds => _analytics._memoryFlushFrequencySeconds;
public int AnalyticsDiscFlushFrequencySeconds => _analytics._diskFlushFrequencySeconds;

public LoggerLevelValue LoggerLevel
{
Expand Down Expand Up @@ -249,6 +252,8 @@ public class SerializedAnalyticsConfig
{
public string devApiUrl;
public string prodApiUrl;
public int memoryFlushFrequencySeconds;
public int diskFlushFrequencySeconds;
}

[Serializable]
Expand Down

0 comments on commit 82eedd1

Please sign in to comment.