GCM Network Manager losing jobs

464 views Asked by At

I am trying to use GCM Network Manager to send logs to out backend service. We have an alarm running about every hour that creates a OneoffTask that, when executed, will call the backend service with the log message.

This works, BUT a very large amount of the Tasks are lost (way more than half). At first, I thought it has something to do with our backend, or the network, but after adding a ton of file logging, it turns out that onRunTask in the service is never triggered for these tasks (but they are definitely getting scheduled. Why are these lost? Am I misunderstanding the API, or is OneoffTasks simply not reliable?

This is how the OneoffTask is scheduled:

GcmNetworkManager.getInstance(context).schedule(new OneoffTask.Builder()
    .setService(AvroLogService.class)
    .setExtras(bundle)

    // A mandatory tag which identifies the task
    // We add a unique hash to the tag to make sure that
    // tasks are logged and not thrown away as dupes.
    // See: http://stackoverflow.com/q/34528960/304262
    .setTag(java.util.UUID.randomUUID().toString())

    // Persist to disk, even across boots:
    .setPersisted(true)

    // Sets a time frame for the execution of this task in seconds.
    // This specifically means that the task can either be
    // executed right now, or at latest at a certain point:
    .setExecutionWindow(0, TWO_WEEKS_IN_SECONDS)
    .build());

Again, not this works, but only part of the messages. For the messages that are subsequently lost, the above code is definitely executed (I've added file logging to verify this), but there is never a corresponding onRunTask triggered for the lost ones.

I have verified that:

  1. Manifest is updated according to the Network Manager Implementation Guide (https://developers.google.com/cloud-messaging/network-manager)
  2. AvroLogService (my service) extends GcmTaskService
  3. It overrides onRunTask
  4. The app has RECEIVE_BOOT_COMPLETED permission.
  5. AvroLogService does NOT override onStartCommand.

I'm lost. Can someone share insights on this?

2

There are 2 answers

1
bongo On

As answer above execution time range may be to big. Also I think that You want execute event periodically try use PeriodicTask.Builder instead ofOneoffTask.Builder

4
Roman_D On

As I guess your constant TWO_WEEKS_IN_SECONDS really means 2 WEEKS. In this case you task is eligible for execution in any point of time from now to 2 WEEKS. So, this task doesn't have to be executed every hour. Try to set execution window in range of one hour or even less (.setExecutionWindow(0, HALF_AN_HOUR_IN_SECONDS))

See google api docs