0

I have a ajax script that on occations can take from 10-30 seconds to execute. During its execution of a PHP file, the PHP script sets a session parameter to different states. The idea behind this is to have a second ajax run async with the first, to update the current workflow.

To do this I am using the following with ajaxStart and ajaxStop:

function getScriptState(){
    $.ajax({
        url : "/modules/core.iris/ajax/getState.ajax.php",
        type : "GET",
        dataType : "json",
        async : true,
        global: false,
        success : function (d){
            if( $('#statusMsg').length !== 0 ){
                if( typeof d.statusMsg !== "undefined" ){
                    $('#statusMsg').html( d.statusMsg );
                }
            }
        }
    });
}
function clearScriptState(){
    $.ajax({
        url : "/modules/core.iris/ajax/clearState.ajax.php",
        ...
    });
}
var loaderMsg = null;
$( document ).ajaxStart(function() {
    getScriptState();
    loaderMsg = setInterval( getScriptState, 500 );
});
$( document ).ajaxStop(function() {
    clearScriptState();
    clearInterval( loaderMsg );
});

The script calling the longrunning PHP script is as follows:

$.ajax({
    url : "/modules/core.wages/ajax/wagesLines.ajax.php",
    async : true,
    type : "POST",
    data: weeks,
    dataType: "json",
    success : function(d){  
    ....

The problem is, that getScriptState() isnt actually running async with the above script. It actually doesnt return the state until a millisecond before the longrunning ajax call has finished.

From my console log when it finishes, it looks like it has run twice before longrunning script has finished, but this doesnt reflect in the DOM.

Here is a picture of the network timeline in chrome. Showing the getState getting called once, then the result of wageLines.ajax.php is run, and then getState is run again several times. Those several times should infact be before the wageLines.ajax.php since it was not finished fetching the data:

enter image description here

As shown in this screenshot, the problem is that all of the calls to getState.ajax.php are being run until the wagesLines.ajax.php is done. So no matter if getState.ajax.php only takes 100ms to execute, it doesnt finish until wagesLines.ajax.php does.

Anyone got any ideas?

6
  • 1
    Perhaps the number of connections to your server is limited, so the polling requests are queued and executed after the long request has finished? Commented Nov 21, 2014 at 14:13
  • Have you checked to see what the status test is returning? Commented Nov 21, 2014 at 14:19
  • They are all returning {"statusMsg":"Fetching wage lines"} which is correct. Until the clearState.ajax.php is run. also @derMrich my server is configured to allow multiple connections. Also, as you can see from my screenshot above, multiple are being run simultaneously Commented Nov 21, 2014 at 14:22
  • I don't understand what's going on with the use of .ajaxStart() and .ajaxStop() here. edit well I guess I do. So have you added console.log() calls to the status check "success" method to see when it's running? I think, for what it's worth, that it would be safer to start the next status check from the success handler instead of from a timer to avoid having multiple status checks in process at the same time. Commented Nov 21, 2014 at 14:26
  • ajaxStart and ajaxStop are functions run when an ajax call start and when it stop. No matter if it stops due to an success or an error. So when I start an ajaxcall, I also start the function getScriptState Commented Nov 21, 2014 at 14:29

1 Answer 1

1

This problem occurs due to the fact that both scripts being called are trying to access the same PHP session.

PHP by default locks the session until the script is finished.

It can be fixed by using session_write_close().

Reference: Long running background PHP script blocks other PHP pages until it is finished

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.