Parallel function on website

Asked

Viewed 42 times

1

I have a class that generates a file .xls for download. The problem is that it has more than 30,000 entries, so it takes about 10 minutes to generate. And in that time I have to keep the page open loading.
I remember some sites that I used to convert files in which after finishing the upload I could close the page, the browser and even turn off the computer that when the process was finished I would receive an email with my file attached.
My intention is to do the same with this file. The user clicks to download, then is redirected to a page that says he will receive the file in the email and meanwhile he can close the page and do something else.

In my researches I ended up finding the pcntl_fork(), that seemed to be perfect, until I discovered that it only works if I rotate the .php directly from the terminal. I couldn’t find anything that works on websites.

2 answers

1


I solved it in a very simple way. I modified the generate button .xlsx so that instead of generating, it calls in the terminal the code it generates with shell_exec("php nomeDoArquivo.php"), but even so he is waiting for the output of the terminal, then ended up taking equal.

To resolve this, as the terminal output does not interest me¹ I asked to ignore it by modifying the command to:

$your_command = "php nomeDoArquivo.php";
shell_exec( $your_command . "> /dev/null 2>/dev/null &" );

Source: Mat

[1]: Great care when ignoring the output, if you have any error in the process you will never know. As my script already sends an attached email I already set it to when it is error send an email to me with the error message.

0

A solution, perhaps not very elegant, would be to queue this file (managed in a database table and/or directory), and on the server would have a cronjob PHP script that would do the job of generating the file and sending it to the email from time to time.

Browser other questions tagged

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