I´ve been facing a issue with WorkManager when reescheduling jobs. Currently, I´ve found that at some point after launching a request with Okhttp and raising an error on AuthInterceptor it gets stuck and no other job gets launched.
This is the JobOrganizer
class that manages the first steps of the work planing. It chains the first queue of jobs. You will see more jobs that are not pasted here but the main difference is that the first chained job goes without WiFi network constraints and the others does.
object JobOrganizer {
const val WORK_INTERVAL: Long = 20
const val SCH_DATA_UPDATE_WORK_RESCHEDULE = "scheduled_data_update_work_reschedule"
const val SCH_DATA_UPDATE_WORK = "scheduled_data_update_work"
private val schDataUpdateJob: OneTimeWorkRequest
get() = OneTimeWorkRequestBuilder<SCHDataUpdateJob>()
.addTag(SCH_DATA_UPDATE_WORK)
.setConstraints(wifiConstraint)
.build()
val wifiConstraint: Constraints
get() = Constraints.Builder()
.setRequiredNetworkType(NetworkType.UNMETERED)
.setRequiresDeviceIdle(false)
.setRequiresBatteryNotLow(false)
.setRequiresCharging(false)
.setRequiresStorageNotLow(false)
.build()
fun getWorkInfos(context: Context, tag: String): LiveData<List<WorkInfo>> {
val workManager = WorkManager.getInstance(context)
return workManager.getWorkInfosByTagLiveData(tag)
}
private fun clearWorks(workManager: WorkManager) {
workManager.pruneWork()
}
private fun cancelSCHJobs(context: Context) {
val workManager = WorkManager.getInstance(context)
workManager.cancelAllWorkByTag(SCH_DATA_UPDATE_WORK )
clearWorks(workManager)
}
fun scheduleJobs(context: Context) {
cancelSCHJobs(context)
WorkManager.getInstance(context)
.beginWith(schTypesDownloadJob)
.then(schDownloadJob)
.then(schDataUpdateJob)
.then(schDataUploadJob)
.then(schCleanupJob)
.enqueue()
FirebaseAnalytics.getInstance(context).logEvent(AnalyticsEvents.Sync.SYNC_SCH_CONFIGURE_FORM_CLEANUP, Bundle())
}
}
The AuthInterceptor class
class AuthInterceptor(private val context: Context?) : Interceptor {
@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response {
val originalRequest = chain.request()
if (context == null) {
return chain.proceed(originalRequest)
}
val auth = AuthRepository(context).getAuth()
if (auth.isNullOrEmpty()) {
return chain.proceed(originalRequest)
}
val version = String.format(
"%s: %s (build %s)",
BuildConfig.FLAVOR,
BuildConfig.VERSION_NAME,
BuildConfig.VERSION_CODE
)
val compressedRequest = originalRequest.newBuilder()
.header("Authorization", String.format("Bearer %s", auth[0].token))
.header("mobile-app-version", version)
.build()
return chain.proceed(compressedRequest)
}
}
The Update Job that reeschedules itself with a 30 min delay. The main try / catch is for the AuthInterceptor errors.
class SCHDataUpdateJob(var context : Context, params : WorkerParameters) : Worker(context, params) {
override fun doWork(): Result {
FirebaseAnalytics.getInstance(context).logEvent(AnalyticsEvents.Sync.SYNC_SCH_UPDATE_START, Bundle())
var success = UPDElementTypesJob(context).doWork()
if (!success) {
FirebaseAnalytics.getInstance(context).logEvent(AnalyticsEvents.Sync.SYNC_UPD_ELEMENTTYPES_ERROR, Bundle())
Log.e("SYNC", "SCHDataUpdateJob UPDElementTypesJob error")
}
FirebaseAnalytics.getInstance(context).logEvent(AnalyticsEvents.Sync.SYNC_SCH_UPDATE_FINISH, Bundle())
val dataUpdateWorkRequest = OneTimeWorkRequestBuilder<SCHDataUpdateJob>()
.setInitialDelay(JobOrganizer.WORK_INTERVAL, TimeUnit.MINUTES)
.addTag(JobOrganizer.SCH_DATA_UPDATE_WORK)
.setConstraints(JobOrganizer.wifiConstraint)
.build()
WorkManager.getInstance(applicationContext)
.enqueue(dataUpdateWorkRequest)
Log.e("SYNC", "SCHDataUpdateJob finished")
return Result.success()
}
}
This is the fragment that calls scheduleJobs().
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
super.onCreateView(inflater, container, savedInstanceState)
mainView = inflater.inflate(R.layout.fragment_draft_list, container, false)
sync = mainView!!.findViewById(R.id.sync)
sync.onClick {
mFirebaseAnalytics!!.logEvent(AnalyticsEvents.UPDATE_BUTTON_CLICKED, Bundle())
if (!ConnectionUtils.isConnectedToWifi(activity!!.applicationContext)) {
showConnectivityDialog()
} else {
sync.visibility = View.GONE
doAsync {
JobOrganizer.scheduleJobs(context!!)
}
}
}
if (forceDownload) {
JobOrganizer.scheduleJobs(context!!)
}
return mainView!!
}
At some point the this last job doens't get scheduled or doesn't run. Any clue?
Thanks.