Query for activity ordered by when they last suspended

What I am trying to accomplish is to limit the rate that an activity is executed in a process. One particular activity I want to only be executed once every 30 seconds.

So what I have done is add a signal catching event before the activity so all instances will wait on a signal. I then have a separate process that is kicked off by a 30 second repeating timer that invoke s a delegate that queries for all executions waiting for the signal, chooses one of them, and sends the signal to just that execution.

That is all working, but I am open to suggestions for better ways to accomplish this.

The problem is that if that activity has an error I route that execution back to waiting on the signal again to retry again later. However that execution that had the error is chosen again by the query soon after it failed. I would rather have that one move to the back of the line.

So my thought was to choose which execution to signal by ordering the query by which one has been waiting the longest, but I can’t find any way to do that or even a way to query when an execution was last suspended.

I know I could probably set some variable on the execution (e.g. a count of errors) and order by that but trying to avoid the extra steps.

If you have synchronous history writing and you craft a custom query, you might be able to use the LAST_UPDATED_TIME_ in ACT_HI_TASKINST to determine which task was updated last. I’m not entirely certain it will be a workable proxy for when the process was suspended, but it is worth investigating.

Actually, the problem was my misunderstanding in how errors and error catching boundary events work.

I was throwing a Java exception from my delegate not realizing that to have error catching work that had to be translated into a BpmnError to actually route it back. Therefore when I got the exception it wasn’t routing back to the signal waiting event from the boundary event it was just failing back to signal event and therefore the start time for the signal event in history didn’t change.

When I properly throw BpmnError so it actually goes through the return path to the signal catching event the history has a new time. Then it turns out I don’t need to do anything else as the query results were automatically sorted by the start time so the later ones ended up at the back of the line.

I have to add that the sorting by the time seems to only occur when using .toList(). It does not seem to apply the sorting when using .listPage()

I was able to force the order by ordering on “START_TIME_” property. Here is what I did in my Kotlin code:

    .orderBy { "START_TIME_" }.asc()

In Java that would be something like:

    .orderBy(new ExecutionQueryProperty("START_TIME_")).asc()

Actually, what I really needed to do was create an EventSubscriptionQuery to query on the event subscription and that query has an orderByCreateDate.

But I have ended up switching from Flowable to Camunda so will bow out now.