简体   繁体   中英

PHP long running script, jQuery to avoid timeout?

I'm working on a business app which relies on batch PDF document creation.

At a certain time my app has to create dozens of PDF files and this makes the script timeout...

I do not have any access to the "set_time_limit" parameters on my hosting.

For now I rely on manual action : my script runs for 5 files, then stops. I refresh the browser page, relaunch the script for 5 other, and so on. This is not a reliable solution as the user must stay focused and periodically hit refresh !

I'm looking for a solution that would "replace the manual user action". PHP doesn't seem to be the solution (due to the timeout limits). Would jQuery be the answer ?

How ?

I searched for "long polling" or "long running" topics but didn't get any solution that would fit.

Thanks for the help !

It will never be a nice solution. But the way you want to solve it is possible. But still not recommended :)

function callBackup() {
    $.ajax({
        url : 'backup.php',
        success : function(response) {
            // Simple check if backup processed all files
            if (response != '1') {
                // Run backup again
                callBackup();
            } else {
                alert('Backup done');
            }
        }
    });
}

Your script at the server needs to echo '1'; if you have processed everything.

I don't think @KoalaBear's solution will work. It is PHP that is timing out not Jquery.

Set_time_limit(0) is easiest way to get around this.

Another solution is to delegate this timing consuming task to a background process.

check Gearman

A good example of background job

But if you use Gearman you have to periodically polling the server to check the status of the Job

You can do this by background workers. Look into intoGearman, Redis resque or celery task queues. Don't let your client wait.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM