Skip to content

Instantly share code, notes, and snippets.

@JEuler
Created February 25, 2019 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JEuler/b68e2f8aa613705a35d7fc7b159a8703 to your computer and use it in GitHub Desktop.
Save JEuler/b68e2f8aa613705a35d7fc7b159a8703 to your computer and use it in GitHub Desktop.
Priority queue job example
class SendDataJob(private val dataInput: SomeDataClass) :
Job(Params(1).requireNetwork().persist().groupBy(“tag”)), KoinComponent {
// job is queued, need to update local storage
override fun onAdded() {
}
@Throws(Throwable::class)
override fun onRun() {
val sendService: SendService? by inject()
sendService?.sendData(dataInput)
?.subscribeOn(Schedulers.io())
?.observeOn(Schedulers.io(), true)
?.subscribe({}, {})
}
override fun onCancel(cancelReason: Int, throwable: Throwable?) {
}
override fun shouldReRunOnThrowable(throwable: Throwable, runCount: Int, maxRunCount: Int): RetryConstraint? {
if (throwable is HttpException) {
//if it is a 4xx error, stop
val errorCode = throwable.code()
return if (errorCode < 400 || errorCode > 499)
RetryConstraint.RETRY else RetryConstraint.CANCEL
}
return RetryConstraint.RETRY
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment