Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP send POST request in separate thread and forget

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:

  • Calling an external script (e.g. written in python) to send this request. I've read that using exec() can be very resource intensive. We have lots of bookings so we would be sending lots of these booking POSTs. So ideally we need something resource conscience.
  • Using sockets. I'm not familiar with the configuration of these and I'm worried about our socket server being down. It also feels like a mission to maintain. Maybe I'm wrong?
  • Using a service like Pusher which is effectively a socket service. The disadvantage is that if the listener misses a message they will never get it again. E.g. the partner would miss storing that booking.

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!

like image 722
Martin Avatar asked Oct 26 '12 21:10

Martin


2 Answers

So essentially you are creating an API in PHP that other clients consume. here is what I would suggest:

  1. 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.

  2. use HTTP persistent connection: in apache its called keep-alive set its value to high, so clients can reuse existing connection & thus reduced latency.

  3. 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.

  4. 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

like image 160
CuriousMind Avatar answered Sep 28 '22 08:09

CuriousMind


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.

like image 24
gapple Avatar answered Sep 28 '22 09:09

gapple