Firebase jobdispatcher not triggering within specified window

孤者浪人 提交于 2019-12-03 17:20:43
mrg

If you look at source of Trigger class you'll notice that you have no sure that your job will run at given time.

/**
 * Creates a new ExecutionWindow based on the provided time interval.
 *
 * @param windowStart The earliest time (in seconds) the job should be
 *                    considered eligible to run. Calculated from when the
 *                    job was scheduled (for new jobs) or last run (for
 *                    recurring jobs).
 * @param windowEnd   The latest time (in seconds) the job should be run in
 *                    an ideal world. Calculated in the same way as
 *                    {@code windowStart}.
 * @throws IllegalArgumentException if the provided parameters are too
 *                                  restrictive.
 */
public static JobTrigger.ExecutionWindowTrigger executionWindow(int windowStart, int windowEnd) {
    if (windowStart < 0) {
        throw new IllegalArgumentException("Window start can't be less than 0");
    } else if (windowEnd < windowStart) {
        throw new IllegalArgumentException("Window end can't be less than window start");
    }

    return new JobTrigger.ExecutionWindowTrigger(windowStart, windowEnd);
}

You should also note that the next repetitive work is started only when the previous one is over. So when your work is long and expensive, the next execution time may be unexpected. For time consuming jobs you should extend SimpleJobService.

For creating a repeated task I am using my util method that creates a proper Trigger:

public class JobDispatcherUtils {
    public static JobTrigger periodicTrigger(int frequency, int tolerance) {
        return Trigger.executionWindow(frequency - tolerance, frequency);
    }
}

Usage:

class DataTrackerJobService extends SimpleJobService {
    // ...
}

public static void scheduleCompatibleJob(Context context) {
    FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context));
    Job myJob = dispatcher.newJobBuilder()
            .setService(DataTrackerJobService.class) // the JobService that will be called
            .setTag("api_trigger")
            .setRecurring(true)
            .setLifetime(Lifetime.FOREVER)
            .setTrigger(JobDispatcherUtils.periodicTrigger(20, 1)) // repeated every 20 seconds with 1 second of tollerance
            .setReplaceCurrent(true)
            .setRetryStrategy(RetryStrategy.DEFAULT_LINEAR)
            .setConstraints(Constraint.ON_ANY_NETWORK)
            .build();

    dispatcher.mustSchedule(myJob);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!