Fix backoff interval scheduling for jobs.

This commit is contained in:
Greyson Parrelli
2023-09-18 12:06:42 -04:00
parent bc8eb44a53
commit 8bad476315
6 changed files with 16 additions and 12 deletions

View File

@@ -137,6 +137,7 @@ import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
import org.thoughtcrime.securesms.events.ReminderUpdateEvent;
import org.thoughtcrime.securesms.exporter.flow.SmsExportDialogs;
import org.thoughtcrime.securesms.groups.SelectionLimits;
import org.thoughtcrime.securesms.jobs.MessageFetchJob;
import org.thoughtcrime.securesms.jobs.ServiceOutageDetectionJob;
import org.thoughtcrime.securesms.keyvalue.SignalStore;
import org.thoughtcrime.securesms.lock.v2.CreateSvrPinActivity;

View File

@@ -213,12 +213,13 @@ class JobDatabase(
}
@Synchronized
fun updateJobAfterRetry(id: String, isRunning: Boolean, runAttempt: Int, nextBackoffInterval: Long, serializedData: ByteArray?) {
fun updateJobAfterRetry(id: String, currentTime: Long, runAttempt: Int, nextBackoffInterval: Long, serializedData: ByteArray?) {
writableDatabase
.update(Jobs.TABLE_NAME)
.values(
Jobs.IS_RUNNING to if (isRunning) 1 else 0,
Jobs.IS_RUNNING to 0,
Jobs.RUN_ATTEMPT to runAttempt,
Jobs.LAST_RUN_ATTEMPT_TIME to currentTime,
Jobs.NEXT_BACKOFF_INTERVAL to nextBackoffInterval,
Jobs.SERIALIZED_DATA to serializedData
)

View File

@@ -253,7 +253,7 @@ class JobController {
int nextRunAttempt = job.getRunAttempt() + 1;
byte[] serializedData = job.serialize();
jobStorage.updateJobAfterRetry(job.getId(), false, nextRunAttempt, backoffInterval, serializedData);
jobStorage.updateJobAfterRetry(job.getId(), System.currentTimeMillis(), nextRunAttempt, backoffInterval, serializedData);
jobTracker.onStateChange(job, JobTracker.JobState.PENDING);
List<Constraint> constraints = Stream.of(jobStorage.getConstraintSpecs(job.getId()))

View File

@@ -40,7 +40,7 @@ public interface JobStorage {
void markJobAsRunning(@NonNull String id, long currentTime);
@WorkerThread
void updateJobAfterRetry(@NonNull String id, boolean isRunning, int runAttempt, long nextRunAttemptTime, @Nullable byte[] serializedData);
void updateJobAfterRetry(@NonNull String id, long currentTime, int runAttempt, long nextBackoffInterval, @Nullable byte[] serializedData);
@WorkerThread
void updateAllJobsToBePending();

View File

@@ -150,10 +150,10 @@ class FastJobStorage(private val jobDatabase: JobDatabase) : JobStorage {
}
@Synchronized
override fun updateJobAfterRetry(id: String, isRunning: Boolean, runAttempt: Int, nextBackoffInterval: Long, serializedData: ByteArray?) {
override fun updateJobAfterRetry(id: String, currentTime: Long, runAttempt: Int, nextBackoffInterval: Long, serializedData: ByteArray?) {
val job = getJobById(id)
if (job == null || !job.isMemoryOnly) {
jobDatabase.updateJobAfterRetry(id, isRunning, runAttempt, nextBackoffInterval, serializedData)
jobDatabase.updateJobAfterRetry(id, currentTime, runAttempt, nextBackoffInterval, serializedData)
}
val iter = jobs.listIterator()
@@ -162,8 +162,9 @@ class FastJobStorage(private val jobDatabase: JobDatabase) : JobStorage {
if (current.id == id) {
iter.set(
current.copy(
isRunning = isRunning,
isRunning = false,
runAttempt = runAttempt,
lastRunAttemptTime = currentTime,
nextBackoffInterval = nextBackoffInterval,
serializedData = serializedData
)

View File

@@ -194,13 +194,13 @@ class FastJobStorageTest {
subject.updateJobAfterRetry(
id = "id1",
isRunning = true,
currentTime = 0,
runAttempt = 1,
nextBackoffInterval = 10,
serializedData = "a".toByteArray()
)
Mockito.verify(database).updateJobAfterRetry(id = "id1", isRunning = true, runAttempt = 1, nextBackoffInterval = 10, serializedData = "a".toByteArray())
Mockito.verify(database).updateJobAfterRetry(id = "id1", currentTime = 0, runAttempt = 1, nextBackoffInterval = 10, serializedData = "a".toByteArray())
}
@Test
@@ -212,13 +212,13 @@ class FastJobStorageTest {
subject.updateJobAfterRetry(
id = "id1",
isRunning = true,
currentTime = 0,
runAttempt = 1,
nextBackoffInterval = 10,
serializedData = "a".toByteArray()
)
Mockito.verify(database, Mockito.times(0)).updateJobAfterRetry(id = "id1", isRunning = true, runAttempt = 1, nextBackoffInterval = 10, serializedData = "a".toByteArray())
Mockito.verify(database, Mockito.times(0)).updateJobAfterRetry(id = "id1", currentTime = 0, runAttempt = 1, nextBackoffInterval = 10, serializedData = "a".toByteArray())
}
@Test
@@ -230,7 +230,7 @@ class FastJobStorageTest {
subject.updateJobAfterRetry(
id = "1",
isRunning = false,
currentTime = 3,
runAttempt = 1,
nextBackoffInterval = 10,
serializedData = "a".toByteArray()
@@ -239,6 +239,7 @@ class FastJobStorageTest {
val job = subject.getJobSpec("1")
check(job != null)
job.isRunning assertIs false
job.lastRunAttemptTime assertIs 3
job.runAttempt assertIs 1
job.nextBackoffInterval assertIs 10
job.serializedData!!.toString(Charset.defaultCharset()) assertIs "a"