I have a task that runs as part of an afterSave callback, it is a somewhat complex calculation. However, it is possible (and desirable) for the user to trigger multiple saves rapidly. However, the result is that this calculation runs repeatedly but it only needs to run after the final save has happened.
As far as I’m aware there is no way to simply interrupt/abort a task in PHP once it has begun? I am wondering if there would be a way in CakePHP to debounce the operation, so it will only perform the calculation after the rapid updates have stopped.
The user does need to see the results of the calculation quite quickly so I can’t just schedule the task.
Then debounce your form submit handler so it can’t be triggered multiple times in a short amount of time.
No, you can’t interrupt a PHP process while it’s running without just killing the whole process. Depending on how you connected your webserver to PHP it either runs 1 PHP process per request or it uses a pre-existing pool (FPM) of PHP processes to execute your request.
In your case I’d extract that calculation logic into a queue task, make that queue task unique and therefore prevent it from being executed in parallel multiple times.
Gotcha, thanks. It is essentially the user checking a bunch of different boxes with their own data, so I can’t simply debounce and keep only the final submit. I do have queue but the user needs immediate feedback once all operations have completed, so unless I ran the queue every few seconds it wouldn’t work either.
I will need to solve this on the frontend somehow I think, just batch the operations and submit them all at once when the user is done. Appreciate your input!
You can display a “… calculating” info as immediate feedback for the user while the queue is still processing it.
See the demo, it shows how you can interact while it still background processes in real time.
Thats usually the cleanest approach, as it makes it clear to them without creating issues.
If its just a calculation thing which gets updated after the use has saved then it shouldn’t be present in the entity/DB anyway.
It should be a virtual field from a PHP side and you can expose that calculation logic to the frontend via an endpoint to fetch the current result for the given values.
Then you don’t need to re-implement it in the frontend.