WorkManager triggered but returns Failure result - android

I am trying out Android WorkManager which is successfully being triggered after every 15 minutes.
However, the work is not being done and I get this error on my logs.
I/WM-WorkerWrapper: Worker result FAILURE for Work
This is how I have set-up my Constraints (Inside the Application Class) to trigger the work.
//set-up work
private fun setUpAsteroidLoadingWork() {
//define work constraints
val workConstraints =
Constraints.Builder()
.setRequiredNetworkType(NetworkType.UNMETERED)
.setRequiresCharging(false)
.build()
//create WorkRequest
val workRequest = PeriodicWorkRequestBuilder<LoadAsteroidsWorker>(15, TimeUnit.MINUTES)
.setConstraints(
workConstraints)
.build()
//get WorkManager
val workManager = WorkManager.getInstance(this)
//enqueue work
workManager.enqueueUniquePeriodicWork(
LoadAsteroidsWorker.WORK_NAME, ExistingPeriodicWorkPolicy.KEEP, workRequest)
}
I start the work inside the Application Class onCreate() method
override fun onCreate() {
super.onCreate()
//initialize Timber
Timber.plant(Timber.DebugTree())
Timber.i("Application's onCreate Called")
//start work inside onCreate
runWorkInBackground()
}
//switch work to run on background
private fun runWorkInBackground(){
CoroutineScope(Default).launch {
setUpAsteroidLoadingWork()
}
}
The code is supposed to trigger some work to download internet data in the repository. I have run a #GET request on postman and data is returned with no errors.
This is the Worker Class
class LoadAsteroidsWorker(context: Context, params: WorkerParameters) :
CoroutineWorker(context, params) {
companion object {
const val WORK_NAME = "LoadAsteroidWorker"
}
override suspend fun doWork(): Result {
Timber.i("do workWork() called")
//get instance of database for use with Repo initialization below
val db = AsteroidDatabase.getDatabaseInstance(applicationContext)
//initialize Repo
val repo = AsteroidRepo(db)
return try {
//define work i.e. load asteroids from Network for the next seven days
repo.getAsteroidsFromNetwork()
Timber.i("called repo method")
Result.success()
}catch (e:HttpException){
Timber.i("error - $e")
Result.retry()
}
}
}
This my WorkManager Dependency
//WorkManager - Kotlin + coroutines implementation 'androidx.work:work-runtime-ktx:2.6.0-alpha02'
Any leads on what I am doing wrong?

After a lot of searching, it has dawned on me that the issue is on the doWork() method where I was only 'catching' HttpException forgetting that there are other exceptions to deal with.
I added a second catch block which at last caught the 'bug'.
override suspend fun doWork(): Result {
Timber.i("do workWork() called")
//get instance of database for use with Repo initialization below
val db = AsteroidDatabase.getDatabaseInstance(applicationContext)
//initialize Repo
val repo = AsteroidRepo(db)
return try {
//define work i.e. load asteroids from Network for the next seven days
repo.getAsteroidsFromNetwork()
Timber.i("called repo method")
Result.success()
}catch (e:HttpException){
Timber.i("error - $e")
Result.retry()
}catch (e: Exception){
//catch general exceptions here
Timber.i("exception - $e")
Result.failure()
}
}
The issue had nothing to do with WorkManager but a JsonDataException.

Related

Insert Room data from WorkManager Worker?

I am following the Android room tutorial.. but have a slightly different requirement than it. I want to run a job in the background.. and insert a database item there, instead of with a button or on startup like they have it. I cannot find a way to do this?
override fun doWork(): Result {
try {
val appContext = applicationContext
......
val newDataItem = DataItem(param1 = "Title", iso_datetime_msg_received = "RIGHTNOW", alert_message = "HI!!!", uuid = 1231)
// THE LINE BELOW IS WHAT I WANT BUT GET AN ERROR
// Suspend function 'insert' should be called only from a coroutine or another suspend function
CustomRoomDatabase.getDatabase(myContext).dataItemDao().insert(newDataItem)
return Result.success(result)
} catch(error: Throwable) {
Log.i(TAG, "RetrieveDataItemWorker got error:" + error)
return Result.retry()
}
}
Your WorkManager class needs to Implement :CoroutineWorker(context, workerParams)
then make your doWork function a suspend fuction

Suspending function test with MockWebServer

