Tnsol17
Tnsol17

Reputation: 11

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 !

Upvotes: 0

Views: 653

Answers (3)

guy_fawkes
guy_fawkes

Reputation: 965

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

Upvotes: 0

Leo
Leo

Reputation: 345

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

Upvotes: 0

KoalaBear
KoalaBear

Reputation: 2948

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.

Upvotes: 1

Related Questions