Jabbit Worker
A unit of background work, typed by the payload it takes and the payload it returns.
Workers are registered against a JobType in the jabbit { } builder and instantiated by the library every time a job starts, so they must be cheap to create and must not hold state between runs.
doWork runs on a background dispatcher and is cancelled cooperatively when the platform stops the job — when constraints stop being satisfied, when the job is cancelled, or when the iOS background window expires. Long-running loops should therefore check isActive or call suspending functions that honour cancellation. A cancelled job returns to JobState.ENQUEUED and runs again later.
class SyncWorker(private val api: Api) : JabbitWorker<SyncInput, SyncOutput> {
override suspend fun doWork(job: JobExecution<SyncInput>): JobResult<SyncOutput> {
return try {
JobResult.success(SyncOutput(items = api.sync(job.input.since)))
} catch (e: IOException) {
JobResult.retry()
}
}
}Content copied to clipboard