I'm testing api that returns result using suspending function with MockWebServer, but it does not work with runBlockingTest, testCoroutineDispatcher, testCorounieScope unless a launch builder is used, why?
abstract class AbstractPostApiTest {
internal lateinit var mockWebServer: MockWebServer
private val responseAsString by lazy {
getResourceAsText(RESPONSE_JSON_PATH)
}
#BeforeEach
open fun setUp() {
mockWebServer = MockWebServer()
println("AbstractPostApiTest setUp() $mockWebServer")
}
#AfterEach
open fun tearDown() {
mockWebServer.shutdown()
}
companion object {
const val RESPONSE_JSON_PATH = "posts.json"
}
#Throws(IOException::class)
fun enqueueResponse(
code: Int = 200,
headers: Map<String, String>? = null
): MockResponse {
// Define mock response
val mockResponse = MockResponse()
// Set response code
mockResponse.setResponseCode(code)
// Set headers
headers?.let {
for ((key, value) in it) {
mockResponse.addHeader(key, value)
}
}
// Set body
mockWebServer.enqueue(
mockResponse.setBody(responseAsString)
)
return mockResponse
}
}
class PostApiTest : AbstractPostApiTest() {
private lateinit var postApi: PostApiCoroutines
private val testCoroutineDispatcher = TestCoroutineDispatcher()
private val testCoroutineScope = TestCoroutineScope(testCoroutineDispatcher)
#BeforeEach
override fun setUp() {
super.setUp()
val okHttpClient = OkHttpClient
.Builder()
.build()
postApi = Retrofit.Builder()
.baseUrl(mockWebServer.url("/"))
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build()
.create(PostApiCoroutines::class.java)
Dispatchers.setMain(testCoroutineDispatcher)
}
#AfterEach
override fun tearDown() {
super.tearDown()
Dispatchers.resetMain()
try {
testCoroutineScope.cleanupTestCoroutines()
} catch (exception: Exception) {
exception.printStackTrace()
}
}
#Test
fun `Given we have a valid request, should be done to correct url`() =
testCoroutineScope.runBlockingTest {
// GIVEN
enqueueResponse(200, RESPONSE_JSON_PATH)
// WHEN
postApi.getPostsResponse()
advanceUntilIdle()
val request = mockWebServer.takeRequest()
// THEN
Truth.assertThat(request.path).isEqualTo("/posts")
}
}
Results error: java.lang.IllegalStateException: This job has not completed yet
This test does not work if launch builder is used, and if launch builder is used it does not require testCoroutineDispatcher or testCoroutineScope, what's the reason for this? Normally suspending functions pass without being in another scope even with runBlockingTest
#Test
fun `Given we have a valid request, should be done to correct url`() =
runBlockingTest {
// GIVEN
enqueueResponse(200, RESPONSE_JSON_PATH)
// WHEN
launch {
postApi.getPosts()
}
val request = mockWebServer.takeRequest()
// THEN
Truth.assertThat(request.path).isEqualTo("/posts")
}
The one above works.
Also the test below pass some of the time.
#Test
fun Given api return 200, should have list of posts() =
testCoroutineScope.runBlockingTest {
// GIVEN
enqueueResponse(200)
// WHEN
var posts: List<Post> = emptyList()
launch {
posts = postApi.getPosts()
}
advanceUntilIdle()
// THEN
Truth.assertThat(posts).isNotNull()
Truth.assertThat(posts.size).isEqualTo(100)
}
I tried many combinations invoking posts = postApi.getPosts() without launch, using async, putting enqueueResponse(200) inside async async { enqueueResponse(200) }.await() but tests failed, sometimes it pass sometimes it does not some with each combination.
There is a bug with runBlockTest not waiting for other threads/jobs to complete before completing the coroutine that the test is running in.
I tried using runBlocking with success (I use the awesome port of Hamcrest to Kotlin Hamkrest)
fun `run test` = runBlocking {
mockWebServer.enqueue(MockResponse().setResponseCode(200).setBody(""))
// make HTTP call
val result = mockWebServer.takeRequest(2000L, TimeUnit.MILLISECONDS)
assertThat(result != null, equalTo(true))
}
There's a few things to note here:
The use of thread blocking calls should never be called without a timeout. Always better to fail with nothing, then to block a thread forever.
The use of runBlocking might be considered by some to be no no. However this blog post outlines the different method of running concurrent code, and the different use cases for them. We normally want to use runBlockingTest or (TestCoroutineDispatcher.runBlockingTest) so that our test code and app code are synchronised. By using the same Dispatcher we can make sure that the jobs all finish, etc. TestCoroutineDispatcher also has that handy "clock" feature to make delays disappear. However when testing the HTTP layer of the application, and where there is a mock server running on a separate thread we have a synchronisation point being takeRequest. So we can happily use runBlocking to allow us to use coroutines and a mock server running on a different thread work together with no problems.

how to use Coroutine in kotlin to call a function every second

