Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WorkManager: Why does failed unique work with the "APPEND" ExistingWork strategy not allow more work under the same name?

Let's say we are developing a messaging app where we want to send messages into given conversations where the order of those messages is important in that conversation only, and if the app is put to the background, we want a guarantee that the message will be sent.

The WorkManager#beginUniqueWork method seems ideal for this, where the uniqueWorkName will be some conversation id, and ExistingWorkPolicy.APPEND will be used as the work policy to keep the work in the order scheduled.

So far in my application, as long as each piece of Work returns Result.SUCCESS, then any future scheduled work will be executed as expected. However, if one particular message fails to send in a fatal way and I return Result.FAILURE, then all future work with the same conversation id never seems to reach my implementation of Worker#doWork().

After digging through the source code of the EnqueueRunnable class, this seems like a very deliberate choice. What I can't really understand is why that is? It seems odd that if a uniqueWorkName fails, that that name becomes unusable for the rest of the life of the application (this persists across killing the app).

Furthermore, I'd like to know if anybody has a good solution to this, or knows if this will change in future versions of WorkManager. So far, the best thing I can think of is to return Result.SUCCESS but encode my own failure state in the output Data so that any observers of the work know it has failed. This however is a bit awkward and isn't very obvious for future maintainers of the code (and can be a bit confusing when watching the logs for a given piece of Work).

Perhaps my intended use of unique work is completely wrong and there's a better solution out there. Any ideas would be greatly appreciated, thanks!

like image 297
Dean Avatar asked Oct 28 '18 16:10

Dean


People also ask

Is WorkManager deprecated?

This method is deprecated.

What is WorkManager explain in depth?

WorkManager is an Android Jetpack library that runs deferrable, guaranteed background work when the work's constraints are satisfied. WorkManager is the current best practice for many types of background work.

How does a WorkManager work?

WorkManager is intended for work that is required to run reliably even if the user navigates off a screen, the app exits, or the device restarts. For example: Sending logs or analytics to backend services. Periodically syncing application data with a server.

How do I know if my WorkManager is running?

At any point after enqueuing work, you can check its status by querying WorkManager by its name , id or by a tag associated with it. The query returns a ListenableFuture of a WorkInfo object, which includes the id of the work, its tags, its current State , and any output data set via Result. success(outputData) .


1 Answers

So I found the answer to my own question in this google issue tracker report.

Basically, unique work using the APPEND strategy creates a WorkContinuation where every new item is chained on as if we were to use the WorkContinuation#then method. Failing or cancelling the chain then cancels all downstream work, and so this is intended behaviour.

The ticket suggests 2 approaches:

If you really want APPEND's behavior, then another thing you could do is to check for WorkStatuses of the WorkRequests, and if (all of them happened to be cancelled) use REPLACE instead of APPEND. Bear in mind, this is inherently racy, because your WorkRequests might not have cancelled yet. So make sure you have some synchronization primitives around your use of WorkManager's APIs.

and

The simplest way to do this is to not actually return Result.FAILED; if you always return SUCCEEDED and return the actual pass/fail bit (if needed) in the output data, you can make sure the chain always keeps running.

Which is what I'm already doing. Hope this helps someone else.

like image 134
Dean Avatar answered Oct 18 '22 21:10

Dean