Fix bug related to in-memory jobs not being prioritized properly.

This commit is contained in:
Greyson Parrelli
2025-10-24 13:37:15 -04:00
committed by jeffrey-signal
parent 9ed921f58c
commit c25ce2bcdd
2 changed files with 33 additions and 6 deletions

View File

@@ -356,10 +356,15 @@ class FastJobStorage(private val jobDatabase: JobDatabase) : JobStorage {
mostEligibleJobForQueue.keys.removeAll(affectedQueues)
for (queue in affectedQueues) {
jobDatabase.getMostEligibleJobInQueue(queue)?.let {
jobSpecCache[it.id] = it
placeJobInEligibleList(it.toMinimalJobSpec())
}
minimalJobs
.filter { it.queueKey == queue }
.minWithOrNull(
compareByDescending<MinimalJobSpec> { it.globalPriority }
.thenByDescending { it.queuePriority }
.thenBy { it.createTime }
.thenBy { it.id }
)
?.let { placeJobInEligibleList(it) }
}
for (jobId in ids) {

View File

@@ -863,6 +863,28 @@ class FastJobStorageTest {
verify(exactly = 0) { database.deleteJobs(ids) }
}
@Test
fun `deleteJobs - memory-only job remains in mostEligibleJobForQueue after deleting other job in same queue`() {
// This test targets a case where we weren't handling in-memory jobs when calculating mostEligiibleForJobQueue after a deletion
val memoryJob = fullSpec(id = "memory-job", factoryKey = "f1", queueKey = "q1", isMemoryOnly = true, globalPriority = 10)
val durableJob = fullSpec(id = "durable-job", factoryKey = "f2", queueKey = "q1", isMemoryOnly = false, globalPriority = 5)
val database = mockDatabase(listOf(memoryJob, durableJob))
val subject = FastJobStorage(database)
subject.init()
// Verify memory job is initially the most eligible
val initialNext = subject.getNextEligibleJob(System.currentTimeMillis(), NO_PREDICATE)
assertThat(initialNext).isNotNull().prop(JobSpec::id).isEqualTo("memory-job")
// Delete the durable job
subject.deleteJobs(listOf("durable-job"))
// The memory job should still be available as the most eligible
val afterDelete = subject.getNextEligibleJob(System.currentTimeMillis(), NO_PREDICATE)
assertThat(afterDelete).isNotNull().prop(JobSpec::id).isEqualTo("memory-job")
}
@Test
fun `deleteJobs - deletes all relevant pieces`() {
val subject = FastJobStorage(mockDatabase(DataSet1.FULL_SPECS))
@@ -1439,8 +1461,8 @@ class FastJobStorageTest {
return mock
}
private fun fullSpec(id: String, factoryKey: String, queueKey: String? = null): FullSpec {
return FullSpec(jobSpec(id, factoryKey, queueKey), emptyList(), emptyList())
private fun fullSpec(id: String, factoryKey: String, queueKey: String? = null, isMemoryOnly: Boolean = false, globalPriority: Int = 0): FullSpec {
return FullSpec(jobSpec(id, factoryKey, queueKey, isMemoryOnly = isMemoryOnly, globalPriority = globalPriority), emptyList(), emptyList())
}
private fun jobSpec(