i just created an app where my function getdata() call every second to fetch new data from server and updateui() function will update view in UI i don't use any asynctask or coroutine in my app i wants to do this please tell me how i can do that.
here's my code...
private fun getdata(){
try {
val api = RetroClient.getApiService()
call = api.myJSON
call!!.enqueue(object : Callback<ProductResponse> {
override fun onResponse(
call: Call<ProductResponse>,
response: Response<ProductResponse>
) {
if (response.isSuccessful) {
productList = response.body()!!.data
for (list in productList) {
if (list.BB.equals("AAA")) {
aProductList.add(list)
}
}
if (recyclerView.adapter != null) {
eAdapter!!.updatedata(aProductList)
}
updateui()
}
}
override fun onFailure(call: Call<ProductResponse>, t: Throwable) {
println("error")
}
})
} catch (ex: Exception) {
} catch (ex: OutOfMemoryError) {
}
Handler().postDelayed({
getdata()
}, 1000)
}
private fun updateui() {
try {
//some code to handel ui
} catch (e: NumberFormatException) {
} catch (e: ArithmeticException) {
} catch (e: NullPointerException) {
} catch (e: Exception) {
}
}
To run a function every second with coroutines:
val scope = MainScope() // could also use an other scope such as viewModelScope if available
var job: Job? = null
fun startUpdates() {
stopUpdates()
job = scope.launch {
while(true) {
getData() // the function that should be ran every second
delay(1000)
}
}
}
fun stopUpdates() {
job?.cancel()
job = null
}
However, if getData() only starts a network request and doesn't wait for its completion, this might not be a very good idea. The function will be called a second after it finished, but because the network request is done asynchronously it may be scheduled way too much.
For example if the network request takes 5 seconds, it will have been started 4 more times before the first one even finished!
To fix this, you should find a way to suspend the coroutine until the network request is done.
This could be done by using a blocking api, then pass Dispatchers.IO to the launch function to make sure it's done on a background thread.
Alternatively you could use suspendCoroutine to convert a callback-based api to a suspending one.
Update - Lifecycle scope
Inside a component with a Android Lifecycle you could use the following code to automate repeating ui updates:
fun startUpdates() {
val lifecycle = this // in Activity
val lifecycle = viewLifecycleOwner // in Fragment
lifecycle.lifecycleScope.launch {
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
// this block is automatically executed when moving into
// the started state, and cancelled when stopping.
while (true) {
getData() // the function to repeat
delay(1000)
}
}
}
}
This code requires the current androidx.lifecycle:lifecycle-runtime-ktx dependency.
The above remark about async, blocking or suspending code inside getData() still applies.
it's not advisable to hit the server every second. if you need to get data continuously try the socket. Because some times your server takes more than a few seconds to respond to your request. Then all your requests will be in a queue..if you still need to try with this.
fun repeatFun(): Job {
return coroutineScope.launch {
while(isActive) {
//do your network request here
delay(1000)
}
}
}
//start the loop
val repeatFun = repeatRequest()
//Cancel the loop
repeatFun.cancel()
For those who are new to Coroutine
add Coroutine in Build.gradle
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2'
To create a repeating Job
/**
* start Job
* val job = startRepeatingJob()
* cancels the job and waits for its completion
* job.cancelAndJoin()
* Params
* timeInterval: time milliSeconds
*/
private fun startRepeatingJob(timeInterval: Long): Job {
return CoroutineScope(Dispatchers.Default).launch {
while (NonCancellable.isActive) {
// add your task here
doSomething()
delay(timeInterval)
}
}
}
To start:
Job myJob = startRepeatingJob(1000L)
To Stop:
myJob .cancel()
I ended up doing like this with an extension function:
fun CoroutineScope.launchPeriodicAsync(repeatMillis: Long, action: () -> Unit) = this.async {
while (isActive) {
action()
delay(repeatMillis)
}
}
then call it like:
val fetchDatesTimer = CoroutineScope(Dispatchers.IO)
.launchPeriodicAsync(TimeUnit.MINUTES.toMillis(1)) {
viewModel.fetchDeliveryDates()
}
and cancel it like:
fetchDatesTimer.cancel()
My solution in Kotlin inside MainViewModel
fun apiCall() {
viewModelScope.launch(Dispatchers.IO) {
while(isActive) {
when(val response = repository.getServerData()) {
is NetworkState.Success -> {
getAllData.postValue(response.data)
}
is NetworkState.Error -> this#MainViewModel.isActive = false
}
delay(1000)
}
}
}
sealed class NetworkState<out R> {
data class Success<out T>(val data: T): NetworkState<T>()
data class Error(val exception: String): NetworkState<Nothing>()
object Loading: NetworkState<Nothing>()
}
My solution for one time running a code after check for something is successful and checking for that periodically, function is:
fun CoroutineScope.launchPeriodic(repeatMillis: Long, action: () -> Unit) : Job {
return launch {
while (!enabled) {
action()
delay(repeatMillis)
}
}
}
and start periodic function here (in which action runs every 2 seconds), which automatically ends up when something is enabled and some code run:
CoroutineScope(Dispatchers.IO).launchPeriodic(TimeUnit.SECONDS.toMillis(2)) {
if(checkIfSomethingIsEnabledCodeIsHere) {
enabled = true
//some code here to run when it is enabled
}
}

