Returning data from a Gradle WorkAction

Is there any easy way to return data from a Gradle 5.6+ WorkAction performed by a WorkQueue back to the thread that submitted the WorkAction?

Currently, I pass data by having a static ConcurrentMap<Long, MyConcurrentDataStructure> that maps from the thread ID of the thread that submits the work to a concurrent data structure. The thread ID is passed a Long parameter to the WorkAction, which uses it as a key to the concurrent data structure from the ConcurrentMap, which it then modifies.

This seems overly complicated. Is there any simpler way to return this data? If not, I think that adding some sort of callback mechanism would be useful. To avoid concurrency issues, the callback code should only ever run for the data from one worker at one time; this should be enforced by enqueuing callback parameters into a FIFO queue, and locking reading from the queue as long as a callback is currently executing.

Is there any easy way to return data from a Gradle 5.6+ WorkAction performed by a WorkQueue back to the thread that submitted the WorkAction?

Don’t use a static variable, workers might be loaded via a different classloader or even running on a separate daemon jvm (so will not share static state). Write it to a file (eg json/xml/properties). You could also serialize an object to/from a file via ObjectOutputStream.

Each worker can write a separate file, then the main thread waits for all to complete and collects them all (eg all files in a specific directory)

1 Like

@Lance Thanks for the help.

Is there any github issue for any sort of simpler worker callback mechanism? I assume not…

Also, if I create a new feature request issue for this, I imagine that it would be very low priority (and probably never implemented). Correct?

Thanks again.

There is an existing feature request for this: https://github.com/gradle/gradle/issues/8233

I just threw together a wrapper for the worker executor API which allows you to specify jobs as Callable and under the hood it does all the serializing / deserializing to files.

See CallableWorkerExecutor.java and SampleTask.java

It could do with some prettying up but it works as a proof of concept

1 Like

@Lance: thanks. Do you want to post the code in a comment on: