How to perform asynchronous tasks in PHP?

Asked

Viewed 436 times

0

I have a simple virtual shop system, where the customer places an order for certain products and the system records this, at the moment the customer logs the order the system sends an email message and also communicates with an API by passing the order data. The problem is that this whole process is taking too long to run so that I can then give a successful response to the customer. The act of sending the email and communicating with the API are not required for me to provide a successful response to the customer.

Having explained my situation, I wanted the system to be able to record the client’s order and immediately return the successful response, and subsequently carry out the actions of sending the email and communicating with the API.

I had two ideas, but I couldn’t find a solution for them:

1 - The first idea would be to return the answer to the client and continue with the rest of the script without my page getting stuck with these actions. It turns out that currently my php script only returns the message to the client when it finishes all the tasks (send email and API).

Ex: Client makes order->System registers order->System responds client [End of communication with client]->System sends e-mail ->System communicates API

2 - The second alternative was to create something similar to threads, but since it is a Webapp my Apache server does not support pthreads.

I would like to know if there is any solution to this problem.


PHP code I’m using:

// Funcao registra pedido 
$resultado = registraPedido($_POST);
if ($resultado) {
  //Funcao envia e-mail 
  enviaEmail($resultado);
  //Funcao comunica API 
  comunicaAPI($resultado);
  echo 'SUCESSO';
} else {
  echo 'ERRO';
}
  • @Marcelobonifazio - I don’t need the email to confirm the request, it served only as an extra, so the act of registering the request does not necessarily need this step, it can be made after the answer given to the customer.

  • It turns out that the call functions of the email and the communication with the API should be made through PHP by security criteria. In this case, I do an Ajax call to a "request.php" script for example, and this records the request, calls the email function and the API function. I wanted to answer the AJAX call and continue running the other function in PHP without the page having to wait for the end of the script

  • The call I make by ajax, however it cannot be async because I need confirmation that the record has been saved

  • As I said before, I cannot call the email and API from Ajax for project security reasons, so it would have to be some alternative on the server side. Is there any way to force the script response to the client and still continue running the script without crashing the Ajax call?

  • And if after the reply of AJAX, you make another request AJAX to perform the other functions? For example: the customer made the order and returned a successful response, and then descends an order confirmation box and the hr he click on OK, you by way AJAX have the other two functions carried out?

  • that function comunicaAPI should follow the same logic of async? registraPedido is the only operation that can’t be async from what I understand, right?

  • Correct, the order log is the main one, after it the success message should be returned to the customer, already the mail and communicationAPI should be async not to lock the answer. I tried using this solution http://stackoverflow.com/questions/15273570/continue-processing-php-after-sending-http-response but it did not work.

  • I will give a better thought to your problem, try to create a minimal example here

  • @Ruhandeoliveirabaiense your PHP is running on which Operating System, GNU/Linux?

  • The application is running on a Linux hosting, with Apache, but as it is a reseller, I only have access to the settings by cPanel

Show 5 more comments

1 answer

1

Good afternoon Marcelo,

...At the moment the customer logs the order the system sends an email message and also communicates with an API by passing the order data. The problem is that this whole process is taking too long to run so that then I can give a successful response to the customer.

I have also had the same problem and learned that we cannot depend on external resources as SMTP server, so I suggest you fire these actions as background processes, here for me solved perfectly.

Example on GNU/Linux:

exec("php index.php envia_email/Enviaemailrequest index {$pedi->idPedido} > /dev/null 2>&1 &");

Where the part that really matters is the end of the command > /dev/null 2>&1 &

This way you have a process in the background (or asynchronous to your main process in Apache, the site with the graphical interface) that can take longer without worries, even in sending emails you can try to send N times if the first attempt fails.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.