I have added a script to an admin page on a magento site to initiate a long running process and periodically query the progress using AJAX
(or XHR
) requests so that it can update a progress bar.
The first request is to the controller function that starts a long running process:
var startfeed = new XMLHttpRequest();
startfeed.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
feedComplete(this);
}
};
startfeed.open("GET", thisFeedUrl, true);
startfeed.send();
In the code above, the thisFeedUrl
variable is the appropriate URL for the following controller action:
public function thistakesalongtimeAction()
{
very_slow_function();
}
The very_slow_function
writes it's current progress to a file, which can be accessed by the following controller action:
public function getprogressAction(){
$progressFileName = getProgressFileName();
echo file_get_contents($progressFileName);
}
This controller is accessed by the following javascript:
var feedProgress = new XMLHttpRequest();
feedProgress.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
progress = JSON.parse(this.responseText);
// Update progress bar
setProgress(progress);
}
};
feedProgress.open("GET", thisFeedUrl, true);
feedProgress.send();
The problem is that the requests for progress do not complete until the long running action has completed. How can I get the progress bar to update correctly while the long running process is incomplete.