Skip to content

Commit

Permalink
fix retried uploads stuck in queued state
Browse files Browse the repository at this point in the history
  • Loading branch information
RitikaPahwa4444 committed Aug 27, 2023
1 parent f561b2c commit 7f22ab4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.free.nrw.commons.contributions;

import androidx.work.BackoffPolicy;
import androidx.work.ExistingWorkPolicy;
import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkManager;
Expand All @@ -13,6 +14,7 @@
import io.reactivex.functions.Consumer;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
import javax.inject.Named;

Expand Down Expand Up @@ -77,10 +79,14 @@ public void saveContribution(Contribution contribution) {
.save(contribution)
.subscribeOn(ioThreadScheduler)
.subscribe(() -> {
OneTimeWorkRequest updatedUploadRequest = new OneTimeWorkRequest
.Builder(UploadWorker.class)
.setBackoffCriteria(BackoffPolicy.LINEAR, 10, TimeUnit.SECONDS)
.build();
WorkManager.getInstance(view.getContext().getApplicationContext())
.enqueueUniqueWork(
UploadWorker.class.getSimpleName(),
ExistingWorkPolicy.KEEP, OneTimeWorkRequest.from(UploadWorker.class));
ExistingWorkPolicy.KEEP, updatedUploadRequest);
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.work.BackoffPolicy;
import androidx.work.ExistingWorkPolicy;
import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkManager;
Expand Down Expand Up @@ -47,6 +48,7 @@
import fr.free.nrw.commons.upload.worker.UploadWorker;
import fr.free.nrw.commons.utils.PermissionUtils;
import fr.free.nrw.commons.utils.ViewUtilWrapper;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
import javax.inject.Named;
import timber.log.Timber;
Expand Down Expand Up @@ -369,9 +371,13 @@ public void toggleLimitedConnectionMode() {
viewUtilWrapper
.showShortToast(getBaseContext(), getString(R.string.limited_connection_enabled));
} else {
OneTimeWorkRequest restartUploadsRequest = new OneTimeWorkRequest
.Builder(UploadWorker.class)
.setBackoffCriteria(BackoffPolicy.LINEAR, 10, TimeUnit.SECONDS)
.build();
WorkManager.getInstance(getApplicationContext()).enqueueUniqueWork(
UploadWorker.class.getSimpleName(),
ExistingWorkPolicy.APPEND_OR_REPLACE, OneTimeWorkRequest.from(UploadWorker.class));
ExistingWorkPolicy.APPEND_OR_REPLACE, restartUploadsRequest);

viewUtilWrapper
.showShortToast(getBaseContext(), getString(R.string.limited_connection_disabled));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.work.BackoffPolicy;
import androidx.work.ExistingWorkPolicy;
import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkManager;
Expand Down Expand Up @@ -57,6 +58,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
import javax.inject.Named;
import timber.log.Timber;
Expand Down Expand Up @@ -317,9 +319,13 @@ public void updateTopCardTitle() {

@Override
public void makeUploadRequest() {
OneTimeWorkRequest uploadRequest = new OneTimeWorkRequest
.Builder(UploadWorker.class)
.setBackoffCriteria(BackoffPolicy.LINEAR, 10, TimeUnit.SECONDS)
.build();
WorkManager.getInstance(getApplicationContext()).enqueueUniqueWork(
UploadWorker.class.getSimpleName(),
ExistingWorkPolicy.APPEND_OR_REPLACE, OneTimeWorkRequest.from(UploadWorker.class));
ExistingWorkPolicy.APPEND_OR_REPLACE, uploadRequest);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,13 @@ class UploadWorker(var appContext: Context, workerParams: WorkerParameters) :
PROCESSING_UPLOADS_NOTIFICATION_ID
)
}
// Trigger WorkManager to process any new contributions that may have been added to the queue
val updatedContributionQueue = withContext(Dispatchers.IO) {
contributionDao.getContribution(statesToProcess).blockingGet()
}
if (updatedContributionQueue.isNotEmpty()) {
return Result.retry()
}
//TODO make this smart, think of handling retries in the future
return Result.success()
}
Expand Down

0 comments on commit 7f22ab4

Please sign in to comment.