Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 967665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:21:22+00:00 2026-05-16T02:21:22+00:00

I’ve been using the Batch API successfully to do processing that would normally lead

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T02:21:22+00:00Added an answer on May 16, 2026 at 2:21 am

    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.

    function _batch_page() {
      $batch = &batch_get();
    
      // Retrieve the current state of batch from db.
      if (isset($_REQUEST['id']) && $data = db_result(db_query("SELECT batch FROM {batch} WHERE bid = %d AND token = '%s'", $_REQUEST['id'], drupal_get_token($_REQUEST['id'])))) {
        $batch = unserialize($data);
      }
      else {
        return FALSE;
      }
    
      // Register database update for end of processing.
      register_shutdown_function('_batch_shutdown');
    
      $op = isset($_REQUEST['op']) ? $_REQUEST['op'] : '';
      $output = NULL;
      switch ($op) {
        case 'start':
          $output = _batch_start();
          break;
    
        case 'do':
          // JS-version AJAX callback.
          _batch_do();
          break;
    
        case 'do_nojs':
          // Non-JS progress page.
          $output = _batch_progress_page_nojs();
          break;
    
        case 'finished':
          $output = _batch_finished();
          break;
      }
    
      return $output;
    }
    

    In _batch_progress_page_nojs(), you will notice the following code.

      $url = url($batch['url'], array('query' => array('id' => $batch['id'], 'op' => $new_op)));
      drupal_set_html_head('<meta http-equiv="Refresh" content="0; URL=' . $url . '">');
      $output = theme('progress_bar', $percentage, $message);
      return $output;
    

    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.

      // Merge required query parameters for batch processing into those provided by
      // batch_set() or hook_batch_alter().
      $batch['url_options']['query']['id'] = $batch['id'];
      $batch['url_options']['query']['op'] = $new_op;
    
      $url = url($batch['url'], $batch['url_options']);
      $element = array(
        '#tag' => 'meta', 
        '#attributes' => array(
          'http-equiv' => 'Refresh', 
          'content' => '0; URL=' . $url,
        ),
      );
      drupal_add_html_head($element, 'batch_progress_meta_refresh');
    
      return theme('progress_bar', array('percent' => $percentage, 'message' => $message));
    

    When JavaScript is enabled, the code that does all the work is in the batch.js file.

    /**
     * Attaches the batch behavior to progress bars.
     */
    Drupal.behaviors.batch = function (context) {
      // This behavior attaches by ID, so is only valid once on a page.
      if ($('#progress.batch-processed').size()) {
        return;
      }
      $('#progress', context).addClass('batch-processed').each(function () {
        var holder = this;
        var uri = Drupal.settings.batch.uri;
        var initMessage = Drupal.settings.batch.initMessage;
        var errorMessage = Drupal.settings.batch.errorMessage;
    
        // Success: redirect to the summary.
        var updateCallback = function (progress, status, pb) {
          if (progress == 100) {
            pb.stopMonitoring();
            window.location = uri+'&op=finished';
          }
        };
    
        var errorCallback = function (pb) {
          var div = document.createElement('p');
          div.className = 'error';
          $(div).html(errorMessage);
          $(holder).prepend(div);
          $('#wait').hide();
        };
    
        var progress = new Drupal.progressBar('updateprogress', updateCallback, "POST", errorCallback);
        progress.setProgress(-1, initMessage);
        $(holder).append(progress.element);
        progress.startMonitoring(uri+'&op=do', 10);
      });
    };
    

    The polling of the batch URL starts with progress.startMonitoring(uri+'&op=do', 10). The batch.js file depends from the functionality exposed in Drupal.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.

    (function ($) {
    
    /**
     * Attaches the batch behavior to progress bars.
     */
    Drupal.behaviors.batch = {
      attach: function (context, settings) {
        $('#progress', context).once('batch', function () {
          var holder = $(this);
    
          // Success: redirect to the summary.
          var updateCallback = function (progress, status, pb) {
            if (progress == 100) {
              pb.stopMonitoring();
              window.location = settings.batch.uri + '&op=finished';
            }
          };
    
          var errorCallback = function (pb) {
            holder.prepend($('<p class="error"></p>').html(settings.batch.errorMessage));
            $('#wait').hide();
          };
    
          var progress = new Drupal.progressBar('updateprogress', updateCallback, 'POST', errorCallback);
          progress.setProgress(-1, settings.batch.initMessage);
          holder.append(progress.element);
          progress.startMonitoring(settings.batch.uri + '&op=do', 10);
        });
      }
    };
    
    })(jQuery);
    

    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.

      // The WAI-ARIA setting aria-live="polite" will announce changes after users
      // have completed their current activity and not interrupt the screen reader.
      this.element = $('<div class="progress" aria-live="polite"></div>').attr('id', id);
      this.element.html('<div class="bar"><div class="filled"></div></div>' +
                        '<div class="percentage"></div>' +
                        '<div class="message">&nbsp;</div>');
    

    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.

    // Drupal 6.
    function _batch_shutdown() {
      if ($batch = batch_get()) {
        db_query("UPDATE {batch} SET batch = '%s' WHERE bid = %d", serialize($batch), $batch['id']);
      }
    }
    
    // Drupal 7.
    function _batch_shutdown() {
      if ($batch = batch_get()) {
        db_update('batch')
          ->fields(array('batch' => serialize($batch)))
          ->condition('bid', $batch['id'])
          ->execute();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I know there's a lot of other questions out there that deal with this
I'm making a simple page using Google Maps API 3. My first. One marker
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.