Created
February 25, 2019 17:01
-
-
Save JEuler/b68e2f8aa613705a35d7fc7b159a8703 to your computer and use it in GitHub Desktop.
Priority queue job example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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