Skip to content

(fix):potential fix for multi scheduling of event work #268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,11 @@ void reschedule(@NonNull Context context, @NonNull Intent broadcastIntent, @NonN
// with wifi the service will be rescheduled on the interval.
// Wifi connection state changes all the time and starting services is expensive
// so it's important to only do this if we have stored events.
ServiceScheduler.startService(context, EventIntentService.JOB_ID, eventServiceIntent);
logger.info("Preemptively flushing events since wifi became available");
// In android O and higher, we use a persistent job so we do not need to restart.
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not schedule on Android O?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm adding a comment. But, it is because in Android O and higher, we use the job scheduler and make the job persistent. this means that it will restart itself.

ServiceScheduler.startService(context, EventIntentService.JOB_ID, eventServiceIntent);
logger.info("Preemptively flushing events since wifi became available");
}
}
} else {
logger.warn("Received unsupported broadcast action to event rescheduler");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.stream.Collectors;

import static android.app.job.JobScheduler.RESULT_SUCCESS;

/**
Expand Down Expand Up @@ -273,6 +275,12 @@ public static void startService(Context context, Integer jobId, Intent intent) {
.build();
JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);

if (jobScheduler.getAllPendingJobs().stream().filter(job -> job.getId() == jobId && job.getExtras().equals(intent.getExtras())).count() > 0) {
// already pending job. don't run again
LoggerFactory.getLogger("ServiceScheduler").info("Job already pending");
return;
}

JobWorkItem jobWorkItem = new JobWorkItem(intent);
try {
jobScheduler.enqueue(jobInfo, jobWorkItem);
Expand Down