Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with AJAX polling session

Status
Not open for further replies.

chapkom

Programmer
Feb 24, 2012
7
0
0
US
Platform: PHP 5.3.9 with jQuery 1.7.1

I'm at wit's end with this one. I'm firing off two asynchronous AJAX calls at the same time, generateTPM() and updateProgress(). generateTPM() initiates a process that takes 50 seconds and updateProgress() polls a text file containing a JSON object representing the status of the process. updateProgress() will not return any results until generateTPM() is finished running, even though both are asynchronous.

$('#uiGenerateTPM').click(function(){
updateProgress();
generateTPM();
});

function generateTPM(){
var data = {
action:'generateTPM'};
var settings = {async:true,
success: function(json){
$('#uiMessageOverlayContent').append('<div id="uiProcessComplete">' + json.message + '</div>');
$('#uiMessageOverlayButtonContainer').html('<img src="/images/ui/ok.png" border="0" id="uiButtonOK" />');
$('#uiButtonOK').click(function(){$('#uiMessageOverlay').hide();});
},
data:data,
cache:false,
type: 'POST',
dataType:"json"};

$('#uiButtonOK').unbind("click");
$('#uiMessageOverlayButtonContainer').html('');
$('#uiMessageOverlay').show();
$('#uiMessageOverlayTitle').html('MRP System Status');
$('#uiMessageOverlayContent').html('Generating Time Phased Material Requirements<br />This may take a few minutes.');

var html = '<div id="uiMpsStatus">&nbsp;</div>';
html = html + '<div id="uiTpmGenStatusContainer">';
html = html + '<div id="uiTpmGenProgressBarContainer"><div id="uiTpmGenProgressBar">&nbsp</div></div>';
html = html + '<div id="uiTpmGenStatus">&nbsp;</div>';
html = html + '<div id="uiTpmGenProgress">&nbsp;</div>';
html = html + '<div id="uiTpmGenProgress">&nbsp;</div>';
html = html + '</div>';
html = html + '<div id="uiTpmGenEndStatusContainer">';
html = html + '<div id="uiTpmGenEndStatus">&nbsp;</div>';
html = html + '<div id="uiTpmInsertStatus">&nbsp;</div>';
html = html + '</div>';

$('#uiMessageOverlayContent').append(html);
$('#uiTpmGenProgressBarContainer').hide();
$.ajax('/ajax/mrp.ajax.php', settings);
}

function updateProgress() {
var data = {
action:'getProgressUpdate'};
var settings = {async:true,
success: function(json){
var done = false;

if(json!=null) {
if(json.MPS!=null) {
$('#uiMpsStatus').html(json.MPS.status);
}
if(json.TPM_Gen!=null) {
$('#uiTpmGenStatus').html(json.TPM_Gen.status);
var progress = ((Math.round(parseFloat(json.TPM_Gen.progress)*10000)/10000) * 100).toFixed(2);
$('#uiTpmGenProgress').html(' (' + progress + '%)');
$('#uiTpmGenProgressBarContainer').show();
$('#uiTpmGenProgressBar').css('width', progress + '%');
}
if(json.TPM_Gen_End!=null) {
$('#uiTpmGenEndStatus').html(json.TPM_Gen_End.status);
}
if(json.TPM_Insert!=null) {
$('#uiTpmInsertStatus').html(json.TPM_Insert.status);
}
if(json.Done==null) {
var t2 = setTimeout("updateProgress()", 1000);
} else {
clearTimeout(t2);
done = true;
clearProgressLog();
}
} else {
var t2 = setTimeout("updateProgress()", 1000);
}
},
data:data,
cache:false,
type: 'POST',
dataType:"json"};

$.ajax('/ajax/polling.ajax.php', settings);
}

As you can see, I'm making AJAX requests from two different files. The order in which I call the functions doesn't matter. Both possible ways give me the same result. The really bizarre thing is that I have this setup working on another part of the application. In fact, this very setup was working a couple of days ago until it suddenly broke yesterday, even though I have not changed this code in three weeks!

As I'm sure you can imagine, I'm scratching my head on this one. Any ideas or suggestions would be appreciated.

 
The problem exists in all four major browsers (Firefox, Chrome, IE, Safari)
 
Hi

Interesting. Next I would take a look at the server side.
[ul]
[li]Any update happened for the web server, PHP, PHP module, anything to which PHP connects ?[/li]
[li]File and directory permissions are the same as earlier ?[/li]
[li]Something interesting mentioned in the web server or any other log ?[/li]
[/ul]


Feherke.
 
I had made a change to the PHP function that generates the TPM. The function still writes to the progress log file and' does not generate any errors in the error log. I don't have access to the source code at this time, but that does give me a good place to start looking.

I'll take a look Monday morning and post back with what I figure out.
 
I just tested this with an old version of the function that generates the TPM (let's hear it for redundant backups and SVN) and the problem persists. This is starting to look like it's not code related.

The old version was very slow, which is the primary reason I rewrote it, so I killed the httpd process associated with generateTPM() and my polling AJAX started working. It's definitely related to generateTPM() in some way, but the fact that the problem occurred with an old version that was previously working is leading me to believe that it's not the code.

There haven't been any changes in the permissions on the webserver and the error log is giving me nothing to go on. I've confirmed the problem on multiple devices, some of which had not previously used the application so I think I've safely ruled out a caching issue.

However... I did notice that my phpMyAdmin updated itself out of the blue at about the same time this problem started... Could that be related in some way?
 
Further testing:

Commented out the parts of the PHP code that write to the progress log file. No change in polling behavior.

Set mrp.ajax.php to run a function that loops from 1 to 1000000000 and doesn't do anything else. No change in polling behavior.

Disabled the ajax call to mrp.ajax.php within generateTPM(). updateProgress() is called every second exactly the way it should be.

Deleted the async:true setting from my ajax calls to allow the jQuery default of true to take effect. The problem persists.

The phpMyAdmin change I mentioned earlier was caused by an expiration of my browser's cache.

Is there any setting in apache itself that could cause this issue? I'm seriously running out of ideas here.
 
Additional testing:

Disabled the ajax code that checks user permissions to generate the TPM in mrp.ajax.php. Polling works as intended.

Re-enabled the PHP include for the user class. Polling problem returned.

Looks like I just caught a break.
 
Turns out I had a circular reference in my PHP code. User.class.php was calling alert.func.php which was calling... User.class.php.

I think I might have just found my problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top