-
Notifications
You must be signed in to change notification settings - Fork 6
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
Coroutines: send events flow #92
Merged
Merged
Changes from all commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
5c999cc
refactor: extract json mapping logic to Kotlin object
wzieba de701d2
refactor: introduce `SendEvents` use case with same logic as `Parsely…
wzieba 4ae78b2
refactor: migrate `ParselyAPIConnection` to Coroutines
wzieba 920e995
test: update `ParselyAPIConnectionTest` to support Coroutines
wzieba 17e7ecd
tests: remove unit tests that checks for `GET` request on empty payload
wzieba 5bff337
tests: update `ROOT_URL` before `ParselyTracker` constructor
wzieba 51c89b9
feat: closing the connection after successful request
wzieba 479d262
feat: close the connection after successful request
wzieba 742fc5d
feat: make `ParselyAPIConnection` return `Result`
wzieba 9668a91
refactor: move handling HTTP request results to `SendEvents`
wzieba b7c98a7
tests: update unit tests to reflect current scope of work of `Parsely…
wzieba 1be8dfc
Merge branch 'coroutines' into coroutines-send-event
wzieba 038c0ab
refactor: use `LocalStorageRepository` methods directly
wzieba f294f73
refactor: use `FlushManager` methods directly
wzieba c93b567
fix: on successful request, remove only sent events
wzieba debd023
style: remove unused methods of `LocalStorageRepository`
wzieba 940b241
tests: add tests for SendEvents usecase
wzieba fa73ccd
refactor: move serialization details to `JsonSerializer`
wzieba fab4007
fix: do not stop flush manager if local queue is not empty
wzieba 4237fcd
fix: add mutual execution for `SendEvents#invoke`
wzieba 69f12b3
refactor: remove `FlushQueue` AsyncTask
wzieba ca330ab
style: remove unused methods
wzieba 8d4edd4
fix: make `LocalStorageRepository#getStoredQueue` thread safe and off…
wzieba 643defe
fix: do not create a deadlock
wzieba 39f8831
fix: do not specify context for `ParselyAPIConnection`
wzieba dbc73e1
tests: fix loading an empty file for `ParselyAPIConnectionTest`
wzieba 366dae5
refactor: rewrite SdkInit to simple function
wzieba acae5f6
refactor: introduce `FlushManager` interface
wzieba 77303c6
refactor: introduce `QueueManager` interface
wzieba be74b28
refactor: introduce `QueueManager` interface
wzieba a13485c
refactor: introduce `RestClient` interface
wzieba cb5113c
feat: start `FlushManager` without checking state of stored queue first
wzieba 4b4e471
refactor: pass `onFlush` lambda to `ParselyFlushManager`
wzieba c9872d4
refactor: rename `SendEvents` to `FlushQueue` and `isDebug` to `skipS…
wzieba 269a9d6
style: improve position of logging statements in `FlushQueue`
wzieba 409c42d
style: return from `FlushQueue` if `skipSendingEvents`
wzieba 95f7a65
style: make variables test-local where possible
wzieba c32302f
feat: update lock logic on local storage repo
wzieba 2c0ce6e
style: remove unnecessary `else` from `FlushQueue`
wzieba 784a021
feat: do not stop FlushManager on successful flush
wzieba 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 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 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 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 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
51 changes: 51 additions & 0 deletions
51
parsely/src/main/java/com/parsely/parselyandroid/FlushQueue.kt
This file contains 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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.parsely.parselyandroid | ||
|
||
import com.parsely.parselyandroid.JsonSerializer.toParselyEventsPayload | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.sync.Mutex | ||
import kotlinx.coroutines.sync.withLock | ||
|
||
internal class FlushQueue( | ||
private val flushManager: FlushManager, | ||
private val repository: QueueRepository, | ||
private val restClient: RestClient, | ||
private val scope: CoroutineScope | ||
) { | ||
|
||
private val mutex = Mutex() | ||
|
||
operator fun invoke(skipSendingEvents: Boolean) { | ||
scope.launch { | ||
mutex.withLock { | ||
val eventsToSend = repository.getStoredQueue() | ||
|
||
if (eventsToSend.isEmpty()) { | ||
flushManager.stop() | ||
return@launch | ||
} | ||
|
||
if (skipSendingEvents) { | ||
ParselyTracker.PLog("Debug mode on. Not sending to Parse.ly. Otherwise, would sent ${eventsToSend.size} events") | ||
repository.remove(eventsToSend) | ||
return@launch | ||
} | ||
ParselyTracker.PLog("Sending request with %d events", eventsToSend.size) | ||
val jsonPayload = toParselyEventsPayload(eventsToSend) | ||
ParselyTracker.PLog("POST Data %s", jsonPayload) | ||
ParselyTracker.PLog("Requested %s", ParselyTracker.ROOT_URL) | ||
restClient.send(jsonPayload) | ||
.fold( | ||
onSuccess = { | ||
ParselyTracker.PLog("Pixel request success") | ||
repository.remove(eventsToSend) | ||
}, | ||
onFailure = { | ||
ParselyTracker.PLog("Pixel request exception") | ||
ParselyTracker.PLog(it.toString()) | ||
} | ||
) | ||
} | ||
} | ||
} | ||
} |
This file contains 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
32 changes: 32 additions & 0 deletions
32
parsely/src/main/java/com/parsely/parselyandroid/JsonSerializer.kt
This file contains 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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.parsely.parselyandroid | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import java.io.IOException | ||
import java.io.StringWriter | ||
|
||
internal object JsonSerializer { | ||
|
||
fun toParselyEventsPayload(eventsToSend: List<Map<String, Any?>?>): String { | ||
val batchMap: MutableMap<String, Any> = HashMap() | ||
batchMap["events"] = eventsToSend | ||
return toJson(batchMap).orEmpty() | ||
} | ||
/** | ||
* Encode an event Map as JSON. | ||
* | ||
* @param map The Map object to encode as JSON. | ||
* @return The JSON-encoded value of `map`. | ||
*/ | ||
private fun toJson(map: Map<String, Any>): String? { | ||
val mapper = ObjectMapper() | ||
var ret: String? = null | ||
try { | ||
val strWriter = StringWriter() | ||
mapper.writeValue(strWriter, map) | ||
ret = strWriter.toString() | ||
} catch (e: IOException) { | ||
e.printStackTrace() | ||
} | ||
return ret | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.
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.
Does this need to stop the
flushManager
as well? I guess it'll get stopped the next time it's flushed. 🤔I wonder if we should skip starting the
FlushQueue
all together in a case where we don't want the events to be sent. This is probably beyond the scope of this PR though, so I am just making a note of it here.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 removed stopping
flushManager
from this part of code in 784a021.I see your point but the
skipSendingEvents
option is used only for tests (debug mode). As it's like--dry-run
, I wouldn't opt to remove startingflushManager
then, as this would cause a significant discrepancy between debug and non-debug modes of SDK. In other words: in debug mode, I believe we want to be as close as possible to non-debug. WDYT?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.
That makes sense and sounds like the more practical option 👍