There are these two applications:
- WordPress-site with REST-API. Let's call this Brandon.
- Another system, using the REST-API. Let's call this Jeremy.
I know about the WordPress Stack Exchange, but this is more a PHP question than it is a WordPress question (I think).
The problem
Jeremy creates and updates 'atoms' on Brandon.
Whenever Jeremy updates an atom on Brandon, then immediately after, an operation called BuildMolecules()
runs, that bulks the atoms into groups and updates electron-, neutron- and proton-information on the molecules on Brandon.
But currently, this is being done on Jeremy's thread. This means that if Jeremy updates an atom, then it has to wait for the BuildMolecules()
to finish, before it returns its response. And even worse, if something goes wrong, while BuildMolecules() runs, then Jeremy receives an error, which is wrong (Since the atom got updated as it should). This last thing could and should be solved with try/catch-statements, but still...
How do I make a function that runs immediately after Jeremy updates an atom, without doing it on Jeremy's thread?
Solution attempts and considerations
CRON-job
I considered running a cron-job every 5-10 seconds, checking if an atom was updated. And if so, then run an update operation. It just feels kind of naughty, since I would have to run this very often, to achieve a smooth integration between Jeremy and Brandon. But this should work. And if the run takes longer than the 5-10 seconds, then I would have to account for that, setting status or something, stopping BuildMolecules()
to run twice, where it should only have been run once.
A new thread
Even if I could set up a new thread (I've never done anything like that), then I can read in this post here: Creating new thread(?) in PHP that I shouldn't try to do it.
If WordPress offers a function for it
I looked into WordPress' REST-documentation, but couldn't find anything that could help me.