-
Notifications
You must be signed in to change notification settings - Fork 4
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
CoderGamester
wants to merge
8
commits into
aptabase:main
Choose a base branch
from
CoderGamester:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f9d9071
Update the Event Flushing Mechanism to avoid losing events due to net…
CoderGamester 2784c90
Merge branch 'main' of https://github.com/CoderGamester/aptabase-unity
CoderGamester fb19f7d
Fix IDispatcher interface declaration to contain to reflect the Task …
CoderGamester 728fa21
Disable sending events if application is offline
CoderGamester 6877da2
Fix the cleanup from disk after getting the events from cache
CoderGamester 502ddb1
Fix type of PlayerPref key
CoderGamester 72c8ec9
Fix bug when using Aptabase Unity SDK for the first time in the game …
CoderGamester ea699f1
Set a limit of events that the queue dispacher can safe to avoid over…
CoderGamester File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -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; | ||
|
||
|
@@ -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) | ||
|
@@ -42,7 +49,7 @@ private void Enqueue(List<Event> data) | |
_events.Enqueue(eventData); | ||
} | ||
|
||
public async void Flush() | ||
public async Task Flush() | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
@@ -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; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we have a better name for this key, eg |
||
|
||
private static string _apiURL; | ||
private static WebRequestHelper _webRequestHelper; | ||
|
@@ -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) | ||
|
@@ -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; | ||
|
@@ -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; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:Also I think we should avoid
async void
methods, eg:There was a problem hiding this comment.
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