[8.x] [RFC] PoC for making chained jobs batchable #34337
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a proof-of-concept to allow chained jobs in a batch. I didn't write any new tests for now, but made sure the old tests still ran.
What's the intention?
I want to be able to define complex processes that consist of multiple jobs. Some of them can happen in parallel but some of them have dependencies on each other. However, I still want to be able to treat the whole batch as one "process". This is similar to the Process Manager (or Saga) pattern.
Example
After adding a new employee to the system, we want to start an onboarding process that will set up all required accounts automatically. None of these steps really have anything to do with each other per se, but together they make up the onboarding process. The onboarding isn't completed unless all of these steps have finished (hence why I want to refer to them as one unit – the batch).
Some of these steps can run in parallel but some can't run unless other steps have finished. You would be able to model this process like this:
Changes
To allow for this, I added a new method
dispatchInBatch
toPendingChain
that only returns the first job of the chain instead of directly dispatching it. This will essentially turn thisinto this
In order for the batch to correctly calculate the number of jobs it needs to run, we can no longer simply count how many entries the
$jobs
array of the batch has. Instead, for each job we have to determine if it has chained jobs and add them to the total. Otherwise the batch would end too soon (or rather, thethen
callback would fire too soon. The remaining jobs in the chain would still be executed afterwards).I want to ensure that the jobs in the chain are also aware that they're being run inside a batch. For this reason we need to ensure that every job in the chain has the reference to the batch set correctly. The first job in the chain gets handled automatically by the batch itself. For each subsequent job we have to manually set the
$batchId
to the previous job's batch id (if it exists).This code probably has a bunch of issues and edge cases I might have overlooked. I'm also not sure how nesting this even deeper would behave. Treat this as a proof-of-concept so we can discuss the idea.