diff --git a/app/src/main/java/org/thoughtcrime/securesms/jobs/FastJobStorage.java b/app/src/main/java/org/thoughtcrime/securesms/jobs/FastJobStorage.java index 6c184beb6..d597b8da2 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/jobs/FastJobStorage.java +++ b/app/src/main/java/org/thoughtcrime/securesms/jobs/FastJobStorage.java @@ -105,9 +105,25 @@ public class FastJobStorage implements JobStorage { return Collections.emptyList(); } else { return Stream.of(jobs) + .groupBy(jobSpec -> { + String queueKey = jobSpec.getQueueKey(); + if (queueKey != null) { + return queueKey; + } else { + return jobSpec.getId(); + } + }) + .map(byQueueKey -> + Stream.of(byQueueKey.getValue()).sorted((j1, j2) -> Long.compare(j1.getCreateTime(), j2.getCreateTime())) + .findFirst() + .orElse(null) + ) + .withoutNulls() + .filter(j -> { + List dependencies = dependenciesByJobId.get(j.getId()); + return dependencies == null || dependencies.isEmpty(); + }) .filterNot(JobSpec::isRunning) - .filter(this::firstInQueue) - .filter(j -> !dependenciesByJobId.containsKey(j.getId()) || dependenciesByJobId.get(j.getId()).isEmpty()) .filter(j -> j.getNextRunAttemptTime() <= currentTime) .sorted((j1, j2) -> Long.compare(j1.getCreateTime(), j2.getCreateTime())) .toList();