Skip to content
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
3 changes: 2 additions & 1 deletion Network/Ktor/src/main/kotlin/CreateHttpClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import io.ktor.client.statement.HttpResponse
import io.ktor.client.statement.request
import io.ktor.http.ContentType
import io.ktor.http.HttpHeaders
import io.ktor.http.isSuccess
import io.ktor.serialization.kotlinx.json.json
import kotlinx.serialization.SerializationException
import kotlinx.serialization.json.Json
Expand All @@ -56,7 +57,7 @@ fun createHttpClient(
}
HttpResponseValidator {
validateResponse { response ->
response.validateContentType { accepted, received ->
if (response.status.isSuccess()) response.validateContentType { accepted, received ->
Comment thread
LouisCAD marked this conversation as resolved.
Resolved
val url = response.request.url
val method = response.request.method
throw IllegalArgumentException("Expected Content-Type $accepted but got $received from $method on $url")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import androidx.work.CoroutineWorker
import androidx.work.ExistingWorkPolicy
import androidx.work.NetworkType
import androidx.work.OneTimeWorkRequest
import androidx.work.WorkInfo
import androidx.work.WorkManager
import androidx.work.WorkerParameters
import androidx.work.await
Expand All @@ -40,6 +41,7 @@ import io.ktor.http.isSuccess
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.tasks.await
import okhttp3.OkHttpClient
Expand Down Expand Up @@ -75,6 +77,9 @@ abstract class AbstractNotificationsRegistrationWorker(
}.awaitAll()
return if (outcomes.any { it == Outcome.ShouldRetry }) {
Result.retry()
} else if (outcomes.any { it == Outcome.Failed }) {
SentryLog.i(TAG, "Work failed")
Result.failure()
} else {
SentryLog.i(TAG, "Work finished")
Result.success()
Expand Down Expand Up @@ -128,9 +133,10 @@ abstract class AbstractNotificationsRegistrationWorker(
return workRequest.id
}

suspend fun awaitWorkerCompletion(uuid: UUID) {
suspend fun awaitWorkerCompletion(uuid: UUID): Boolean {
val workManager = WorkManager.getInstance(appCtx)
workManager.getWorkInfoByIdFlow(uuid).first { it?.state?.isFinished == true }
val workInfo = workManager.getWorkInfoByIdFlow(uuid).filterNotNull().first { it.state.isFinished }
return workInfo.state != WorkInfo.State.FAILED
}

@PublishedApi
Expand All @@ -140,6 +146,7 @@ abstract class AbstractNotificationsRegistrationWorker(

private enum class Outcome {
Done,
Failed,
ShouldRetry,
}

Expand All @@ -155,11 +162,11 @@ private fun handleUnsuccessfulResponse(response: HttpResponse): Outcome {
}
401 -> {
SentryLog.w(TAG, errorMessage)
Outcome.Done
Outcome.Failed
}
else -> {
SentryLog.wtf(TAG, errorMessage)
Outcome.Done
Outcome.Failed
}
}
}
Expand All @@ -171,6 +178,6 @@ private fun handleNetworkOperationFailure(t: Throwable): Outcome {
Outcome.ShouldRetry
} else {
SentryLog.wtf(TAG, errorMessage, t)
Outcome.Done
Outcome.Failed
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ object NotificationsRegistrationManager : AssociatedUserDataCleanable {

latestFcmToken.collectLatest { fcmToken ->
userIdsFlow.collectLatest { userIds ->
while (true) {
do {
awaitAnyNeedForUpdate(userIds, fcmToken, latestNotificationTopics)
val id = AbstractNotificationsRegistrationWorker.schedule(workerClass)
AbstractNotificationsRegistrationWorker.awaitWorkerCompletion(id) // Don't loop immediately.
}
val didNotFail = AbstractNotificationsRegistrationWorker.awaitWorkerCompletion(id) // Don't loop immediately.
} while (didNotFail)
Comment thread
LouisCAD marked this conversation as resolved.
Resolved
}
}
}
Expand Down
Loading