Skip to content
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

feat: use new header to set polling interval #285

Merged
merged 2 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import okhttp3.Cache
import okhttp3.Headers
import okhttp3.OkHttpClient
import okhttp3.ResponseBody.Companion.toResponseBody
import retrofit2.Retrofit
Expand Down Expand Up @@ -115,7 +116,7 @@
}

internal fun fetchUserMessages() {
GlobalScope.launch {

Check warning on line 119 in messaginginapp/src/main/java/io/customer/messaginginapp/gist/data/listeners/Queue.kt

View workflow job for this annotation

GitHub Actions / Android Lint (messaginginapp)

This is a delicate API and its use requires care. Make sure you fully read and understand documentation of the declaration that is marked as a delicate API.

Check warning on line 119 in messaginginapp/src/main/java/io/customer/messaginginapp/gist/data/listeners/Queue.kt

View workflow job for this annotation

GitHub Actions / API check

This is a delicate API and its use requires care. Make sure you fully read and understand documentation of the declaration that is marked as a delicate API.

Check warning on line 119 in messaginginapp/src/main/java/io/customer/messaginginapp/gist/data/listeners/Queue.kt

View workflow job for this annotation

GitHub Actions / Unit tests (messaginginapp)

This is a delicate API and its use requires care. Make sure you fully read and understand documentation of the declaration that is marked as a delicate API.

Check warning on line 119 in messaginginapp/src/main/java/io/customer/messaginginapp/gist/data/listeners/Queue.kt

View workflow job for this annotation

GitHub Actions / API check

This is a delicate API and its use requires care. Make sure you fully read and understand documentation of the declaration that is marked as a delicate API.

Check warning on line 119 in messaginginapp/src/main/java/io/customer/messaginginapp/gist/data/listeners/Queue.kt

View workflow job for this annotation

GitHub Actions / Android Lint (messaginginapp)

This is a delicate API and its use requires care. Make sure you fully read and understand documentation of the declaration that is marked as a delicate API.

Check warning on line 119 in messaginginapp/src/main/java/io/customer/messaginginapp/gist/data/listeners/Queue.kt

View workflow job for this annotation

GitHub Actions / Unit tests (messaginginapp)

This is a delicate API and its use requires care. Make sure you fully read and understand documentation of the declaration that is marked as a delicate API.

Check warning on line 119 in messaginginapp/src/main/java/io/customer/messaginginapp/gist/data/listeners/Queue.kt

View workflow job for this annotation

GitHub Actions / instrumentation-test (java_layout)

This is a delicate API and its use requires care. Make sure you fully read and understand documentation of the declaration that is marked as a delicate API.

Check warning on line 119 in messaginginapp/src/main/java/io/customer/messaginginapp/gist/data/listeners/Queue.kt

View workflow job for this annotation

GitHub Actions / instrumentation-test (kotlin_compose)

This is a delicate API and its use requires care. Make sure you fully read and understand documentation of the declaration that is marked as a delicate API.

Check warning on line 119 in messaginginapp/src/main/java/io/customer/messaginginapp/gist/data/listeners/Queue.kt

View workflow job for this annotation

GitHub Actions / instrumentation-test (kotlin_compose)

This is a delicate API and its use requires care. Make sure you fully read and understand documentation of the declaration that is marked as a delicate API.

Check warning on line 119 in messaginginapp/src/main/java/io/customer/messaginginapp/gist/data/listeners/Queue.kt

View workflow job for this annotation

GitHub Actions / instrumentation-test (java_layout)

This is a delicate API and its use requires care. Make sure you fully read and understand documentation of the declaration that is marked as a delicate API.
try {
Log.i(GIST_TAG, "Fetching user messages")
val latestMessagesResponse = gistQueueService.fetchMessagesForUser()
Expand All @@ -132,6 +133,9 @@
)
latestMessagesResponse.body()?.let { handleMessages(it) }
}

// Check if the polling interval changed and update timer.
updatePollingInterval(latestMessagesResponse.headers())

Check warning on line 138 in messaginginapp/src/main/java/io/customer/messaginginapp/gist/data/listeners/Queue.kt

View check run for this annotation

Codecov / codecov/patch

messaginginapp/src/main/java/io/customer/messaginginapp/gist/data/listeners/Queue.kt#L138

Added line #L138 was not covered by tests
} catch (e: Exception) {
Log.e(
GIST_TAG,
Expand All @@ -141,6 +145,20 @@
}
}

private fun updatePollingInterval(headers: Headers) {
headers["X-Gist-Queue-Polling-Interval"]?.toIntOrNull()?.let { pollingIntervalSeconds ->
if (pollingIntervalSeconds > 0) {
val newPollingIntervalMilliseconds = (pollingIntervalSeconds * 1000).toLong()

Check warning on line 151 in messaginginapp/src/main/java/io/customer/messaginginapp/gist/data/listeners/Queue.kt

View check run for this annotation

Codecov / codecov/patch

messaginginapp/src/main/java/io/customer/messaginginapp/gist/data/listeners/Queue.kt#L151

Added line #L151 was not covered by tests
if (newPollingIntervalMilliseconds != GistSdk.pollInterval) {
GistSdk.pollInterval = newPollingIntervalMilliseconds

Check warning on line 153 in messaginginapp/src/main/java/io/customer/messaginginapp/gist/data/listeners/Queue.kt

View check run for this annotation

Codecov / codecov/patch

messaginginapp/src/main/java/io/customer/messaginginapp/gist/data/listeners/Queue.kt#L153

Added line #L153 was not covered by tests
// Queue check fetches messages again and could result in infinite loop.
GistSdk.observeMessagesForUser(true)
Log.i(GIST_TAG, "Polling interval changed to: $pollingIntervalSeconds seconds")

Check warning on line 156 in messaginginapp/src/main/java/io/customer/messaginginapp/gist/data/listeners/Queue.kt

View check run for this annotation

Codecov / codecov/patch

messaginginapp/src/main/java/io/customer/messaginginapp/gist/data/listeners/Queue.kt#L155-L156

Added lines #L155 - L156 were not covered by tests
}
}
}
}

