Avoid enqueueing multiple jobs to fetch updates

Due to the way we rely on AlarmManager as a cron system to schedule new jobs to
fetch updates from event loop, when offline, it might happen that
multiple instances of the same job (FetchUpdateJob) are enqueued one
after the other, resulting in duplicated API calls when back online.

Here, we cancel any pre-existing "FetchUpdateJob" job before adding a
new one to the queue.

MAILAND-3074
This commit is contained in:
Marino Meneghel 2023-04-24 17:46:24 +02:00 committed by Zorica Stojchevska
parent 4a237a77e8
commit 24ed833afb
2 changed files with 8 additions and 2 deletions

View File

@ -25,6 +25,7 @@ import androidx.annotation.NonNull;
import androidx.core.app.ProtonJobIntentService;
import com.birbit.android.jobqueue.JobManager;
import com.birbit.android.jobqueue.TagConstraint;
import javax.inject.Inject;
@ -70,6 +71,7 @@ public class EventUpdaterService extends ProtonJobIntentService {
private void startService() {
mJobManager.cancelJobs(TagConstraint.ALL, FetchUpdatesJob.FETCH_UPDATE_JOB_TAG);
mJobManager.addJob(new FetchUpdatesJob(eventManager));
alarmReceiver.setAlarm(getApplicationContext());
}

View File

@ -26,11 +26,10 @@ import ch.protonmail.android.jobs.ProtonMailBaseJob
import com.birbit.android.jobqueue.Params
import timber.log.Timber
import java.io.IOException
import java.net.ConnectException
import java.util.concurrent.TimeUnit
class FetchUpdatesJob internal constructor(private val eventManager: EventManager) : ProtonMailBaseJob(
Params(Priority.HIGH).requireNetwork()
Params(Priority.HIGH).addTags(FETCH_UPDATE_JOB_TAG).requireNetwork()
) {
constructor() : this(ProtonMailApplication.getApplication().eventManager)
@ -58,4 +57,9 @@ class FetchUpdatesJob internal constructor(private val eventManager: EventManage
}
override fun onProtonCancel(cancelReason: Int, throwable: Throwable?) {}
companion object {
internal const val FETCH_UPDATE_JOB_TAG = "FetchUpdateJobTag"
}
}