Remove jobs one at a time

gtask_note_sync
Alex Baker 7 years ago
parent 2ee8fa9e4e
commit a274899dab

@ -142,7 +142,39 @@ public class JobQueueTest {
Freeze.freezeAt(now).thawAfter(new Snippet() {{ Freeze.freezeAt(now).thawAfter(new Snippet() {{
assertEquals( assertEquals(
singletonList(new Reminder(1, now, TYPE_DUE)), singletonList(new Reminder(1, now, TYPE_DUE)),
queue.removeOverdueJobs()); queue.getOverdueJobs());
}});
}
@Test
public void twoOverdueJobsAtSameTimeReturned() {
long now = currentTimeMillis();
queue.add(new Reminder(1, now, TYPE_DUE));
queue.add(new Reminder(2, now, TYPE_DUE));
verify(jobManager).schedule(TAG, now);
Freeze.freezeAt(now).thawAfter(new Snippet() {{
assertEquals(
asList(new Reminder(1, now, TYPE_DUE), new Reminder(2, now, TYPE_DUE)),
queue.getOverdueJobs());
}});
}
@Test
public void twoOverdueJobsAtDifferentTimes() {
long now = currentTimeMillis();
queue.add(new Reminder(1, now, TYPE_DUE));
queue.add(new Reminder(2, now + ONE_MINUTE, TYPE_DUE));
verify(jobManager).schedule(TAG, now);
Freeze.freezeAt(now + 2 * ONE_MINUTE).thawAfter(new Snippet() {{
assertEquals(
asList(new Reminder(1, now, TYPE_DUE), new Reminder(2, now + ONE_MINUTE, TYPE_DUE)),
queue.getOverdueJobs());
}}); }});
} }
@ -156,7 +188,7 @@ public class JobQueueTest {
verify(jobManager).schedule(TAG, now); verify(jobManager).schedule(TAG, now);
Freeze.freezeAt(now).thawAfter(new Snippet() {{ Freeze.freezeAt(now).thawAfter(new Snippet() {{
queue.removeOverdueJobs(); queue.remove(new Reminder(1, now, TYPE_DUE));
}}); }});
assertEquals( assertEquals(
@ -175,7 +207,8 @@ public class JobQueueTest {
verify(jobManager).schedule(TAG, now); verify(jobManager).schedule(TAG, now);
Freeze.freezeAt(now + ONE_MINUTE).thawAfter(new Snippet() {{ Freeze.freezeAt(now + ONE_MINUTE).thawAfter(new Snippet() {{
queue.removeOverdueJobs(); queue.remove(new Reminder(1, now, TYPE_DUE));
queue.remove(new Reminder(2, now + ONE_MINUTE, TYPE_DUE));
}}); }});
assertEquals( assertEquals(

@ -189,7 +189,11 @@ public class AlarmService {
jobs.scheduleNext(); jobs.scheduleNext();
} }
public List<Alarm> removePastDueAlarms() { public List<Alarm> getOverdueAlarms() {
return jobs.removeOverdueJobs(); return jobs.getOverdueJobs();
}
public boolean remove(Alarm alarm) {
return jobs.remove(alarm);
} }
} }

@ -97,7 +97,11 @@ public final class ReminderService {
} }
public List<Reminder> getPastReminders() { public List<Reminder> getPastReminders() {
return jobs.removeOverdueJobs(); return jobs.getOverdueJobs();
}
public boolean remove(Reminder reminder) {
return jobs.remove(reminder);
} }
public void scheduleNextJob() { public void scheduleNextJob() {

@ -29,11 +29,12 @@ public class AlarmJob extends WakefulJob {
@Override @Override
protected void run() { protected void run() {
if (!preferences.isCurrentlyQuietHours()) { if (!preferences.isCurrentlyQuietHours()) {
for (Alarm alarm : alarmService.removePastDueAlarms()) { for (Alarm alarm : alarmService.getOverdueAlarms()) {
Task task = taskDao.fetch(alarm.getTaskId(), Task.REMINDER_LAST); Task task = taskDao.fetch(alarm.getTaskId(), Task.REMINDER_LAST);
if (task != null && task.getReminderLast() < alarm.getTime()) { if (task != null && task.getReminderLast() < alarm.getTime()) {
notifier.triggerTaskNotification(alarm.getTaskId(), ReminderService.TYPE_ALARM); notifier.triggerTaskNotification(alarm.getTaskId(), ReminderService.TYPE_ALARM);
} }
alarmService.remove(alarm);
} }
} }
} }

@ -15,6 +15,7 @@ public abstract class Job extends InjectingIntentService {
public Job(String name) { public Job(String name) {
super(name); super(name);
setIntentRedelivery(true);
} }
@Override @Override

@ -62,15 +62,18 @@ public class JobQueue<T extends JobQueueEntry> {
} }
} }
public synchronized List<T> removeOverdueJobs() { public synchronized List<T> getOverdueJobs() {
List<T> result = newArrayList(); List<T> result = newArrayList();
SortedSet<Long> lapsed = jobs.keySet().headSet(currentTimeMillis() + 1); for (Long key : jobs.keySet().headSet(currentTimeMillis() + 1)) {
for (Long key : ImmutableSortedSet.copyOf(lapsed)) { result.addAll(jobs.get(key));
result.addAll(jobs.removeAll(key));
} }
return result; return result;
} }
public synchronized boolean remove(T entry) {
return jobs.remove(entry.getTime(), entry);
}
public synchronized void scheduleNext() { public synchronized void scheduleNext() {
scheduleNext(false); scheduleNext(false);
} }

@ -32,6 +32,7 @@ public class ReminderJob extends WakefulJob {
if (!preferences.isCurrentlyQuietHours()) { if (!preferences.isCurrentlyQuietHours()) {
for (Reminder reminder : reminderService.getPastReminders()) { for (Reminder reminder : reminderService.getPastReminders()) {
notifier.triggerTaskNotification(reminder.getId(), reminder.getType()); notifier.triggerTaskNotification(reminder.getId(), reminder.getType());
reminderService.remove(reminder);
} }
} }
} }

Loading…
Cancel
Save