Synchronize access to refresh scheduler

pull/757/head
Alex Baker 6 years ago
parent 48b5e18062
commit 944a112dbc

@ -1,6 +1,5 @@
package org.tasks.scheduling; package org.tasks.scheduling;
import static com.google.common.collect.Lists.newArrayList;
import static com.todoroo.andlib.utility.DateUtilities.ONE_MINUTE; import static com.todoroo.andlib.utility.DateUtilities.ONE_MINUTE;
import static org.tasks.time.DateTimeUtils.currentTimeMillis; import static org.tasks.time.DateTimeUtils.currentTimeMillis;
@ -22,7 +21,7 @@ public class RefreshScheduler {
this.workManager = workManager; this.workManager = workManager;
} }
public void scheduleRefresh(Task task) { public synchronized void scheduleRefresh(Task task) {
if (task.isCompleted()) { if (task.isCompleted()) {
scheduleRefresh(task.getCompletionDate() + ONE_MINUTE); scheduleRefresh(task.getCompletionDate() + ONE_MINUTE);
} else if (task.hasDueDate()) { } else if (task.hasDueDate()) {
@ -33,26 +32,21 @@ public class RefreshScheduler {
} }
} }
private void scheduleRefresh(Long refreshTime) { private void scheduleRefresh(Long timestamp) {
long now = currentTimeMillis(); long now = currentTimeMillis();
if (now < refreshTime) { if (now < timestamp) {
refreshTime += 1000; // this is ghetto SortedSet<Long> upcoming = jobs.tailSet(now);
schedule(refreshTime); boolean reschedule = upcoming.isEmpty() || timestamp < upcoming.first();
jobs.add(timestamp);
if (reschedule) {
scheduleNext();
}
} }
} }
private void schedule(long timestamp) { public synchronized void scheduleNext() {
SortedSet<Long> upcoming = jobs.tailSet(currentTimeMillis()); SortedSet<Long> lapsed = jobs.headSet(currentTimeMillis() + 1);
boolean reschedule = upcoming.isEmpty() || timestamp < upcoming.first(); jobs.removeAll(lapsed);
jobs.add(timestamp);
if (reschedule) {
scheduleNext();
}
}
public void scheduleNext() {
long now = currentTimeMillis();
jobs.removeAll(newArrayList(jobs.headSet(now + 1)));
if (!jobs.isEmpty()) { if (!jobs.isEmpty()) {
workManager.scheduleRefresh(jobs.first()); workManager.scheduleRefresh(jobs.first());
} }

Loading…
Cancel
Save