Skip to content

Update the Event Flushing Mechanism to save in disk #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion Runtime/Aptabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public static void OnApplicationFocus(bool hasFocus)
}
else
{
Flush();
_dispatcher.FlushOrSaveToDisk();
StopPolling();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we need to make this return a Task as well:

public static Task Flush()
{
     return _dispatcher.Flush();
}

Also I think we should avoid async void methods, eg:

private static async void StartPolling(int flushTimer)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flush already return a Task
And I do agree that we need to avoid async void.

The goal of this PR is to keep changes to a minimal

}
}
Expand Down
22 changes: 19 additions & 3 deletions Runtime/Dispatcher/Dispatcher.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AptabaseSDK.TinyJson;
using UnityEngine;
Expand All @@ -8,6 +9,7 @@ namespace AptabaseSDK
public class Dispatcher: IDispatcher
{
private const string EVENTS_ENDPOINT = "/api/v0/events";
private const string APTABASE_KEY = "aptabase_key";

private const int MAX_BATCH_SIZE = 25;

Expand All @@ -21,14 +23,19 @@ public class Dispatcher: IDispatcher

public Dispatcher(string appKey, string baseURL, EnvironmentInfo env)
{
var cachedEventsJson = PlayerPrefs.GetString(APTABASE_KEY);
var cacheEvents = string.IsNullOrEmpty(cachedEventsJson) ? new List<Event>() : cachedEventsJson.FromJson<List<Event>>();

//create event queue
_events = new Queue<Event>();
_events = new Queue<Event>(cacheEvents);

//web request setup information
_apiURL = $"{baseURL}{EVENTS_ENDPOINT}";
_appKey = appKey;
_environment = env;
_webRequestHelper = new WebRequestHelper();

PlayerPrefs.DeleteKey(APTABASE_KEY);
}

public void Enqueue(Event data)
Expand All @@ -42,7 +49,7 @@ private void Enqueue(List<Event> data)
_events.Enqueue(eventData);
}

public async void Flush()
public async Task Flush()
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm concerned we don't have any kind of thread safety mechanism on the Flush method

if (_flushInProgress || _events.Count <= 0)
return;
Expand Down Expand Up @@ -75,9 +82,18 @@ public async void Flush()

_flushInProgress = false;
}


public async Task FlushOrSaveToDisk()
{
await Flush();

PlayerPrefs.SetString(APTABASE_KEY, _events.Take(1000).ToList().ToJson());
}

private static async Task<bool> SendEvents(List<Event> events)
{
if(Application.internetReachability == NetworkReachability.NotReachable) return false;

var webRequest = _webRequestHelper.CreateWebRequest(_apiURL, _appKey, _environment, events.ToJson());
var result = await _webRequestHelper.SendWebRequestAsync(webRequest);
return result;
Expand Down
8 changes: 6 additions & 2 deletions Runtime/Dispatcher/IDispatcher.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using System.Threading.Tasks;

namespace AptabaseSDK
{
public interface IDispatcher
{
public void Enqueue(Event data);
void Enqueue(Event data);

public void Flush();
Task Flush();

Task FlushOrSaveToDisk();
}
}
22 changes: 19 additions & 3 deletions Runtime/Dispatcher/WebGLDispatcher.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AptabaseSDK.TinyJson;
using UnityEngine;

namespace AptabaseSDK
{
public class WebGLDispatcher: IDispatcher
{
private const string EVENT_ENDPOINT = "/api/v0/event";
private const string APTABASE_KEY = "aptabase_key";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have a better name for this key, eg aptabase_events_cache?


private static string _apiURL;
private static WebRequestHelper _webRequestHelper;
Expand All @@ -18,20 +21,24 @@ public class WebGLDispatcher: IDispatcher

public WebGLDispatcher(string appKey, string baseURL, EnvironmentInfo env)
{
var cachedEventsJson = PlayerPrefs.GetString(APTABASE_KEY);
var cacheEvents = string.IsNullOrEmpty(cachedEventsJson) ? new List<Event>() : cachedEventsJson.FromJson<List<Event>>();

//create event queue
_events = new Queue<Event>();
_events = new Queue<Event>(cacheEvents);

//web request setup information
_apiURL = $"{baseURL}{EVENT_ENDPOINT}";
_appKey = appKey;
_environment = env;
_webRequestHelper = new WebRequestHelper();

PlayerPrefs.DeleteKey(APTABASE_KEY);
}

public void Enqueue(Event data)
{
_events.Enqueue(data);
Flush();
}

private void Enqueue(List<Event> data)
Expand All @@ -40,7 +47,7 @@ private void Enqueue(List<Event> data)
_events.Enqueue(eventData);
}

public async void Flush()
public async Task Flush()
{
if (_flushInProgress || _events.Count <= 0)
return;
Expand Down Expand Up @@ -69,9 +76,18 @@ public async void Flush()

_flushInProgress = false;
}

public async Task FlushOrSaveToDisk()
{
await Flush();

PlayerPrefs.SetString(APTABASE_KEY, _events.Take(1000).ToList().ToJson());
}

private static async Task<bool> SendEvent(Event eventData)
{
if(Application.internetReachability == NetworkReachability.NotReachable) return false;

var webRequest = _webRequestHelper.CreateWebRequest(_apiURL, _appKey, _environment, eventData.ToJson());
var result = await _webRequestHelper.SendWebRequestAsync(webRequest);
return result;
Expand Down