_batch_process

Versions
6 – 7
_batch_process()

Process sets in a batch.

If the batch was marked for progressive execution (default), this executes as many operations in batch sets until an execution time of 1 second has been exceeded. It will continue with the next operation of the same batch set in the next request.

Return value

An array containing a completion value (in percent) and a status message.

▾ 3 functions call _batch_process()

batch_process in includes/form.inc
Processes the batch.
_batch_do in includes/batch.inc
Do one pass of execution in JavaScript-mode and return progress to the browser.
_batch_progress_page_nojs in includes/batch.inc
Output a batch processing page without JavaScript support.

Code

includes/batch.inc, line 240

<?php
function _batch_process() {
  $batch       = &batch_get();
  $current_set = &_batch_current_set();
  // Indicate that this batch set needs to be initialized.
  $set_changed = TRUE;

  // If this batch was marked for progressive execution (e.g. forms submitted by
  // drupal_form_submit()), initialize a timer to determine whether we need to
  // proceed with the same batch phase when a processing time of 1 second has
  // been exceeded.
  if ($batch['progressive']) {
    timer_start('batch_processing');
  }

  while (!$current_set['success']) {
    // If this is the first time we iterate this batch set in the current
    // request, we check if it requires an additional file for functions
    // definitions.
    if ($set_changed && isset($current_set['file']) && is_file($current_set['file'])) {
      include_once DRUPAL_ROOT . '/' . $current_set['file'];
    }

    $task_message = '';
    // We assume a single pass operation and set the completion level to 1 by
    // default.
    $finished = 1;
    if ((list($function, $args) = reset($current_set['operations'])) && function_exists($function)) {
      // Build the 'context' array, execute the function call, and retrieve the
      // user message.
      $batch_context = array(
        'sandbox'  => &$current_set['sandbox'],
        'results'  => &$current_set['results'],
        'finished' => &$finished,
        'message'  => &$task_message,
      );
      // Process the current operation.
      call_user_func_array($function, array_merge($args, array(&$batch_context)));
    }

    if ($finished == 1) {
      // Make sure this step is not counted twice when computing $current.
      $finished = 0;
      // Remove the processed operation and clear the sandbox.
      array_shift($current_set['operations']);
      $current_set['sandbox'] = array();
    }

    // When all operations in the current batch set are completed, browse
    // through the remaining sets until we find a set that contains operations.
    // Note that _batch_next_set() executes stored form submit handlers in
    // remaining batch sets, which can add new sets to the batch.
    $set_changed = FALSE;
    $old_set = $current_set;
    while (empty($current_set['operations']) && ($current_set['success'] = TRUE) && _batch_next_set()) {
      $current_set = &_batch_current_set();
      $set_changed = TRUE;
    }
    // At this point, either $current_set contains operations that need to be
    // processed or all sets have been completed.

    // If we are in progressive mode, break processing after 1 second.
    if ($batch['progressive'] && timer_read('batch_processing') > 1000) {
      // Record elapsed wall clock time.
      $current_set['elapsed'] = round((microtime(TRUE) - $current_set['start']) * 1000, 2);
      break;
    }
  }

  if ($batch['progressive']) {
    // Gather progress information.

    // Reporting 100% progress will cause the whole batch to be considered
    // processed. If processing was paused right after moving to a new set,
    // we have to use the info from the new (unprocessed) set.
    if ($set_changed && isset($current_set['operations'])) {
      // Processing will continue with a fresh batch set.
      $remaining        = count($current_set['operations']);
      $total            = $current_set['total'];
      $progress_message = $current_set['init_message'];
      $task_message     = '';
    }
    else {
      // Processing will continue with the current batch set.
      $remaining        = count($old_set['operations']);
      $total            = $old_set['total'];
      $progress_message = $old_set['progress_message'];
    }

    $current = $total - $remaining + $finished;
    $percentage = _batch_api_percentage($total, $current);

    $elapsed    = $current_set['elapsed'];
    // Estimate remaining with percentage in floating format.
    $estimate   = $elapsed * ($total - $current) / $current;
    $values     = array(
      '@remaining'  => $remaining,
      '@total'      => $total,
      '@current'    => floor($current),
      '@percentage' => $percentage,
      '@elapsed'    => format_interval($elapsed / 1000),
      '@estimate'   => format_interval($estimate / 1000),
    );
    $message = strtr($progress_message, $values);
    if (!empty($message)) {
      $message .= '<br />';
    }
    if (!empty($task_message)) {
      $message .= $task_message;
    }

    return array($percentage, $message);
  }
  else {
    // If we are not in progressive mode, the entire batch has been processed.
    return _batch_finished();
  }
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.