1

I have a very simple PHP script:

ini_set('max_execution_time', 240000);

//Switch for terminating the test processing status loop
$completed = false; 

while ($completed == false) {
    sleep(2);
}

This is launched using AJAX which is again, very simple:

$.ajax({
    url: "lib/check_processing_status.php",
    timeout: 0,
    async: true,
    error: function() {
        console.log("FAILURE");
    },
    success: function() {
        console.log("SUCCESS");
    }
});

This script should run indefinitely, or at least until the timeout is triggered. Yet it fails after a duration which differs each time it is run. Sometimes just 40 seconds, sometimes 1 minute and 10 seconds.

Why is this failing!?

Additional:

Just to add some context, the reason I am having a loop like this, is because I will be querying a server for the progress of a process, so instead of calling loads of AJAX requests every 5 seconds (filling up the console window with 10's of requests), I figured it neater to stay inside the PHP script until the process is complete and then return when its complete.

Tony Hinkle
  • 4,706
  • 7
  • 23
  • 35
Single Entity
  • 2,925
  • 3
  • 37
  • 66
  • 1
    Even if you set the ajax timeout to 0, your browser may not wait indefinitely: http://stackoverflow.com/questions/7297833/how-long-will-the-browser-wait-after-an-ajax-request – Mike Scotty Jun 14 '16 at 14:08
  • Would you say the browser is terminating the php script? – Single Entity Jun 14 '16 at 14:08
  • Not the php script, the connection. – Mike Scotty Jun 14 '16 at 14:10
  • The connection will time-out after some time, you should add the while/sleep in the front-end instead of the back-end. So do a request, get a false as result and after some seconds do an other request. – R Pelzer Jun 14 '16 at 14:12
  • Ironically that was how I originally coded it, but I wasn't happy with the many PHP requests in the console window for debugging purposes, so I changed it, I didn't realise browsers can kill an active connection. – Single Entity Jun 14 '16 at 14:16
  • 1
    What you are trying to do is similar to "long polling" in jQuery (similar problems may occur here). There are multiple articles about this out on the web – ssc-hrep3 Jun 14 '16 at 14:18
  • Ah indeed, I found this which is very informative: https://techoctave.com/c7/posts/60-simple-long-polling-example-with-javascript-and-jquery – Single Entity Jun 14 '16 at 14:20
  • The above is what I was doing the first time round, well, I've learnt something here, thank you everybody. – Single Entity Jun 14 '16 at 14:23

1 Answers1

0

The problem is the browser was killing the PHP scripts connection, even though the script was configured not to time out for over an hour and was running as intended. I assume the browser thought it was not functioning correctly.

This implies polling a server using a long running PHP script isn't really possible, you have to do it on the JavaScript side or consider a more involved long polling solution which is explained nicely here:

https://techoctave.com/c7/posts/60-simple-long-polling-example-with-javascript-and-jquery

Big thanks to those in the comments of the question who answered this.

Single Entity
  • 2,925
  • 3
  • 37
  • 66