Reputation: 3837
I have a periodic work request that is suddenly stopping its recurrences after a day or more.
inline fun <reified W : Worker> Context.schedule(repeatInterval: Long = 1,
interval: TimeUnit = TimeUnit.HOURS,
flexTimeInterval: Long? = null,
flexInterval: TimeUnit? = null,
constraints: Constraints = networkConstraint) {
val workManager = WorkManager.getInstance(this)
val workRequestBuilder = if (flexInterval != null && flexTimeInterval != null) {
PeriodicWorkRequestBuilder<W>(repeatInterval, interval, flexTimeInterval, flexInterval)
} else {
PeriodicWorkRequestBuilder<W>(repeatInterval, interval)
}
val work = workRequestBuilder
.setConstraints(constraints)
.addTag(W::class.java.name)
.build()
background {
workManager.cancelAllWorkByTag(W::class.java.name).await()
workManager.enqueue(work)
}
}
It has been fine up until the latest version of my application. Where I've noticed it just stops recurring after a while. Previously it went on for months on end no problems.
My question is what are the reasons other than the periodic work request being cancelled for it to suddenly stop? If a job were to hang and block it's thread and never return the Work result would this cause issues? According to the documentation if the job does not return in 10 minutes it is stopped. That is why I am so confused... the only place I cancel it is right before I schedule it.
It is scheduled every 35 minutes:
fun schedule(context: Context) = context.schedule<DeviceCheckInWorker>(
flexTimeInterval = 15,
interval = TimeUnit.MINUTES,
repeatInterval = 35,
flexInterval = TimeUnit.MINUTES)
Upvotes: 3
Views: 1068
Reputation: 3837
Feel silly for asking this question. The answer is as the documentation would state, a periodic work request would continue repeating until it is cancelled.
There is no reason otherwise that it would not repeat.
Our new version introduced an event in response to an outside source intent that would essentially restart our app from another activity. In that other activity was startup code that first cleared all workers with WorkManager.getInstance(context).cancelAllWork()
and then started the scheduled workers again. The only problem was this specific worker periodic worker that I thought was stopping was not scheduled again in this function.
So of course... like I had a hunch I was at fault for cancelling it specifically myself. Had to ask... was one of those moments. Had to watch my app for a day and a bit through logcat
. Fun times.
Sorry, StackOverflow for the useless question.
Upvotes: 4