Generate report in the background in php

Asked

Viewed 620 times

2

In the system that I am developing has reached the most critical part that is the generation of reports. I wish to generate a report that when requested by the client, he informs a message saying that as soon as the report is ready an e-mail will be sent informing.

I already have the report ready, but it is a lot of data and it takes time to be generated.

I use Codeigniter and to generate PDF mPDF.

I need suggestions to do this way or even how to create a service that runs in the background in PHP to generate the report

  • I believe you’re interested in competition, right? If so, do a Threads search with php: http://php.net/manual/en/class.thread.php

1 answer

2


There are some ways to accomplish what you need, one of them very simple and the other would need interaction with the shell.

AJAX

The first and simplest of all, Request the script via AJAX. Being it asynchronous, you do not need to wait for the answer and will not catch the execution (on the part of the browser).

Some care is needed in this case. It is necessary to close the session so that in a future request, the server is not occupied by a session open. For this, use session_write_close.

exec() and/or shell_exec()

You can use the operating system itself to run or schedule the execution of the script. However, it is worth noting that direct access to shell is dangerous and should be avoided (or used very carefully).

A little more information, you can get here: https://stackoverflow.com/a/4628279/1628790

Threads

PHP has support for threads and in that case you could use a thread without waiting for her answer. It is worth noting that in this case I am "theorizing", because I never used in practice a thread without waiting for the answer (I don’t even know if it’s possible). But there is one more possible solution.

PHP: pthreads

Considerations

Regardless of the choice, your script will have its execution started and, soon after, will be notified to the user that the result will be sent by email (without the script has been executed completely).

Of all the solutions, I would choose the first one. Easy, fast and will work smoothly and with few changes to your code.

UPDATE

It was asked, in the comments, if the user closing the browser would not interrupt the execution of the script in PHP.

In this case, it’s important to understand how PHP handles a request:

  1. Request made (in this case, the browser);
  2. Request received (by server);
  3. Script execution;
  4. Output/output;
  5. Termination of the execution.

Another detail is that steps 3 and 4 can be interspersed. PHP does not need to run the entire script to perform the output. Output can be performed while executing a script.

When output is performed, PHP will check if there is an active connection waiting for a response. At this point, a directive called ignore_user_abort. Her default pattern is false.

Basically, this directive defines that if a connection is not active, the execution of the script will be stopped. That is, if there is no browser (or any other type of client) waiting for the request, the execution of the script will be stopped.

However, PHP is only aware that the connection does not exist when trying to perform the first output.

PHP: ignore_user_abort - Notes

PHP will not Detect that the user has aborted the Connection until an Attempt is made to send information to the client. Simply using an echo statement does not Guarantee that information is sent, see flush().

In this case, your script will run until some information is sent to the client. As in your case, no information will be sent to the customer, you will not need to worry about it.

If that were a necessity, it would be enough to amend the directive to true or use the function ignore_user_abort()

  • Using AJAX I could do as follows: The client requests the report then shows the message that will be forwarded an email when it is ready and he can leave the browser (close) and even then he will receive the email? Or any of the ones you talked about?

  • @Weslley understands that once the request is sent to the server, there is no way to stop it from the browser. The server will receive the request, execute it, and output (reply). The only difference is that you won’t have a browser waiting for the answer from the server (which is not necessary in your case).

  • I understood Gabriel, I even tested and it worked. What I must do now is optimize the report to avoid timeout. In case, in the server part could use Thread to process the request?

  • @Weslley did an update on the answer about an interesting fact (but it does not apply to your case). You can use thread yes, but simply using thread will not improve the performance of your script. It is more interesting to check something is the delay of execution and act upon the bottlenecks.

  • Thank you Gabriel, your reply helped me a lot!

Browser other questions tagged

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