My app processes bookings, and upon process I would like it to send the booking details to whichever of my app's partners made the booking, so they can store a reference to the booking, AND without hold up the other processing my app has to do.
I've thought about how to send a message to a partner, and my solution would be to send a cURL POST request to whichever of my partner's is making the booking (besides answering my question maybe someone has a better solution than this?).
Each partner would have a specific URL that they would setup to receive this POST request and store the booking information we send them.
THE PROBLEM: If we try send this POST request and their web server is slow or down then we may wait unnecessarily long to get a response, which in turn would delay the confirmation of the booking of the actual user making use of our service.
IDEAL SOLUTION: I would like to send this PHP cURL request in another thread so we can continue on our merry way and confirm the booking. If in the other thread there is a delay, this will not hold us up.
Other solutions I have considered are:
It would be great to get some feedback about what I'm trying to accomplish here, especially from someone that has been in need of a solution for the same kind of situation I'm in. Thanks for any help!
So essentially you are creating an API in PHP that other clients consume. here is what I would suggest:
Let your clients make requests to you, via POST/GET method; instead of you as a API server trying to push data to your clients. that is much better approach because it frees you with things client's server down, slow or something else. So when they send you a request, it means they are fully capable for handling response.
use HTTP persistent connection: in apache its called keep-alive
set its value to high, so clients can reuse existing connection & thus reduced latency.
For multiprocessing in php, have a look at
Getting into multiprocessing. Basically, there is pcntl_fork()
function which allows you to fork a process & create new child process for multiprocessing.
Implement a background job que based on redis or something similar. idea is that all long running jobs gets dropped into the background job que & then a worker is spawned for each task so these jobs are executed via multiprocessing. PHP Workers with Redis & Solo
Hope it helps
What about having a separate script running through cron?
It would likely require a greater delay on obtaining final confirmation (only being able to send external requests every minute), but would allow the user interface action to just store the information in a queue and continue, and then the scheduled task can process it later.
The queue processor would be able to check for a valid response from the external service and try again if necessary, without holding up the user interface.
For more timely processing, you could create a daemon that checks the queue for entries to process on a smaller delay than is possible with cron. PEAR has a System_Daemon package that can help with creating the daemon in PHP.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With