Reputation: 196
I'm using WorkManager to schedule some periodic work, to synchronize a local DB with a remote one. Everything worked fine for months, until one day the DB stopped the synchronization. I found out through Stetho that all my Work instances had "schedule_requested_at" value equal to -1.
PeriodicWorkRequest refreshCpnWork = new PeriodicWorkRequest.Builder(MainWorker.class, Constants.WORKER_INTERVAL_MIN, TimeUnit.MINUTES)
.addTag("PeriodicWork")
.build();
WorkManager.getInstance().enqueue(refreshCpnWork);
This is the way I schedule my work, and as I said it worked fine for months. But from a certain moment it stopped scheduling my work, and in the picture instead of a correct timestamp there is -1.
Upvotes: 0
Views: 440
Reputation: 196
I found out what is the meaning of -1 in schedule_requested_at.
From WorkSpec.java (work-runtime:1.0.0-alpha11):
/**
* This field tells us if this {@link WorkSpec} instance, is actually currently scheduled and
* being counted against the {@code SCHEDULER_LIMIT}. This bit is reset for PeriodicWorkRequests
* in API < 23, because AlarmManager does not know of PeriodicWorkRequests. So for the next
* request to be rescheduled this field has to be reset to {@code SCHEDULE_NOT_REQUESTED_AT}.
* For the JobScheduler implementation, we don't reset this field because JobScheduler natively
* supports PeriodicWorkRequests.
*/
@ColumnInfo(name = "schedule_requested_at")
public long scheduleRequestedAt = SCHEDULE_NOT_REQUESTED_YET;
If the value is -1 the work has been inserted into the table but it hasn't been scheduled yet.
In my case there were too many Work instance to schedule something new, following this query coming from WorkSpecDao.java:
/**
* @return The List of {@link WorkSpec}s that are eligible to be scheduled.
*/
@Query("SELECT * FROM workspec WHERE "
+ "state=" + WorkTypeConverters.StateIds.ENQUEUED
// We only want WorkSpecs which have not been previously scheduled.
+ " AND schedule_requested_at=" + WorkSpec.SCHEDULE_NOT_REQUESTED_YET
+ " LIMIT "
+ "(SELECT MAX(:schedulerLimit" + "-COUNT(*), 0) FROM workspec WHERE"
+ " schedule_requested_at<>" + WorkSpec.SCHEDULE_NOT_REQUESTED_YET
+ " AND state NOT IN " + COMPLETED_STATES
+ ")"
)
List<WorkSpec> getEligibleWorkForScheduling(int schedulerLimit);
Upvotes: 0