Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is setOverrideDeadline not respected?

I use the following code to schedule a job when the network becomes available:

static JobScheduler jobScheduler(Context context) {
    return (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
}

static JobInfo jobInfo(Context context, Date nextTime) {
    return new JobInfo.Builder((int) R.string.action_send_mail, new ComponentName(context, MessageJobService.class))
                .setMinimumLatency(max(0, nextTime.getTime() - System.currentTimeMillis()))
                .setOverrideDeadline(15 * 1000)
                .setBackoffCriteria(5000, JobInfo.BACKOFF_POLICY_EXPONENTIAL)
                .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
                .build();
};

public static void scheduleNextJob(Context context) {
    MailMessage mailMessage = DbHelper.inContext(context).loadNextMessage();

    if(mailMessage != null) {
        jobScheduler(context).schedule(jobInfo(context, mailMessage.getNextAttempt()));
    }
}

My understanding of setOverrideDeadline is that my job will fire after 5 seconds when the network becomes available, or no later than 15 seconds. In practice, if I enable airline mode, and run scheduleNextJob the job never fires until the I disable airline mode.

At that point, within my job, isOverrideDeadlineExpired is true, so I'm pretty sure the deadline is being set, it just doesn't seem to be respected.

Why is the override deadline not being respected?

like image 393
David Berry Avatar asked Jan 10 '18 17:01

David Berry


1 Answers

Apparently the combination of setMinimumLatency(0) and setOverrideDeadline doesn't work as expected. Changing the minimum latency to at least 1 seems to solve the problem:

static JobInfo jobInfo(Context context, Date nextTime) {
    return new JobInfo.Builder((int) R.string.action_send_mail, new ComponentName(context, MessageJobService.class))
                .setMinimumLatency(max(1, nextTime.getTime() - System.currentTimeMillis()))
                .setOverrideDeadline(15 * 1000)
                .setBackoffCriteria(5000, JobInfo.BACKOFF_POLICY_EXPONENTIAL)
                .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
                .build();
};
like image 114
David Berry Avatar answered Nov 14 '22 23:11

David Berry