From 8827b4b5ef19960d4526128b0b5654be952536f2 Mon Sep 17 00:00:00 2001 From: Valere Date: Fri, 13 Mar 2020 15:48:00 +0100 Subject: [PATCH] Fix / correctly update local echo failed state + endure Workmanager queues are reset after a clear cache --- .../android/internal/session/DefaultSession.kt | 1 + .../session/content/UploadContentWorker.kt | 10 ++++++++++ .../send/MultipleEventSendingDispatcherWorker.kt | 15 ++++++++++----- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultSession.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultSession.kt index 84b76345c8..25dc939196 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultSession.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultSession.kt @@ -193,6 +193,7 @@ internal class DefaultSession @Inject constructor( stopAnyBackgroundSync() liveEntityObservers.forEach { it.cancelProcess() } cacheService.get().clearCache(callback) + workManagerProvider.cancelAllWorks() } @Subscribe(threadMode = ThreadMode.MAIN) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/UploadContentWorker.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/UploadContentWorker.kt index 1dde25fd78..1c88f87804 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/UploadContentWorker.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/UploadContentWorker.kt @@ -77,6 +77,16 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter return Result.success(inputData) } + // Just defensive code to ensure that we never have an uncaught exception that could break the queue + return try { + internalDoWork(params) + } catch (failure: Throwable) { + Timber.e(failure) + handleFailure(params, failure) + } + } + + private suspend fun internalDoWork(params: Params): Result { val sessionComponent = getSessionComponent(params.sessionId) ?: return Result.success() sessionComponent.inject(this) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/MultipleEventSendingDispatcherWorker.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/MultipleEventSendingDispatcherWorker.kt index 03db817dd6..8c31dd1682 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/MultipleEventSendingDispatcherWorker.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/MultipleEventSendingDispatcherWorker.kt @@ -23,6 +23,7 @@ import androidx.work.OneTimeWorkRequest import androidx.work.WorkerParameters import com.squareup.moshi.JsonClass import im.vector.matrix.android.api.session.events.model.Event +import im.vector.matrix.android.api.session.room.send.SendState import im.vector.matrix.android.internal.di.WorkManagerProvider import im.vector.matrix.android.internal.session.room.timeline.TimelineSendEventWorkCommon import im.vector.matrix.android.internal.worker.SessionWorkerParams @@ -49,6 +50,7 @@ internal class MultipleEventSendingDispatcherWorker(context: Context, params: Wo @Inject lateinit var workManagerProvider: WorkManagerProvider @Inject lateinit var timelineSendEventWorkCommon: TimelineSendEventWorkCommon + @Inject lateinit var localEchoUpdater: LocalEchoUpdater override suspend fun doWork(): Result { Timber.v("Start dispatch sending multiple event work") @@ -57,14 +59,17 @@ internal class MultipleEventSendingDispatcherWorker(context: Context, params: Wo Timber.e("Work cancelled due to input error from parent") } - if (params.lastFailureMessage != null) { - // Transmit the error - return Result.success(inputData) - } - val sessionComponent = getSessionComponent(params.sessionId) ?: return Result.success() sessionComponent.inject(this) + if (params.lastFailureMessage != null) { + params.events.forEach { event -> + event.eventId?.let { localEchoUpdater.updateSendState(it, SendState.UNDELIVERED) } + } + // Transmit the error if needed? + return Result.success(inputData) + } + // Create a work for every event params.events.forEach { event -> if (params.isEncrypted) {