Cancel Retrofit request started from ViewModel coroutine job

I would like my app users to be able to cancel file upload.
My coroutine upload job in ViewModel looks like this
private var uploadImageJob: Job? = null
private val _uploadResult = MutableLiveData<Result<Image>>()
val uploadResult: LiveData<Result<Image>>
get() = _uploadResult
fun uploadImage(filePath: String, listener: ProgressRequestBody.UploadCallbacks) {
//...
uploadImageJob = viewModelScope.launch {
_uploadResult.value = withContext(Dispatchers.IO) {
repository.uploadImage(filePart)
}
}
}
fun cancelImageUpload() {
uploadImageJob?.cancel()
}
Then in the repository the Retrofit 2 request is handled like this
suspend fun uploadImage(file: MultipartBody.Part): Result<Image> {
return try {
val response = webservice.uploadImage(file).awaitResponse()
if (response.isSuccessful) {
Result.Success(response.body()!!)
} else {
Result.Error(response.message(), null)
}
} catch (e: Exception) {
Result.Error(e.message.orEmpty(), e)
}
}
When cancelImageUpload() it called the job gets cancelled and the exception gets caught in the repository but the result won't get assigned to uploadResult.value.
Any ideas please how to make this work?
PS: There is a similar question Cancel file upload (retrofit) started from coroutine kotlin android but it suggests using coroutines call adapter which is depricated now.
Have finally managed to make it work by moving withContext one level up like this
uploadImageJob = viewModelScope.launch {
withContext(Dispatchers.IO) {
_uploadResult.postValue(repository.uploadImage(filePart))
}
}

Worker manager: not start work in enqueue

I have such code, I need to implement the task queue, if the task is in the queue, then you do not need to add it.
I implemented as shown in when, everything works, but sometimes the state of the worker remains ENQUEUED, and new tasks are not added to the queue.
That is, when there is no Internet, I add a task, when the Internet appears, tasks begin to run out, but for some reason, sometimes it doesn’t happen, I can’t understand why the task does not start despite the fact that the Internet is there and the task is in the queue.
How can you determine why the task will not start?
Does anyone have a better suggestion?
//run task
runOneTimeWorkByType<GetDocumentsWorker>(GET_DOCUMENTS_TAG)
private inline fun <reified W : Worker> runOneTimeWorkByType(tag: String) {
val workerInfoList = workManager
.getWorkInfosByTag(tag)
.get()
for (item in workerInfoList) {
if (item.state == WorkInfo.State.ENQUEUED){
return
}
}
val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
val workRequest =
OneTimeWorkRequestBuilder<W>()
.setConstraints(constraints)
.addTag(tag)
.build()
workManager.enqueue(workRequest)
}
class GetDocumentsWorker(ctx: Context, workerParams: WorkerParameters) :
Worker(ctx, workerParams) {
#Inject
lateinit var serviceUtils: ServiceUtils
init {
App.appComponent.inject(this)
}
override fun doWork(): Result {
Log.d("workmng", "GetDocumentsWorker: start")
try {
serviceUtils.documentsGet()
} catch (e: Exception) {
Log.d("workmng", "GetDocumentsWorker: exception", e.cause)
return Result.retry()
}
Log.d("workmng", "GetDocumentsWorker: end")
return Result.success()
}
}
UPDATE:
I tried to start the task without conditions, but in this case, nothing starts either, have ideas why so?
fun runGetDocumentsTask() {
val workRequest =
OneTimeWorkRequestBuilder<GetDocumentsWorker>()
.addTag(GET_DOCUMENTS_TAG)
.build()
workManager.enqueue(workRequest)
}
Everything starts to work fine when I cancel jobs: workManager.cancelAllWork()
When creating a worker, I run several periodic tasks, can there be a problem in them? If so, how to fix it?
private var workManager: WorkManager = WorkManager.getInstance(ctx)
init {
//workManager.cancelAllWork()
runSendAllPeriodicTasks()
}
private fun runSendAllPeriodicTasks() {
runOneTimeWorkOnPeriod<SendAllWorker>(15, TimeUnit.MINUTES)
runOneTimeWorkOnPeriod<FailureFilesResendWorker>(3, TimeUnit.HOURS)
runOneTimeWorkOnPeriod<GetItemsWorker>(1, TimeUnit.HOURS)
}

Categories

Resources