Generate background report in php

1

In the system I am developing, it has come in the most critical part of reporting. I want to generate a report that when prompted by the client, it reports a message saying that as soon as the report is ready an email will be forwarded.

I already have the report ready, but it's a lot of data and it takes a while to be generated.

I use CodeIgniter and to generate the PDF the mPDF.

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

    
asked by anonymous 08.11.2017 / 13:20

1 answer

1

There are a few ways you can accomplish what you need, one of which is quite simple and the other would require shell interaction.

AJAX

The first and simplest of all, Request the script via AJAX. Being asynchronous, you do not have to wait for the response and will not crash the execution (by the browser).

Some caution is needed in this case. It is necessary to close the session so that, in a future request, the server is not busy, because a session is open. To do this, use session_write_close .

exec () and / or shell_exec ()

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

A little more information, you can get here: link

Threads

PHP has threads support, in which case you could use a thread without waiting for its response. In this case, I am "theorizing", therefore, never use a thread in practice without waiting for the answer (nor do I know if it is possible). But there is one more possible solution.

PHP: pthreads

Considerations

Regardless of the choice, your script will have its execution started, and then the user will be warned that the result will be sent by email (without the script being fully executed).

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 is important to understand how PHP handles a request:

  • Requested request (in this case, the browser);
  • Request received (by server);
  • Script execution;
  • Output / Output;
  • End of execution.
  • Another detail is that steps 3 and 4 can be interleaved. PHP does not need to run the entire script so that it can perform the output. The output can be performed while running a script.

    When the 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 . Its default is false .

    This policy basically defines that if a connection is not active, script execution will be aborted. That is, if the browser (or any other type of client) is not waiting for the request, the execution of the script will be interrupted.

    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 have to worry about that.

    If this were a necessity, you would need to change the directive to true or use the ignore_user_abort()

        
    08.11.2017 / 13:34