Create helper class for scheduling background tasks with best available system API

RESOLVED WONTFIX

Status

()

RESOLVED WONTFIX
3 years ago
9 months ago

People

(Reporter: sebastian, Assigned: k.krish)

Tracking

(Depends on: 2 bugs, Blocks: 2 bugs)

Firefox Tracking Flags

(Not tracked)

Details

Attachments

(1 obsolete attachment)

For synchronizing the catalog of downloadable content (bug 1201059) and content notifications (bug 1241810) we need to schedule repeating background tasks.

Android has various APIs for that depending on version and device configuration:

* AlarmManager
  http://developer.android.com/reference/android/app/AlarmManager.html
  The AlarmManager is available on all Android devices and versions. However it is very simple and we need to handle things like reboots or network availability ourselves.

* JobScheduler
  http://developer.android.com/reference/android/app/job/JobScheduler.html
  JobScheduler has a fine-grained configuration and handles most problems of scheduling background tasks. However it has been introduced in Android 5 and there's no official backported version available.

* GcmNetworkManager
  https://developers.google.com/android/reference/com/google/android/gms/gcm/GcmNetworkManager
  An API to schedule network-oriented tasks. It does almost all the heavy lifting for you but it's tied to GCM / Google Play Services and therefore not available on all devices.

It would be nice to have a generic - maybe JobScheduler-like - helper class that you can use to configure a background task and in the background this class uses the best available API (For example AlarmManager on Gingerbread - or GcmNetworkManager if available, but JobScheduler on 5.0).
nalexander: Before considering implementing something like that for bug 1201059 and bug 1241810 I was wondering if we might have some other background "tick" that I could use to start other tasks. To piggyback on another, already existing, background task might be a simple workaround.
Flags: needinfo?(nalexander)
(In reply to Sebastian Kaspari (:sebastian) from comment #1)
> nalexander: Before considering implementing something like that for bug
> 1201059 and bug 1241810 I was wondering if we might have some other
> background "tick" that I could use to start other tasks. To piggyback on
> another, already existing, background task might be a simple workaround.

I was not aware of JobScheduler or GcmNetworkManager.  rnewman built a scheduler based on AlarmManager and reboot intents for Product Announcements (now deceased) and I used that model for our FHR uploader (also deceased).

jchen and snorp (I think?) wrote a service to let Gecko schedule things like this, but I don't think it tries to address reboots, etc: https://dxr.mozilla.org/mozilla-central/source/mobile/android/base/java/org/mozilla/gecko/GeckoService.java.

I wish JobScheduler had a support library version :(  But failing that, perhaps we can dust off one of the existing solutions and try to generalize it for more tasks.
Flags: needinfo?(nalexander)
rnewman: do you have an opinion here?
Flags: needinfo?(rnewman)
(In reply to Nick Alexander :nalexander from comment #2)
> I wish JobScheduler had a support library version :(

Yeah, me too. Someone started this on Github a while ago but the library is already deprecated:
https://github.com/evant/JobSchedulerCompat

However maybe it's a good source of Dos and Donts if we are going to build our own.
Nope, no opinion. There were almost certainly comments in the code to the effect of "we should have one place that schedules all of these things" -- we'd like to respond to network state changes, timers, Gecko events, etc. -- so perhaps that's now this bug.
Flags: needinfo?(rnewman)
Blocks: 1253280
Blocks: 1253285

Comment 6

3 years ago
Created attachment 8727499 [details] [diff] [review]
Basic patch
Attachment #8727499 - Flags: review?(s.kaspari)
Comment on attachment 8727499 [details] [diff] [review]
Basic patch

Review of attachment 8727499 [details] [diff] [review]:
-----------------------------------------------------------------

This bug is about creating this very generic helper for scheduling all kinds of background tasks.

Periodically running DownloadContentService is something that gets more interesting after the sync client is in the tree (bug 1201059). After that we can think of running this service more often in the background (For this specific action).
Attachment #8727499 - Flags: review?(s.kaspari)
Attachment #8727499 - Attachment is obsolete: true
Depends on: 1257492
Blocks: 1257492
No longer depends on: 1257492
I got a larger list of JobScheduler compats: https://android-arsenal.com/search?q=jobscheduler

The two popular ones are:
 * Trigger (331 stars): https://github.com/airk000/Trigger
 * android-job (780 stars): https://github.com/evernote/android-job

The latter schedules over GcmNetworkManager, JobScheduler, and AlarmManager and is maintained by a company (Evernote), so is probably more reliable.

I think it'd be better to try to re-use one of these than rolling our own.
(In reply to Michael Comella (:mcomella) from comment #8)
> I think it'd be better to try to re-use one of these than rolling our own.

Definitely! Let's evaluate them before writing any code. I still have hopes that we can move from API 15 to 16 soon and just use JobScheduler. That's why I would prefer something that is very close to JobScheduler (and can have a "shitty" AlarmManager implementation on API 15) so that we can move to the platform API easily.
Unfortunately, JobScheduler is API 21. :(
Oh, sorry, for the confusion. I felt certain that this had been introduced with Jelly Bean. Okay, this makes the other libraries even more interesting. :)
The Firebase team released a new library that does exactly this and replaces GcmNetworkManager:
https://github.com/firebase/firebase-jobdispatcher-android
No longer blocks: 1248596
Assigning to krish. He is currently looking at the various options we have.
Assignee: nobody → k.krish
Status: NEW → ASSIGNED
(Assignee)

Comment 14

3 years ago
After a brief analysis, We have decided to move forward to use this library - https://github.com/evernote/android-job to schedule the tasks.
(Assignee)

Updated

3 years ago
Depends on: 1291180
(Assignee)

Updated

3 years ago
Depends on: 1291360
The Android team implemented this for us in the Android support library:
https://developer.android.com/reference/androidx/work/WorkManager
Status: ASSIGNED → RESOLVED
Last Resolved: 9 months ago
Resolution: --- → WONTFIX
You need to log in before you can comment on or make changes to this bug.