I’ve been using the Batch API successfully to do processing that would normally lead to PHP timeouts or out of memory errors, and it’s been working nicely.
I’ve looked through the code a little, but I’m still unclear about what’s happening behind the scenes.
Could someone familiar with the process describe how it works?
What happens is that, to avoid PHP time outs, the browser periodically pings through AJAX the URL (http://example.com/batch?id=$id) that causes the batch operations to be executed.
See _batch_page(), which is the function called by system_batch_page(), the menu callback for the “batch” path.
In _batch_progress_page_nojs(), you will notice the following code.
Setting the “Refresh” meta tag will cause the page to refresh.
Similar code is present in Drupal 7; the difference is that the code has been ported, and it uses the new functions Drupal 7 implements.
When JavaScript is enabled, the code that does all the work is in the batch.js file.
The polling of the batch URL starts with
progress.startMonitoring(uri+'&op=do', 10). The batch.js file depends from the functionality exposed inDrupal.progressBar, which is defined in the progress.js file.Similar code is used in Drupal 7, which uses a slightly different version of the batch.js, and progress.js files.
The differences are that, since Drupal 7, all the jQuery code is wrapped in
(function ($) { })(jQuery);, and that the jQuery Once plugin is included with Drupal 7. Drupal 7 sets also WAI-ARIA attributes for compatibility with screen readers; this happens also in the HTML added from JavaScript code, such as the following, found in the progress.js file.When serving a batch page, Drupal sets _batch_shutdown() as shutdown callback; when PHP shuts down because a timeout, the function updates the batch array in the database.