Skip to content

Commit c785d6f

Browse files
enqueue failed uploads for a retry
1 parent 0a64989 commit c785d6f

File tree

2 files changed

+63
-76
lines changed

2 files changed

+63
-76
lines changed

app/src/main/java/fr/free/nrw/commons/contributions/MainActivity.java

-16
Original file line numberDiff line numberDiff line change
@@ -370,21 +370,6 @@ public boolean onOptionsItemSelected(MenuItem item) {
370370
}
371371
}
372372

373-
/**
374-
* Retry all failed uploads as soon as the user returns to the app
375-
*/
376-
@SuppressLint("CheckResult")
377-
private void retryAllFailedUploads() {
378-
contributionDao.
379-
getContribution(Collections.singletonList(Contribution.STATE_FAILED))
380-
.subscribeOn(Schedulers.io())
381-
.subscribe(failedUploads -> {
382-
for (Contribution contribution: failedUploads) {
383-
contributionsFragment.retryUpload(contribution);
384-
}
385-
});
386-
}
387-
388373
public void toggleLimitedConnectionMode() {
389374
defaultKvStore.putBoolean(CommonsApplication.IS_LIMITED_CONNECTION_MODE_ENABLED,
390375
!defaultKvStore
@@ -440,7 +425,6 @@ protected void onResume() {
440425
WelcomeActivity.startYourself(this);
441426
}
442427

443-
retryAllFailedUploads();
444428
}
445429

446430
@Override

app/src/main/java/fr/free/nrw/commons/upload/worker/UploadWorker.kt

+63-60
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ class UploadWorker(var appContext: Context, workerParams: WorkerParameters) :
105105

106106
statesToProcess.add(Contribution.STATE_QUEUED)
107107
statesToProcess.add(Contribution.STATE_QUEUED_LIMITED_CONNECTION_MODE)
108+
statesToProcess.add(Contribution.STATE_FAILED)
108109
}
109110

110111
@dagger.Module
@@ -164,75 +165,77 @@ class UploadWorker(var appContext: Context, workerParams: WorkerParameters) :
164165
}
165166

166167
override suspend fun doWork(): Result {
167-
try {
168-
var countUpload = 0
169-
notificationManager = NotificationManagerCompat.from(appContext)
170-
val processingUploads = getNotificationBuilder(
171-
CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL
172-
)!!
173-
withContext(Dispatchers.IO) {
174-
val queuedContributions = contributionDao.getContribution(statesToProcess)
175-
.blockingGet()
176-
//Showing initial notification for the number of uploads being processed
177-
178-
Timber.e("Queued Contributions: " + queuedContributions.size)
179-
180-
processingUploads.setContentTitle(appContext.getString(R.string.starting_uploads))
181-
processingUploads.setContentText(
182-
appContext.resources.getQuantityString(
183-
R.plurals.starting_multiple_uploads,
184-
queuedContributions.size,
185-
queuedContributions.size
186-
)
187-
)
188-
notificationManager?.notify(
189-
PROCESSING_UPLOADS_NOTIFICATION_TAG,
190-
PROCESSING_UPLOADS_NOTIFICATION_ID,
191-
processingUploads.build()
168+
var countUpload = 0
169+
notificationManager = NotificationManagerCompat.from(appContext)
170+
val processingUploads = getNotificationBuilder(
171+
CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL
172+
)!!
173+
withContext(Dispatchers.IO) {
174+
val queuedContributions = contributionDao.getContribution(statesToProcess)
175+
.blockingGet()
176+
//Showing initial notification for the number of uploads being processed
177+
178+
Timber.e("Queued Contributions: " + queuedContributions.size)
179+
180+
processingUploads.setContentTitle(appContext.getString(R.string.starting_uploads))
181+
processingUploads.setContentText(
182+
appContext.resources.getQuantityString(
183+
R.plurals.starting_multiple_uploads,
184+
queuedContributions.size,
185+
queuedContributions.size
192186
)
187+
)
188+
notificationManager?.notify(
189+
PROCESSING_UPLOADS_NOTIFICATION_TAG,
190+
PROCESSING_UPLOADS_NOTIFICATION_ID,
191+
processingUploads.build()
192+
)
193193

194+
/**
195+
* To avoid race condition when multiple of these workers are working, assign this state
196+
so that the next one does not process these contribution again
197+
*/
198+
queuedContributions.forEach {
199+
it.state = Contribution.STATE_IN_PROGRESS
200+
contributionDao.saveSynchronous(it)
201+
}
202+
203+
queuedContributions.asFlow().map { contribution ->
194204
/**
195-
* To avoid race condition when multiple of these workers are working, assign this state
196-
so that the next one does not process these contribution again
205+
* If the limited connection mode is on, lets iterate through the queued
206+
* contributions
207+
* and set the state as STATE_QUEUED_LIMITED_CONNECTION_MODE ,
208+
* otherwise proceed with the upload
197209
*/
198-
queuedContributions.forEach {
199-
it.state = Contribution.STATE_IN_PROGRESS
200-
contributionDao.saveSynchronous(it)
201-
}
202-
203-
queuedContributions.asFlow().map { contribution ->
204-
/**
205-
* If the limited connection mode is on, lets iterate through the queued
206-
* contributions
207-
* and set the state as STATE_QUEUED_LIMITED_CONNECTION_MODE ,
208-
* otherwise proceed with the upload
209-
*/
210-
if (isLimitedConnectionModeEnabled()) {
211-
if (contribution.state == Contribution.STATE_QUEUED) {
212-
contribution.state = Contribution.STATE_QUEUED_LIMITED_CONNECTION_MODE
213-
contributionDao.saveSynchronous(contribution)
214-
}
215-
} else {
216-
contribution.transferred = 0
217-
contribution.state = Contribution.STATE_IN_PROGRESS
210+
if (isLimitedConnectionModeEnabled()) {
211+
if (contribution.state == Contribution.STATE_QUEUED) {
212+
contribution.state = Contribution.STATE_QUEUED_LIMITED_CONNECTION_MODE
218213
contributionDao.saveSynchronous(contribution)
219-
setProgressAsync(Data.Builder().putInt("progress", countUpload).build())
220-
countUpload++
221-
uploadContribution(contribution = contribution)
222214
}
223-
}.collect()
215+
} else {
216+
contribution.transferred = 0
217+
contribution.state = Contribution.STATE_IN_PROGRESS
218+
contributionDao.saveSynchronous(contribution)
219+
setProgressAsync(Data.Builder().putInt("progress", countUpload).build())
220+
countUpload++
221+
uploadContribution(contribution = contribution)
222+
}
223+
}.collect()
224224

225-
//Dismiss the global notification
226-
notificationManager?.cancel(
227-
PROCESSING_UPLOADS_NOTIFICATION_TAG,
228-
PROCESSING_UPLOADS_NOTIFICATION_ID
229-
)
230-
}
231-
//TODO make this smart, think of handling retries in the future
232-
return Result.success()
233-
} catch (e: Exception) {
225+
//Dismiss the global notification
226+
notificationManager?.cancel(
227+
PROCESSING_UPLOADS_NOTIFICATION_TAG,
228+
PROCESSING_UPLOADS_NOTIFICATION_ID
229+
)
230+
}
231+
232+
// Retry if any new contribution is added to the queue
233+
val updatedContributionsQueue = contributionDao.getContribution(statesToProcess).blockingGet()
234+
if (updatedContributionsQueue.isNotEmpty()) {
234235
return Result.retry()
235236
}
237+
238+
return Result.success()
236239
}
237240

238241
override suspend fun getForegroundInfo(): ForegroundInfo {

0 commit comments

Comments
 (0)