How to make background job and foreground process exclusive?

Hi,

By default, only one background job will run on a process at one time (as the exclusive flag defaults to true). It is easy to limit a process instance to only run one foreground process (e.g. completing a task) at a time by implementing locking before the call to the engine is made. This gives quite a lot of flexibility.

However, sometimes I want to make a background job exclusive with a foreground process. For example, if someone sends a message that updates a timer, I don’t want to process that update immediately if there’s currently a background job running due to that timer having expired, I want to wait until the background job has finished.

One option would be to let one of the jobs fail and retry it but I will sometimes have side effects that are difficult or impossible to roll back.

Is this a problem that others have already solved? I am currently using 5.22 but we will eventually migrate to Flowable 6.

Thanks,

Tom

Hey @tomcooke,

I am having troubles understanding your example.

If your receiving of a message that updates a timer is also an exclusive job then you will correctly have a single job running for the process instance.

If you are modifying currently executing jobs you can check if they are already running (are locked or not) and if they are postpone your job.

Cheers,
Filip

Hi @filiphr ,

Many thanks for your response - in my example I would be calling RuntimeService.messageEventReceived() and the flow from the handler to the end activity would not have any asynchronous activities, my understanding was that the exclusive flag only applies to tasks initiated from timers or explicitly marked as asynchronous?

Sometimes I might be able to handle this by using the asynchronous flag and benefit from exclusive jobs but at other times I would want to know that the flow from messageEventReceived has been processed before returning to the caller and so the asynchronous flag wouldn’t be appropriate.

Being able to check whether or not my background jobs are already running and postpone my call to messageEventReceived would solve part of the problem but I would also need a way to prevent the background job from running when my call to messageEventReceived is being processed.

Thanks,

Tom

Your understanding is correct. The exclusive flag is applied in such scenarios only. I was not sure what exactly you are doing.

You could achieve this with some low level API from the impl packages. You could create your own command that would do something like DefaultInternalJobManager#lockJobScopeInternal before running your logic and DefaultInternalJobManager#clearJobScopeLockInternal. Keep in mind that this are low level non public APIs.

Cheers,
Filip

Hi Filip,

Thanks for confirming my understanding, and for the tips on implementing this - very helpful, thank you!

Cheers,

Tom