WorkManager's 15 minute PeriodicWorkRequest doesn't run for 6+ hours straight

Viewed 633

Similar question: Android workmanager scheduled worker lost after task killed
The answer here suggests that this is a problem with specific manufacturers, but I am using an Android One phone which has the unmodified OS.

For the most part my PeriodicWorkRequest works as expected, but then for a straight 5-8 hours window, it doesn't get executed at all. I log whenever the request is executed in a log file, and this is how it looks:

enter image description here

As visible in the image, the red box indicates the interval where WorkManager did not run the request. I can assure that the app was never force killed since install. It was cleared from recents some times, but that did not seem to have any immediate effects.

This is how I initialized the request:

PeriodicWorkRequest checkLastSeenRequest =
                new PeriodicWorkRequest.Builder(LastSeenWorker.class,
                        PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS, TimeUnit.MILLISECONDS,
                        PeriodicWorkRequest.MIN_PERIODIC_FLEX_MILLIS, TimeUnit.MILLISECONDS)
                        .setBackoffCriteria(
                                BackoffPolicy.LINEAR,
                                PeriodicWorkRequest.MIN_BACKOFF_MILLIS,
                                TimeUnit.MILLISECONDS)
                        .build();
        WorkManager.getInstance(this).enqueueUniquePeriodicWork(
                "checkLastSeen",
                ExistingPeriodicWorkPolicy.KEEP,
                checkLastSeenRequest);

And this is the createWork() method of RxWorker:

@NonNull
@Override
public Single<Result> createWork() {

    return Single.create(subscriber -> {
        FirebaseFirestore firestore = FirebaseFirestore.getInstance();
        firestore.collection("users").document("doc").get()
                .addOnCompleteListener(task -> {
                    if (task.isSuccessful()) {

                        DocumentSnapshot doc = task.getResult();

                        if (doc != null && doc.exists() && doc.getData() != null
                                    && doc.getData().get("lastSeen") != null) {

-->                         // this is where I log

                            subscriber.onSuccess(Result.success());
                        } else {
-->                         XLog.e("No document found to read from");
                            subscriber.onError(new Error("Document not found"));
                        }
                    } else {
-->                     XLog.e(task.getException());
                        subscriber.onError(task.getException());
                    }
                });
    });
}

From the documentation here my understanding is that in doze mode as well there is a window where all the requests are allowed to execute even as these become less frequent. So why is there a complete blackout for more than 6 hours? Do I need to get battery optimization permissions for it to work as expected? Any help appreciated!

1 Answers
  1. try to check working your code in the Doze mode as described it here

Maintenance windows in Doze won’t happen frequently. First window occurs an hour after last activity, the next after two, the next after four and so on. But it doesn’t matter that much since user isn’t even moving her device anyway.

Some useful information

  1. Maybe you have another choice: Schedule tasks with WorkManager
Related