private fun handleMessages(messages: List<Message>) {
// Sorting messages by priority and placing nulls last
val sortedMessages = messages.sortedWith(compareBy(nullsLast()) { it.priority })
Expand Down Expand Up @@ -187,7 +205,7 @@
}

internal fun logView(message: Message) {
GlobalScope.launch {

Check warning on line 208 in messaginginapp/src/main/java/io/customer/messaginginapp/gist/data/listeners/Queue.kt

View workflow job for this annotation

GitHub Actions / API check

This is a delicate API and its use requires care. Make sure you fully read and understand documentation of the declaration that is marked as a delicate API.

Check warning on line 208 in messaginginapp/src/main/java/io/customer/messaginginapp/gist/data/listeners/Queue.kt

View workflow job for this annotation

GitHub Actions / API check

This is a delicate API and its use requires care. Make sure you fully read and understand documentation of the declaration that is marked as a delicate API.

Check warning on line 208 in messaginginapp/src/main/java/io/customer/messaginginapp/gist/data/listeners/Queue.kt

View workflow job for this annotation

GitHub Actions / instrumentation-test (java_layout)

This is a delicate API and its use requires care. Make sure you fully read and understand documentation of the declaration that is marked as a delicate API.

Check warning on line 208 in messaginginapp/src/main/java/io/customer/messaginginapp/gist/data/listeners/Queue.kt

View workflow job for this annotation

GitHub Actions / instrumentation-test (kotlin_compose)

This is a delicate API and its use requires care. Make sure you fully read and understand documentation of the declaration that is marked as a delicate API.

Check warning on line 208 in messaginginapp/src/main/java/io/customer/messaginginapp/gist/data/listeners/Queue.kt

View workflow job for this annotation

GitHub Actions / instrumentation-test (kotlin_compose)

This is a delicate API and its use requires care. Make sure you fully read and understand documentation of the declaration that is marked as a delicate API.

Check warning on line 208 in messaginginapp/src/main/java/io/customer/messaginginapp/gist/data/listeners/Queue.kt

View workflow job for this annotation

GitHub Actions / instrumentation-test (java_layout)

This is a delicate API and its use requires care. Make sure you fully read and understand documentation of the declaration that is marked as a delicate API.
try {
if (message.queueId != null) {
Log.i(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
object GistSdk : Application.ActivityLifecycleCallbacks {
private const val SHARED_PREFERENCES_NAME = "gist-sdk"
private const val SHARED_PREFERENCES_USER_TOKEN_KEY = "userToken"
private const val POLL_INTERVAL = 10_000L

private val sharedPreferences by lazy {
application.getSharedPreferences(SHARED_PREFERENCES_NAME, MODE_PRIVATE)
}

internal var pollInterval = 600_000L
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is the default poll value increased?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This will be our new default, the server will then adjust it remotely, you can read more here: INAPP-12248

lateinit var siteId: String
lateinit var dataCenter: String
internal lateinit var gistEnvironment: GistEnvironment
Expand Down Expand Up @@ -187,16 +187,18 @@

// Gist Message Observer

private fun observeMessagesForUser() {
internal fun observeMessagesForUser(skipQueueCheck: Boolean = false) {

Check warning on line 190 in messaginginapp/src/main/java/io/customer/messaginginapp/gist/presentation/GistSdk.kt

View check run for this annotation

Codecov / codecov/patch

messaginginapp/src/main/java/io/customer/messaginginapp/gist/presentation/GistSdk.kt#L190

Added line #L190 was not covered by tests
// Clean up any previous observers
observeUserMessagesJob?.cancel()

Log.i(GIST_TAG, "Messages timer started")
gistQueue.fetchUserMessages()
if (!skipQueueCheck) {
gistQueue.fetchUserMessages()

Check warning on line 196 in messaginginapp/src/main/java/io/customer/messaginginapp/gist/presentation/GistSdk.kt

View check run for this annotation

Codecov / codecov/patch

messaginginapp/src/main/java/io/customer/messaginginapp/gist/presentation/GistSdk.kt#L196

Added line #L196 was not covered by tests
}
observeUserMessagesJob = GlobalScope.launch {
try {
// Poll for user messages
val ticker = ticker(POLL_INTERVAL, context = this.coroutineContext)
val ticker = ticker(pollInterval, context = this.coroutineContext)

Check warning on line 201 in messaginginapp/src/main/java/io/customer/messaginginapp/gist/presentation/GistSdk.kt

View check run for this annotation

Codecov / codecov/patch

messaginginapp/src/main/java/io/customer/messaginginapp/gist/presentation/GistSdk.kt#L201

Added line #L201 was not covered by tests
for (tick in ticker) {
gistQueue.fetchUserMessages()
}
Expand Down
Loading